Exemple #1
0
    def load_module(self, fullname, path=None):
        imp_lock()
        module = None
        try:
            try:
                if sys.version_info[0] == 2:
                    module = sys.modules.get(fullname)
                    if module is None:
                        for ext, ext_tuple in EXTENSION_SUFFIXES.iteritems():
                            filename = fullname + ext
                            if filename in self._file_cache:
                                break

                        filename = pyi_os_path.os_path_join(
                            SYS_PREFIX, filename)
                        with open(filename, 'rb') as fp:
                            module = imp.load_module(fullname, fp, filename,
                                                     ext_tuple)
                        if hasattr(module, '__setattr__'):
                            module.__file__ = filename
                        else:
                            module.__dict__['__file__'] = filename
                else:
                    module = sys.modules.get(fullname)
                    if module is None:
                        for ext in EXTENSION_SUFFIXES:
                            filename = pyi_os_path.os_path_join(
                                SYS_PREFIX, fullname + ext)
                            try:
                                with open(filename):
                                    pass
                            except IOError:
                                continue

                            loader = EXTENSION_LOADER(fullname, filename)
                            try:
                                module = loader.load_module(fullname)
                            except ImportError as e:
                                raise ImportError('%s: %s' % (e, fullname))

            except Exception:
                if fullname in sys.modules:
                    sys.modules.pop(fullname)
                raise

        finally:
            imp_unlock()

        return module
 def get_filename(self, fullname):
     """
     This method should return the value that __file__ would be set to
     if the named module was loaded. If the module is not found, then
     ImportError should be raised.
     """
     # Then, append the appropriate suffix (__init__.pyc for a package, or just .pyc for a module).
     # Method is_package() will raise ImportError if module not found.
     if self.is_package(fullname):
         filename = pyi_os_path.os_path_join(pyi_os_path.os_path_join(SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep)), '__init__.pyc')
     else:
         filename = pyi_os_path.os_path_join(SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep) + '.pyc')
     return filename
Exemple #3
0
 def get_filename(self, fullname):
     """
     This method should return the value that __file__ would be set to
     if the named module was loaded. If the module is not found, then
     ImportError should be raised.
     """
     if self.is_package(fullname):
         filename = pyi_os_path.os_path_join(
             pyi_os_path.os_path_join(
                 SYS_PREFIX, fullname.replace('.', pyi_os_path.os_sep)),
             '__init__.pyc')
     else:
         filename = pyi_os_path.os_path_join(
             SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep) + '.pyc')
     return filename
Exemple #4
0
 def scan_directory(self, dir):
     self._checked_path.add(dir)
     try:
         files = pyi_os_path.os_listdir(dir)
         for file in files:
             self._file_cache[file] = pyi_os_path.os_path_join(dir, file)
     except:
         pass
Exemple #5
0
 def get_filename(self, fullname):
     """
     This method should return the value that __file__ would be set to
     if the named module was loaded. If the module is not found, then
     ImportError should be raised.
     """
     # Then, append the appropriate suffix (__init__.pyc for a package, or just .pyc for a module).
     # Method is_package() will raise ImportError if module not found.
     if self.is_package(fullname):
         filename = pyi_os_path.os_path_join(
             pyi_os_path.os_path_join(
                 SYS_PREFIX, fullname.replace('.', pyi_os_path.os_sep)),
             '__init__.pyc')
     else:
         filename = pyi_os_path.os_path_join(
             SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep) + '.pyc')
     return filename
 def get_filename(self, fullname):
     """
     This method should return the value that __file__ would be set to
     if the named module was loaded. If the module is not found, then
     ImportError should be raised.
     """
     # The absolute absolute path to the executable is taken from
     # sys.prefix. In onefile mode it points to the temp directory where
     # files are unpacked by PyInstaller. Then, append the appropriate
     # suffix (__init__.pyc for a package, or just .pyc for a module).
     # Method is_package() will raise ImportError if module not found.
     if self.is_package(fullname):
         filename = pyi_os_path.os_path_join(pyi_os_path.os_path_join(SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep)), '__init__.pyc')
     else:
         filename = pyi_os_path.os_path_join(SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep) + '.pyc')
     return filename
 def get_filename(self, fullname):
     """
     This method should return the value that __file__ would be set to
     if the named module was loaded. If the module is not found, then
     ImportError should be raised.
     """
     # The absolute absolute path to the executable is taken from
     # sys.prefix. In onefile mode it points to the temp directory where
     # files are unpacked by PyInstaller. Then, append the appropriate
     # suffix (__init__.pyc for a package, or just .pyc for a module).
     # Method is_package() will raise ImportError if module not found.
     if self.is_package(fullname):
         filename = pyi_os_path.os_path_join(pyi_os_path.os_path_join(SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep)), '__init__.pyc')
     else:
         filename = pyi_os_path.os_path_join(SYS_PREFIX,
             fullname.replace('.', pyi_os_path.os_sep) + '.pyc')
     return filename
Exemple #8
0
 def get_filename(self, fullname):
     """
     This method should return the value that __file__ would be set to
     if the named module was loaded. If the module is not found, then
     ImportError should be raised.
     """
     for ext in EXTENSION_SUFFIXES:
         if fullname + ext in self._file_cache:
             return pyi_os_path.os_path_join(SYS_PREFIX, fullname + ext)
     # ImportError should be raised if module not found.
     raise ImportError('No module named ' + fullname)
 def get_filename(self, fullname):
     """
     This method should return the value that __file__ would be set to
     if the named module was loaded. If the module is not found, then
     ImportError should be raised.
     """
     for ext in EXTENSION_SUFFIXES:
         if fullname + ext in self._file_cache:
             return pyi_os_path.os_path_join(SYS_PREFIX, fullname + ext)
     # ImportError should be raised if module not found.
     raise ImportError('No module named ' + fullname)
    def get_source(self, fullname):
        """
        Method should return the source code for the module as a string.
        But frozen modules does not contain source code.

        Return None.
        """
        if fullname in self.toc:
            # Try loading .py file from the filesystem
            filename = pyi_os_path.os_path_join(
                SYS_PREFIX,
                fullname.replace('.', pyi_os_path.os_sep) + '.py')
            try:
                with open(filename, 'r') as fp:
                    return fp.read()
            except FileNotFoundError:
                pass
            return None
        else:
            # ImportError should be raised if module not found.
            raise ImportError('No module named ' + fullname)
Exemple #11
0
    def get_source(self, fullname):
        """
        Method should return the source code for the module as a string.
        But frozen modules does not contain source code.

        Return None, unless the corresponding source file was explicitly collected to the filesystem.
        """
        if fullname in self.toc:
            # Try loading the .py file from the filesystem (only for collected modules)
            if self.is_package(fullname):
                fullname += '.__init__'
            filename = pyi_os_path.os_path_join(
                SYS_PREFIX,
                fullname.replace('.', pyi_os_path.os_sep) + '.py')
            try:
                with open(filename, 'r') as fp:
                    return fp.read()
            except FileNotFoundError:
                pass
            return None
        else:
            # ImportError should be raised if module not found.
            raise ImportError('No module named ' + fullname)
Exemple #12
0
    def load_module(self, fullname, path=None):
        # Deprecated in Python 3.4, see PEP-451
        imp_lock()

        module = None

        try:
            if sys.version_info[0] == 2:
                # Python 2 implementation - TODO drop or improve it. 'imp' module is no longer built-in.
                # PEP302 If there is an existing module object named 'fullname'
                # in sys.modules, the loader must use that existing module.
                module = sys.modules.get(fullname)

                if module is None:
                    # Need to search for the filename again, since to
                    # be thread-safe we can't store it in find_module().
                    for ext, ext_tuple in EXTENSION_SUFFIXES.iteritems():
                        filename = fullname + ext
                        if filename in self._file_cache:
                            break
                    filename = pyi_os_path.os_path_join(SYS_PREFIX, filename)
                    with open(filename, 'rb') as fp:
                        module = imp.load_module(fullname, fp, filename,
                                                 ext_tuple)
                    # Set __file__ attribute.
                    if hasattr(module, '__setattr__'):
                        module.__file__ = filename
                    else:
                        # Some modules (eg: Python for .NET) have no __setattr__
                        # and dict entry have to be set.
                        module.__dict__['__file__'] = filename
            else:
                # PEP302 If there is an existing module object named 'fullname'
                # in sys.modules, the loader must use that existing module.
                module = sys.modules.get(fullname)
                if module is None:
                    # Python 3 implementation.
                    for ext in EXTENSION_SUFFIXES:
                        filename = pyi_os_path.os_path_join(
                            SYS_PREFIX, fullname + ext)
                        # Test if a file exists.
                        # Cannot use os.path.exists. Use workaround with function open().
                        # No exception means that a file exists.
                        try:
                            with open(filename):
                                pass
                        except IOError:
                            # Continue trying new suffix.
                            continue
                        # Load module.
                        loader = EXTENSION_LOADER(fullname, filename)
                        try:
                            module = loader.load_module(fullname)
                        except ImportError as e:
                            raise ImportError('%s: %s' % (e, fullname))

        except Exception:
            # Remove 'fullname' from sys.modules if it was appended there.
            if fullname in sys.modules:
                sys.modules.pop(fullname)
            raise  # Raise the same exception again.

        finally:
            # Release the interpreter's import lock.
            imp_unlock()

        return module
Exemple #13
0
    def load_module(self, fullname, real_fullname=None):
        """
        PEP-302 loader.load_module() method for the ``sys.meta_path`` hook.

        Return the loaded module (instance of imp_new_module()) or raises
        an exception, preferably ImportError if an existing exception
        is not being propagated.

        When called from FrozenPackageImporter, `real_fullname` is the name of the
        module as it is stored in the archive. This module will be loaded and installed
        into sys.modules using `fullname` as its name
        """
        # Acquire the interpreter's import lock.
        imp_lock()
        module = None
        if real_fullname is None:
            real_fullname = fullname
        try:
            # PEP302 If there is an existing module object named 'fullname'
            # in sys.modules, the loader must use that existing module.
            module = sys.modules.get(fullname)

            # Module not in sys.modules - load it and it to sys.modules.
            if module is None:
                # Load code object from the bundled ZIP archive.
                is_pkg, bytecode = self._pyz_archive.extract(real_fullname)
                # Create new empty 'module' object.
                module = imp_new_module(fullname)

                # TODO Replace bytecode.co_filename by something more meaningful:
                # e.g. /absolute/path/frozen_executable/path/to/module/module_name.pyc
                # Paths from developer machine are masked.

                ### Set __file__ attribute of a module relative to the executable
                # so that data files can be found. The absolute absolute path
                # to the executable is taken from sys.prefix. In onefile mode it
                # points to the temp directory where files are unpacked by PyInstaller.
                # Then, append the appropriate suffix (__init__.pyc for a package, or just .pyc for a module).
                if is_pkg:
                    module.__file__ = pyi_os_path.os_path_join(
                        pyi_os_path.os_path_join(
                            SYS_PREFIX,
                            fullname.replace('.', pyi_os_path.os_sep)),
                        '__init__.pyc')
                else:
                    module.__file__ = pyi_os_path.os_path_join(
                        SYS_PREFIX,
                        fullname.replace('.', pyi_os_path.os_sep) + '.pyc')

                ### Set __path__  if 'fullname' is a package.
                # Python has modules and packages. A Python package is container
                # for several modules or packages.
                if is_pkg:

                    # If a module has a __path__ attribute, the import mechanism
                    # will treat it as a package.
                    #
                    # Since PYTHONHOME is set in bootloader, 'sys.prefix' points to the
                    # correct path where PyInstaller should find bundled dynamic
                    # libraries. In one-file mode it points to the tmp directory where
                    # bundled files are extracted at execution time.
                    #
                    # __path__ cannot be empty list because 'wx' module prepends something to it.
                    # It cannot contain value 'sys.prefix' because 'xml.etree.cElementTree' fails
                    # Otherwise.
                    #
                    # Set __path__ to point to 'sys.prefix/package/subpackage'.
                    module.__path__ = [
                        pyi_os_path.os_path_dirname(module.__file__)
                    ]

                ### Set __loader__
                # The attribute __loader__ improves support for module 'pkg_resources' and
                # with the frozen apps the following functions are working:
                # pkg_resources.resource_string(), pkg_resources.resource_stream().
                module.__loader__ = self

                ### Set __package__
                # Accoring to PEP302 this attribute must be set.
                # When it is present, relative imports will be based on this
                # attribute rather than the module __name__ attribute.
                # More details can be found in PEP366.
                # For ordinary modules this is set like:
                #     'aa.bb.cc.dd'  ->  'aa.bb.cc'
                if is_pkg:
                    module.__package__ = fullname
                else:
                    module.__package__ = fullname.rsplit('.', 1)[0]

                ### Set __spec__ for Python 3.4+
                # In Python 3.4 was introduced module attribute __spec__ to
                # consolidate all module attributes.
                if sys.version_info[0:2] > (3, 3):
                    module.__spec__ = _frozen_importlib.ModuleSpec(
                        real_fullname, self, is_package=is_pkg)

                ### Add module object to sys.modules dictionary.
                # Module object must be in sys.modules before the loader
                # executes the module code. This is crucial because the module
                # code may (directly or indirectly) import itself; adding it
                # to sys.modules beforehand prevents unbounded recursion in the
                # worst case and multiple loading in the best.
                sys.modules[fullname] = module

                # Run the module code.
                exec(bytecode, module.__dict__)
                # Reread the module from sys.modules in case it's changed itself
                module = sys.modules[fullname]

        except Exception:
            # Remove 'fullname' from sys.modules if it was appended there.
            if fullname in sys.modules:
                sys.modules.pop(fullname)
            # TODO Do we need to raise different types of Exceptions for better debugging?
            # PEP302 requires to raise ImportError exception.
            #raise ImportError("Can't load frozen module: %s" % fullname)

            raise

        finally:
            # Release the interpreter's import lock.
            imp_unlock()

        # Module returned only in case of no exception.
        return module
    def load_module(self, fullname, path=None):
        imp_lock()

        module = None

        try:
            if sys.version_info[0] == 2:
                # Python 2 implementation - TODO drop or improve it. 'imp' module is no longer built-in.
                # PEP302 If there is an existing module object named 'fullname'
                # in sys.modules, the loader must use that existing module.
                module = sys.modules.get(fullname)

                if module is None:
                    # Need to search for the filename again, since to
                    # be thread-safe we can't store it in find_module().
                    for ext, ext_tuple in EXTENSION_SUFFIXES.iteritems():
                        filename = fullname + ext
                        if filename in self._file_cache:
                            break
                    filename = pyi_os_path.os_path_join(SYS_PREFIX, filename)
                    fp = open(filename, 'rb')
                    module = imp.load_module(fullname, fp, filename, ext_tuple)
                    # Set __file__ attribute.
                    if hasattr(module, '__setattr__'):
                        module.__file__ = filename
                    else:
                        # Some modules (eg: Python for .NET) have no __setattr__
                        # and dict entry have to be set.
                        module.__dict__['__file__'] = filename
            else:
                # PEP302 If there is an existing module object named 'fullname'
                # in sys.modules, the loader must use that existing module.
                module = sys.modules.get(fullname)
                if module is None:
                    # Python 3 implementation.
                    for ext in EXTENSION_SUFFIXES:
                        filename = pyi_os_path.os_path_join(SYS_PREFIX, fullname + ext)
                        # Test if a file exists.
                        # Cannot use os.path.exists. Use workaround with function open().
                        # No exception means that a file exists.
                        try:
                            with open(filename):
                                pass
                        except IOError:
                            # Continue trying new suffix.
                            continue
                        # Load module.
                        loader = EXTENSION_LOADER(fullname, filename)
                        module = loader.load_module(fullname)

        except Exception:
            # Remove 'fullname' from sys.modules if it was appended there.
            if fullname in sys.modules:
                sys.modules.pop(fullname)
            raise  # Raise the same exception again.

        finally:
            # Release the interpreter's import lock.
            imp_unlock()

        return module
    def load_module(self, fullname, real_fullname=None):
        """
        PEP-302 loader.load_module() method for the ``sys.meta_path`` hook.

        Return the loaded module (instance of imp_new_module()) or raises
        an exception, preferably ImportError if an existing exception
        is not being propagated.

        When called from FrozenPackageImporter, `real_fullname` is the name of the
        module as it is stored in the archive. This module will be loaded and installed
        into sys.modules using `fullname` as its name
        """
        # Acquire the interpreter's import lock.
        imp_lock()
        module = None
        if real_fullname is None:
            real_fullname=fullname
        try:
            # PEP302 If there is an existing module object named 'fullname'
            # in sys.modules, the loader must use that existing module.
            module = sys.modules.get(fullname)

            # Module not in sys.modules - load it and it to sys.modules.
            if module is None:
                # Load code object from the bundled ZIP archive.
                is_pkg, bytecode = self._pyz_archive.extract(real_fullname)
                # Create new empty 'module' object.
                module = imp_new_module(fullname)

                # TODO Replace bytecode.co_filename by something more meaningful:
                # e.g. /absolute/path/frozen_executable/path/to/module/module_name.pyc
                # Paths from developer machine are masked.

                ### Set __file__ attribute of a module relative to the executable
                # so that data files can be found. The absolute absolute path
                # to the executable is taken from sys.prefix. In onefile mode it
                # points to the temp directory where files are unpacked by PyInstaller.
                # Then, append the appropriate suffix (__init__.pyc for a package, or just .pyc for a module).
                if is_pkg:
                    module.__file__ = pyi_os_path.os_path_join(pyi_os_path.os_path_join(SYS_PREFIX,
                        fullname.replace('.', pyi_os_path.os_sep)), '__init__.pyc')
                else:
                    module.__file__ = pyi_os_path.os_path_join(SYS_PREFIX,
                        fullname.replace('.', pyi_os_path.os_sep) + '.pyc')

                ### Set __path__  if 'fullname' is a package.
                # Python has modules and packages. A Python package is container
                # for several modules or packages.
                if is_pkg:

                    # If a module has a __path__ attribute, the import mechanism
                    # will treat it as a package.
                    #
                    # Since PYTHONHOME is set in bootloader, 'sys.prefix' points to the
                    # correct path where PyInstaller should find bundled dynamic
                    # libraries. In one-file mode it points to the tmp directory where
                    # bundled files are extracted at execution time.
                    #
                    # __path__ cannot be empty list because 'wx' module prepends something to it.
                    # It cannot contain value 'sys.prefix' because 'xml.etree.cElementTree' fails
                    # Otherwise.
                    #
                    # Set __path__ to point to 'sys.prefix/package/subpackage'.
                    module.__path__ = [pyi_os_path.os_path_dirname(module.__file__)]

                ### Set __loader__
                # The attribute __loader__ improves support for module 'pkg_resources' and
                # with the frozen apps the following functions are working:
                # pkg_resources.resource_string(), pkg_resources.resource_stream().
                module.__loader__ = self

                ### Set __package__
                # Accoring to PEP302 this attribute must be set.
                # When it is present, relative imports will be based on this
                # attribute rather than the module __name__ attribute.
                # More details can be found in PEP366.
                # For ordinary modules this is set like:
                #     'aa.bb.cc.dd'  ->  'aa.bb.cc'
                if is_pkg:
                    module.__package__ = fullname
                else:
                    module.__package__ = fullname.rsplit('.', 1)[0]

                ### Set __spec__ for Python 3.4+
                # In Python 3.4 was introduced module attribute __spec__ to
                # consolidate all module attributes.
                if sys.version_info[0:2] > (3, 3):
                    module.__spec__ = _frozen_importlib.ModuleSpec(
                        real_fullname, self, is_package=is_pkg)

                ### Add module object to sys.modules dictionary.
                # Module object must be in sys.modules before the loader
                # executes the module code. This is crucial because the module
                # code may (directly or indirectly) import itself; adding it
                # to sys.modules beforehand prevents unbounded recursion in the
                # worst case and multiple loading in the best.
                sys.modules[fullname] = module

                # Run the module code.
                exec(bytecode, module.__dict__)
                # Reread the module from sys.modules in case it's changed itself
                module = sys.modules[fullname]

        except Exception:
            # Remove 'fullname' from sys.modules if it was appended there.
            if fullname in sys.modules:
                sys.modules.pop(fullname)
            # TODO Do we need to raise different types of Exceptions for better debugging?
            # PEP302 requires to raise ImportError exception.
            #raise ImportError("Can't load frozen module: %s" % fullname)

            raise

        finally:
            # Release the interpreter's import lock.
            imp_unlock()


        # Module returned only in case of no exception.
        return module