Exemple #1
0
 def load_module(self, fullname):
     verbose = _memimporter.get_verbose_flag()
     if fullname in sys.modules:
         mod = sys.modules[fullname]
         if verbose:
             sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
         return mod
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
     initname = "init" + fullname.split(".")[-1] # name of initfunction
     filename = fullname.replace(".", "\\")
     if filename in ("pywintypes", "pythoncom"):
         filename = filename + "%d%d" % sys.version_info[:2]
         suffixes = ('.dll',)
     else:
         suffixes = self._suffixes
     for s in suffixes:
         path = filename + s
         if path in self._files:
             if verbose:
                 sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
             mod = _memimporter.import_module(fullname, path, initname, self.get_data)
             mod.__file__ = "%s\\%s" % (self.archive, path)
             mod.__loader__ = self
             if verbose:
                 sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
             return mod
     raise zipimport.ZipImportError("can't find module %s" % fullname)
Exemple #2
0
 def load_module(self, fullname):
     imp.acquire_lock()
     try:
         #print "loading module %s"%fullname
         if fullname in sys.modules:
             return sys.modules[fullname]
         mod = None
         c = None
         if self.extension == "py":
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = ("<memimport>/%s" % self.path).replace("/", sep)
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit(sep, 1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname] = mod
             code = compile(self.contents, mod.__file__, "exec")
             exec code in mod.__dict__
         elif self.extension in ["pyc", "pyo"]:
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = ("<memimport>/%s" % self.path).replace("/", sep)
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit(sep, 1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname] = mod
             c = marshal.loads(self.contents[8:])
             exec c in mod.__dict__
         elif self.extension in ("dll", "pyd", "so"):
             initname = "init" + fullname.rsplit(".", 1)[-1]
             path = fullname.replace(".", "/") + "." + self.extension
             #print "Loading %s from memory"%fullname
             #print "init:%s, %s.%s"%(initname,fullname,self.extension)
             mod = _memimporter.import_module(self.contents, initname,
                                              fullname, path)
             mod.__name__ = fullname
             mod.__file__ = ("<memimport>/%s" % self.path).replace("/", sep)
             mod.__loader__ = self
             mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname] = mod
     except Exception as e:
         if fullname in sys.modules:
             del sys.modules[fullname]
         import traceback
         exc_type, exc_value, exc_traceback = sys.exc_info()
         traceback.print_tb(exc_traceback)
         print "PupyPackageLoader: Error while loading package %s (%s) : %s" % (
             fullname, self.extension, str(e))
         raise e
     finally:
         imp.release_lock()
     mod = sys.modules[
         fullname]  # reread the module in case it changed itself
     return mod
Exemple #3
0
    def load_module(self, fullname):
        global remote_print_error

        imp.acquire_lock()
        try:
            dprint('loading module {}'.format(fullname))
            if fullname in sys.modules:
                return sys.modules[fullname]

            mod = None
            if self.extension == 'py':
                mod = imp.new_module(fullname)
                mod.__name__ = fullname
                mod.__file__ = 'pupy://{}'.format(self.path)
                if self.is_pkg:
                    mod.__path__ = [mod.__file__.rsplit('/', 1)[0]]
                    mod.__package__ = fullname
                else:
                    mod.__package__ = fullname.rsplit('.', 1)[0]
                code = compile(self.contents, mod.__file__, 'exec')
                sys.modules[fullname] = mod
                exec(code, mod.__dict__)

            elif self.extension in ('pyc', 'pyo', 'pye'):
                mod = imp.new_module(fullname)
                mod.__name__ = fullname
                mod.__file__ = 'pupy://{}'.format(self.path)
                if self.is_pkg:
                    mod.__path__ = [mod.__file__.rsplit('/', 1)[0]]
                    mod.__package__ = fullname
                else:
                    mod.__package__ = fullname.rsplit('.', 1)[0]
                sys.modules[fullname] = mod
                try:
                    dprint('Load {} from marshalled file ({})'.format(
                        fullname, self.extension))
                    loadpy(self.contents[8:], mod.__dict__,
                           self.extension == 'pye')
                except Exception, e:
                    dprint('Load {} failed: Exception: {}'.format(fullname, e))

            elif self.extension in ('dll', 'pyd', 'so'):
                if '.' in fullname:
                    packagename, modulename = fullname.rsplit(".", 1)
                else:
                    packagename = modulename = fullname

                initname = "init" + modulename
                path = self.fullname.rsplit('.', 1)[0].replace(
                    ".", '/') + "." + self.extension
                dprint('Loading {} from memory'.format(fullname))
                dprint('init={} fullname={} path={}'.format(
                    initname, fullname, path))
                mod = _memimporter.import_module(self.contents, initname,
                                                 fullname, path)
                if mod:
                    mod.__name__ = modulename
                    mod.__file__ = 'pupy://{}'.format(self.path)
                    mod.__package__ = packagename
                    sys.modules[fullname] = mod
Exemple #4
0
 def load_module(self, fullname):
     verbose = _memimporter.get_verbose_flag()
     if fullname in sys.modules:
         mod = sys.modules[fullname]
         if verbose:
             sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
         return mod
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
     initname = "init" + fullname.split(".")[-1]  # name of initfunction
     filename = fullname.replace(".", "\\")
     if filename in ("pywintypes", "pythoncom"):
         filename = filename + "%d%d" % sys.version_info[:2]
         suffixes = (".dll",)
     else:
         suffixes = self._suffixes
     for s in suffixes:
         path = filename + s
         if path in self._files:
             if verbose:
                 sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
             mod = _memimporter.import_module(fullname, path, initname, self.get_data)
             mod.__file__ = "%s\\%s" % (self.archive, path)
             mod.__loader__ = self
             if verbose:
                 sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
             return mod
     raise zipimport.ZipImportError("can't find module %s" % fullname)
Exemple #5
0
        def create_module(self, spec):
            mod =  super().create_module(spec)
            if mod is None:
                verbose = _memimporter.get_verbose_flag()
                fullname = spec.name

                filename = fullname.replace(".", "\\")
                suffixes = self._suffixes
                initname = "PyInit_" + fullname.split(".")[-1]

                for s in suffixes:
                    path = filename + s
                    if path in self._files:
                        if verbose > 1:
                            sys.stderr.write("# found %s in zipfile %s\n"
                                            % (path, self.archive))
                        mod = _memimporter.import_module(fullname, path,
                                                        initname,
                                                        self.get_data, spec)
                        mod.__file__ = "%s\\%s" % (self.archive, path)
                        mod.__loader__ = self
                        mod.__memimported__ = True
                        if verbose:
                            sys.stderr.write("import %s # loaded from zipfile %s\n"
                                            % (fullname, mod.__file__))
                        return mod
Exemple #6
0
 def load_module(self, fullname):
     if sys.modules.has_key(fullname):
         mod = sys.modules[fullname]
         if DEBUG_ZIPIMPORT:
             sys.stderr.write(
                 "import %s # previously loaded from zipfile %s\n" %
                 (fullname, self.archive))
         return mod
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
     initname = "init" + fullname.split(".")[-1]  # name of initfunction
     filename = fullname.replace(".", "\\")
     path = filename + '.pyd'
     if path in self._files:
         if DEBUG_ZIPIMPORT:
             sys.stderr.write("# found %s in zipfile %s\n" %
                              (path, self.archive))
         code = self.get_data(path)
         mod = _memimporter.import_module(code, initname, fullname, path)
         mod.__file__ = "%s\\%s" % (self.archive, path)
         mod.__loader__ = self
         if DEBUG_ZIPIMPORT:
             sys.stderr.write("import %s # loaded from zipfile %s\n" %
                              (fullname, mod.__file__))
         return mod
     raise zipimport.ZipImportError, "can't find module %s" % fullname
Exemple #7
0
 def load_module(self, fullname):
     imp.acquire_lock()
     try:
         dprint('loading module {}'.format(fullname))
         if fullname in sys.modules:
             return sys.modules[fullname]
         mod=None
         c=None
         if self.extension=="py":
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = '<memimport>/{}'.format(self.path)
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit('/',1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname]=mod
             code = compile(self.contents, mod.__file__, "exec")
             exec code in mod.__dict__
         elif self.extension in ["pyc","pyo"]:
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = '<memimport>/{}'.format(self.path)
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit('/',1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname]=mod
             c=marshal.loads(self.contents[8:])
             exec c in mod.__dict__
         elif self.extension in ("dll","pyd","so"):
             initname = "init" + fullname.rsplit(".",1)[-1]
             path=fullname.replace(".",'/')+"."+self.extension
             dprint('Loading {} from memory'.format(fullname))
             dprint('init:{}, {}.{}'.format(initname,fullname,self.extension))
             mod = _memimporter.import_module(self.contents, initname, fullname, path)
             mod.__name__=fullname
             mod.__file__ = '<memimport>/{}'.format(self.path)
             mod.__loader__ = self
             mod.__package__ = fullname.rsplit('.',1)[0]
             sys.modules[fullname]=mod
     except Exception as e:
         if fullname in sys.modules:
             del sys.modules[fullname]
         import traceback
         exc_type, exc_value, exc_traceback = sys.exc_info()
         traceback.print_tb(exc_traceback)
         dprint('PupyPackageLoader: '
                    'Error while loading package {} ({}) : {}'.format(
                        fullname, self.extension, str(e)))
         raise e
     finally:
         imp.release_lock()
     mod = sys.modules[fullname] # reread the module in case it changed itself
     return mod
Exemple #8
0
 def load_module(self, fullname):
     imp.acquire_lock()
     try:
         #print "loading module %s"%fullname
         if fullname in sys.modules:
             return sys.modules[fullname]
         mod=None
         c=None
         if self.extension=="py":
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = ("<memimport>/%s" % self.path).replace("/",sep)
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit(sep,1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname]=mod
             code = compile(self.contents, mod.__file__, "exec")
             exec code in mod.__dict__
         elif self.extension in ["pyc","pyo"]:
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = ("<memimport>/%s" % self.path).replace("/",sep)
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit(sep,1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname]=mod
             c=marshal.loads(self.contents[8:])
             exec c in mod.__dict__
         elif self.extension in ("dll","pyd","so"):
             initname = "init" + fullname.rsplit(".",1)[-1]
             path=fullname.replace(".","/")+"."+self.extension
             #print "Loading %s from memory"%fullname
             #print "init:%s, %s.%s"%(initname,fullname,self.extension)
             mod = _memimporter.import_module(self.contents, initname, fullname, path)
             mod.__name__=fullname
             mod.__file__ = ("<memimport>/%s" % self.path).replace("/",sep)
             mod.__loader__ = self
             mod.__package__ = fullname.rsplit('.',1)[0]
             sys.modules[fullname]=mod
     except Exception as e:
         if fullname in sys.modules:
             del sys.modules[fullname]
         import traceback
         exc_type, exc_value, exc_traceback = sys.exc_info()
         traceback.print_tb(exc_traceback)
         print "PupyPackageLoader: Error while loading package %s (%s) : %s"%(fullname, self.extension, str(e))
         raise e
     finally:
         imp.release_lock()
     mod = sys.modules[fullname] # reread the module in case it changed itself
     return mod
def __bootstrap__():
   global __bootstrap__, __loader__, __file__
   import _memimporter, sys
   _memimporter.set_find_proc(lambda x: __loader__.get_data(r'%s%s' % (__loader__.prefix, x)))
   __file__ = r'%s%s.pyd' % (__loader__.prefix, __name__.split('.')[-1])
   mod = _memimporter.import_module(__loader__.get_data(__file__), 'init'+__name__.split('.')[-1], __name__, __file__)
   mod.__file__ = __file__
   mod.__loader__ = __loader__
   sys.modules[__name__] = mod
   del __bootstrap__, __loader__, __file__, _memimporter, sys
Exemple #10
0
    def load_module(self, fullname):
        verbose = _memimporter.get_verbose_flag()

        if fullname in sys.modules:
            mod = sys.modules[fullname]
            if verbose:
                sys.stderr.write(
                    "import %s # previously loaded from zipfile %s\n"
                    % (fullname, self.archive))
            return mod
        try:
            return zipimport.zipimporter.load_module(self, fullname)
        except ImportError as err:
            if verbose:
                sys.stderr.write("error loading %s: %s\n"% (fullname, err))

            if sys.version_info >= (3, 0):
                # name of initfunction
                initname = "PyInit_" + fullname.split(".")[-1]
            else:
                # name of initfunction
                initname = "init" + fullname.split(".")[-1]
            filename = fullname.replace(".", "\\")
            if filename in ("pywintypes", "pythoncom"):
                filename = filename + "%d%d" % sys.version_info[:2]
                suffixes = ('.dll',)
            else:
                suffixes = self._suffixes
            for s in suffixes:
                path = filename + s
                if path in self._files:
                    if verbose > 1:
                        sys.stderr.write("# found %s in zipfile %s\n"
                                         % (path, self.archive))
                    spec = importlib.util.find_spec(fullname, path)
                    mod = _memimporter.import_module(fullname, path,
                                                     initname,
                                                     self.get_data, spec)
                    mod.__file__ = "%s\\%s" % (self.archive, path)
                    mod.__loader__ = self
                    mod.__memimported__ = True
                    sys.modules[fullname] = mod
                    if verbose:
                        sys.stderr.write("import %s # loaded from zipfile %s\n"
                                         % (fullname, mod.__file__))
                    return mod
            raise zipimport.ZipImportError("can't find module %s" % fullname) from err
Exemple #11
0
 def load_module(self, fullname):
     if sys.modules.has_key(fullname):
         mod = sys.modules[fullname]
         if _memimporter.get_verbose_flag():
             sys.stderr.write(
                 "import %s # previously loaded from zipfile %s\n" %
                 (fullname, self.archive))
         return mod
     _memimporter.set_find_proc(self.locate_dll_image)
     filename = fullname.replace(".", "\\")
     try:
         for s in ('.pyd', '.pyc', '.py'):
             try:
                 with open(filename + s, 'U') as fp:
                     return imp.load_module(fullname, fp, filename + s,
                                            (s, 'U', 1))
             except:
                 continue
     except:
         pass
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
     initname = "init" + fullname.split(".")[-1]  # name of initfunction
     if filename in ("pywintypes", "pythoncom"):
         filename = filename + "%d%d" % sys.version_info[:2]
         suffixes = ('.dll', )
     else:
         suffixes = self._suffixes
     for s in suffixes:
         path = filename + s
         if path in self._files:
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("# found %s in zipfile %s\n" %
                                  (path, self.archive))
             code = self.get_data(path)
             mod = _memimporter.import_module(code, initname, fullname,
                                              path)
             mod.__file__ = "%s\\%s" % (self.archive, path)
             mod.__loader__ = self
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("import %s # loaded from zipfile %s\n" %
                                  (fullname, mod.__file__))
             return mod
     raise zipimport.ZipImportError, "can't find module %s" % fullname
 def load_module(self, fullname):
     if fullname in sys.modules:
         mod = sys.modules[fullname]
         if _memimporter.get_verbose_flag():
             sys.stderr.write(
                 "import %s # previously loaded from zipfile %s\n" %
                 (fullname, self.archive))
         return mod
     _memimporter.set_find_proc(self.locate_dll_image)
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
     # name of initfunction
     if sys.version[0] == "3":
         initname = "PyInit_" + fullname.split(".")[-1]
     else:
         initname = "init" + fullname.split(".")[-1]
     filename = fullname.replace(".", "\\")
     if filename in ("pywintypes", "pythoncom"):
         filename = filename + "%d%d" % sys.version_info[:2]
         suffixes = ('.dll', )
     else:
         suffixes = self._suffixes
     for s in suffixes:
         path = filename + s
         if path in self._files:
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("# found %s in zipfile %s\n" %
                                  (path, self.archive))
             code = self.get_data(path)
             path = path.replace('\\', '/')
             #sys.stderr.write('code=%r, initname=%r, fullname=%r, path=%r\n' % (code[:100], initname, fullname, path))
             mod = _memimporter.import_module(code, initname, fullname,
                                              path)
             mod.__file__ = "%s\\%s" % (self.archive, path)
             mod.__loader__ = self
             sys.modules[fullname] = mod
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("import %s # loaded from zipfile %s\n" %
                                  (fullname, mod.__file__))
             return mod
     raise zipimport.ZipImportError("can't find module %s" % fullname)
 def load_module(self, fullname):
     if sys.modules.has_key(fullname):
         mod = sys.modules[fullname]
         if _memimporter.get_verbose_flag():
             sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
         return mod
     _memimporter.set_find_proc(self.locate_dll_image)
     filename = fullname.replace(".","\\")
     try:
         for s in ('.pyd','.pyc','.py'):
             try:
                 with open(filename+s,'U') as fp:
                     return imp.load_module(fullname,fp,filename+s,(s,'U',1))
             except:
                 continue
     except:
         pass
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
     initname = "init" + fullname.split(".")[-1] # name of initfunction
     if filename in ("pywintypes", "pythoncom"):
         filename = filename + "%d%d" % sys.version_info[:2]
         suffixes = ('.dll',)
     else:
         suffixes = self._suffixes
     for s in suffixes:
         path = filename + s
         if path in self._files:
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
             code = self.get_data(path)
             mod = _memimporter.import_module(code, initname, fullname, path)
             mod.__file__ = "%s\\%s" % (self.archive, path)
             mod.__loader__ = self
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
             return mod
     raise zipimport.ZipImportError, "can't find module %s" % fullname
 def load_module(self, fullname):
     if fullname in sys.modules:
         mod = sys.modules[fullname]
         if _memimporter.get_verbose_flag():
             sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
         return mod
     _memimporter.set_find_proc(self.locate_dll_image)
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
      # name of initfunction
     if sys.version[0] == "3":
         initname = "PyInit_" + fullname.split(".")[-1]
     else:
         initname = "init" + fullname.split(".")[-1]
     filename = fullname.replace(".", "\\")
     if filename in ("pywintypes", "pythoncom"):
         filename = filename + "%d%d" % sys.version_info[:2]
         suffixes = ('.dll',)
     else:
         suffixes = self._suffixes
     for s in suffixes:
         path = filename + s
         if path in self._files:
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
             code = self.get_data(path)
             path = path.replace('\\', '/')
             #sys.stderr.write('code=%r, initname=%r, fullname=%r, path=%r\n' % (code[:100], initname, fullname, path))
             mod = _memimporter.import_module(code, initname, fullname, path)
             mod.__file__ = "%s\\%s" % (self.archive, path)
             mod.__loader__ = self
             sys.modules[fullname] = mod
             if _memimporter.get_verbose_flag():
                 sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
             return mod
     raise zipimport.ZipImportError("can't find module %s" % fullname)
Exemple #15
0
 def load_module(self, fullname):
     if sys.modules.has_key(fullname):
         mod = sys.modules[fullname]
         if DEBUG_ZIPIMPORT:
             sys.stderr.write("import %s # previously loaded from zipfile %s\n" % (fullname, self.archive))
         return mod
     try:
         return zipimport.zipimporter.load_module(self, fullname)
     except zipimport.ZipImportError:
         pass
     initname = "init" + fullname.split(".")[-1] # name of initfunction
     filename = fullname.replace(".", "\\")
     path = filename + '.pyd'
     if path in self._files:
         if DEBUG_ZIPIMPORT:
             sys.stderr.write("# found %s in zipfile %s\n" % (path, self.archive))
         code = self.get_data(path)
         mod = _memimporter.import_module(code, initname, fullname, path)
         mod.__file__ = "%s\\%s" % (self.archive, path)
         mod.__loader__ = self
         if DEBUG_ZIPIMPORT:
             sys.stderr.write("import %s # loaded from zipfile %s\n" % (fullname, mod.__file__))
         return mod
     raise zipimport.ZipImportError, "can't find module %s" % fullname
Exemple #16
0
    def load_module(self, fullname):
        imp.acquire_lock()
        try:
            dprint('loading module {}'.format(fullname))
            if fullname in sys.modules:
                return sys.modules[fullname]

            mod=None
            c=None
            if self.extension=="py":
                mod = imp.new_module(fullname)
                mod.__name__ = fullname
                mod.__file__ = 'pupy://{}'.format(self.path)
                if self.is_pkg:
                    mod.__path__ = [mod.__file__.rsplit('/',1)[0]]
                    mod.__package__ = fullname
                else:
                    mod.__package__ = fullname.rsplit('.', 1)[0]
                code = compile(self.contents, mod.__file__, "exec")
                sys.modules[fullname] = mod
                exec (code, mod.__dict__)

            elif self.extension in ["pyc","pyo"]:
                mod = imp.new_module(fullname)
                mod.__name__ = fullname
                mod.__file__ = 'pupy://{}'.format(self.path)
                if self.is_pkg:
                    mod.__path__ = [mod.__file__.rsplit('/',1)[0]]
                    mod.__package__ = fullname
                else:
                    mod.__package__ = fullname.rsplit('.', 1)[0]
                sys.modules[fullname] = mod
                exec (marshal.loads(self.contents[8:]), mod.__dict__)

            elif self.extension in ("dll", "pyd", "so"):
                initname = "init" + fullname.rsplit(".",1)[-1]
                path = self.fullname.rsplit('.', 1)[0].replace(".",'/') + "." + self.extension
                dprint('Loading {} from memory'.format(fullname))
                dprint('init={} fullname={} path={}'.format(initname, fullname, path))
                mod = _memimporter.import_module(self.contents, initname, fullname, path)
                if mod:
                    mod.__name__=fullname
                    mod.__file__ = 'pupy://{}'.format(self.path)
                    mod.__package__ = fullname.rsplit('.',1)[0]
                    sys.modules[fullname] = mod

            try:
                memtrace(fullname)
            except Exception, e:
                dprint('memtrace failed: {}'.format(e))


        except Exception as e:

            if fullname in sys.modules:
                del sys.modules[fullname]

            import traceback
            exc_type, exc_value, exc_traceback = sys.exc_info()
            sys.stderr.write('Error importing %s : %s'%(fullname, traceback.format_exc()))
            dprint('PupyPackageLoader: '
                       'Error while loading package {} ({}) : {}'.format(
                           fullname, self.path, str(e)))
            if remote_print_error:
                try:
                    remote_print_error("Error loading package {} ({} pkg={}) : {}".format(
                        fullname, self.path, self.is_pkg, str(traceback.format_exc())))
                except:
                    pass

            raise e

        finally:
            self.contents = None
            imp.release_lock()
            gc.collect()

        return sys.modules[fullname]
Exemple #17
0
    def load_module(self, fullname):
        imp.acquire_lock()
        try:
            dprint('loading module {}'.format(fullname))
            if fullname in sys.modules:
                return sys.modules[fullname]

            mod = None
            c = None
            if self.extension == "py":
                mod = imp.new_module(fullname)
                mod.__name__ = fullname
                mod.__file__ = 'pupy://{}'.format(self.path)
                if self.is_pkg:
                    mod.__path__ = [mod.__file__.rsplit('/', 1)[0]]
                    mod.__package__ = fullname
                else:
                    mod.__package__ = fullname.rsplit('.', 1)[0]
                code = compile(self.contents, mod.__file__, "exec")
                sys.modules[fullname] = mod
                exec(code, mod.__dict__)

            elif self.extension in ["pyc", "pyo"]:
                mod = imp.new_module(fullname)
                mod.__name__ = fullname
                mod.__file__ = 'pupy://{}'.format(self.path)
                if self.is_pkg:
                    mod.__path__ = [mod.__file__.rsplit('/', 1)[0]]
                    mod.__package__ = fullname
                else:
                    mod.__package__ = fullname.rsplit('.', 1)[0]
                sys.modules[fullname] = mod
                exec(marshal.loads(self.contents[8:]), mod.__dict__)

            elif self.extension in ("dll", "pyd", "so"):
                initname = "init" + fullname.rsplit(".", 1)[-1]
                path = self.fullname.rsplit('.', 1)[0].replace(
                    ".", '/') + "." + self.extension
                dprint('Loading {} from memory'.format(fullname))
                dprint('init={} fullname={} path={}'.format(
                    initname, fullname, path))
                mod = _memimporter.import_module(self.contents, initname,
                                                 fullname, path)
                if mod:
                    mod.__name__ = fullname
                    mod.__file__ = 'pupy://{}'.format(self.path)
                    mod.__package__ = fullname.rsplit('.', 1)[0]
                    sys.modules[fullname] = mod

            try:
                memtrace(fullname)
            except Exception, e:
                dprint('memtrace failed: {}'.format(e))

        except Exception as e:

            if fullname in sys.modules:
                del sys.modules[fullname]

            import traceback
            exc_type, exc_value, exc_traceback = sys.exc_info()
            sys.stderr.write('Error importing %s : %s' %
                             (fullname, traceback.format_exc()))
            dprint('PupyPackageLoader: '
                   'Error while loading package {} ({}) : {}'.format(
                       fullname, self.path, str(e)))
            if remote_print_error:
                try:
                    remote_print_error(
                        "Error loading package {} ({} pkg={}) : {}".format(
                            fullname, self.path, self.is_pkg,
                            str(traceback.format_exc())))
                except:
                    pass

            raise e

        finally:
            self.contents = None
            imp.release_lock()
            gc.collect()

        return sys.modules[fullname]
Exemple #18
0
 def load_module(self, fullname):
     imp.acquire_lock()
     try:
         #print "loading module %s"%fullname
         if fullname in sys.modules:
             return sys.modules[fullname]
         mod = None
         c = None
         if self.extension == "py":
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = "<memimport>\\%s" % self.path.replace("/", "\\")
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit("\\", 1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname] = mod
             code = compile(self.contents, mod.__file__, "exec")
             exec code in mod.__dict__
         elif self.extension in ["pyc", "pyo"]:
             mod = imp.new_module(fullname)
             mod.__name__ = fullname
             mod.__file__ = "<memimport>\\%s" % self.path.replace("/", "\\")
             mod.__loader__ = self
             if self.is_pkg:
                 mod.__path__ = [mod.__file__.rsplit("\\", 1)[0]]
                 mod.__package__ = fullname
             else:
                 mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname] = mod
             c = marshal.loads(self.contents[8:])
             exec c in mod.__dict__
         elif self.extension in ("dll", "pyd"):
             initname = "init" + fullname.rsplit(".", 1)[-1]
             path = fullname.replace(".", "/") + "." + self.extension
             #print "Loading %s from memory"%fullname
             #print "init:%s, %s.%s"%(initname,fullname,self.extension)
             mod = _memimporter.import_module(self.contents, initname,
                                              fullname, path)
             mod.__name__ = fullname
             mod.__file__ = "<memimport>\\%s" % self.path.replace("/", "\\")
             mod.__loader__ = self
             mod.__package__ = fullname.rsplit('.', 1)[0]
             sys.modules[fullname] = mod
         elif self.extension in ('so', ):
             with tempfile.NamedTemporaryFile(mode='wb') as tmpfile:
                 tmpfile.write(self.contents)
                 tmpfile.flush()
                 initname = fullname.rsplit(".", 1)[-1]
                 sys.modules[fullname] = imp.load_dynamic(
                     initname, tmpfile.name)
     except Exception as e:
         if fullname in sys.modules:
             del sys.modules[fullname]
         import traceback
         print "PupyPackageLoader: Error while loading package %s (%s) : %s" % (
             fullname, self.extension, str(e))
         raise e
     finally:
         imp.release_lock()
     mod = sys.modules[
         fullname]  # reread the module in case it changed itself
     return mod