c# - Importing submodule using custom importer contains "<module>" in find_module fullname parameter -


currently i'm working on custom importer ironpython, should add abstraction layer writing custom importer. abstraction layer ironpython module, bases on pep 302 , ironpython zipimporter module. architecture looks this:

architexture

for testing importer code, i've written simple test package modules, looks this:

/math/     __init__.py     /mathimpl/              __init__.py              __math2__.py 

/math/__init__.py:

print ('import: /math/__init__.py') 

/math/mathimpl/__init__.py:

# sample math package print ('begin import /math/mathimpl/__init__.py') import math2 print ('end import /math/mathimpl/__init__.py: ' + str(math2.add(1, 2))) 

/math/mathimpl/math2.py:

# add 2 values def add(x, y):     return x + y print ('import math2.py!') 

if try import mathimpl in script: import math.mathimpl

my genericimporter get's called , searchs module/package in find_module method. returns instance of importer if found, else not:

public object find_module(codecontext/*!*/ context, string fullname, params object[] args) {     // set module     if (fullname.contains("<module>"))     {         throw new exception("why, why fullname contains <module>?");     }      // find resolver     foreach (var resolver in host.resolver)     {         var res = resolver.getmoduleinformation(fullname);          // if script resolved resolver         if (res != resolvedtype.none)         {             this.resolver = resolver;             return this;         }     }     return null; } 

if find_module called first time,fullname contains math, ok, because math should imported first. second time find_module called, math.mathimpl should imported, problem here is, fullname has value <module>.mathimpl, instead of math.mathimpl.

my idea was, module name (__name__) not set correctly when math imported, set in case when importing module in load_module:

public object load_module(codecontext/*!*/ context, string fullname) {     string code = null;     genericmodulecodetype moduletype;     bool ispackage = false;     string modpath = null;     pythonmodule mod;     pythondictionary dict = null;      // go through available import types search-order     foreach (var order in _search_order)     {         string tempcode = this.resolver.getscriptsource(fullname + order.key);          if (tempcode != null)         {             moduletype = order.value;             code = tempcode;             modpath = fullname + order.key;              console.writeline("     import: " + modpath);              if ((order.value & genericmodulecodetype.package) == genericmodulecodetype.package)             {                 ispackage = true;             }              break;         }     }      // of no code loaded     if (code == null)     {         return null;     }      var scriptcode = context.modulecontext.context.compilesourcecode         (             new sourceunit(context.languagecontext, new sourcestringcontentprovider(code), modpath, sourcecodekind.autodetect),             new ironpython.compiler.pythoncompileroptions() { },             errorsink.default         );      // initialize module     mod = context.modulecontext.context.initializemodule(modpath, context.modulecontext, scriptcode, moduleoptions.none);      dict = mod.get__dict__();      // set values before execute script     dict.add("__name__", fullname);     dict.add("__loader__", this);     dict.add("__package__", null);      if (ispackage)     {         // add path         string subname = getsubname(fullname);         string fullpath = string.format(fullname.replace(".", "/"));          list pkgpath = pythonops.makelist(fullpath);         dict.add("__path__", pkgpath);     }     else     {         stringbuilder packagename = new stringbuilder();         string[] packageparts = fullname.split(new char[] { '/' });         (int = 0; < packageparts.length - 1; i++)         {             if (i > 0)             {                 packagename.append(".");             }              packagename.append(packageparts[i]);         }          dict["__package__"] = packagename.tostring();     }      var scope = context.modulecontext.globalscope;     scriptcode.run(scope);      return mod; } 

i hope 1 has idea, why happens. few line may cause problem are:

var scriptcode = context.modulecontext.context.compilesourcecode     (        new sourceunit(context.languagecontext, new sourcestringcontentprovider(code), modpath, sourcecodekind.autodetect),             new ironpython.compiler.pythoncompileroptions() { },             errorsink.default      ); 

and

mod = context.modulecontext.context.initializemodule(modpath, context.modulecontext, scriptcode, moduleoptions.none); 

because don't know, whether creating module way completly correct.

the problem can reproduced downloading project/branch: https://github.com/simplicbe/simplic.dlr/tree/f_res_noid , starting sample.importresolver. exception in find_module raised.

thank all!

this problem solved. modpath not allowed contains /. in general chars allowed, can in file-name.

maybe helpful else...


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -