Python imp.load_compiled() Examples

The following are 30 code examples of imp.load_compiled(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imp , or try the search function .
Example #1
Source File: ihooks.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #2
Source File: ihooks.py    From RevitBatchProcessor with GNU General Public License v3.0 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #3
Source File: ihooks.py    From PokemonGo-DesktopMap with MIT License 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #4
Source File: ihooks.py    From oss-ftp with MIT License 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #5
Source File: ihooks.py    From unity-python with MIT License 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #6
Source File: ihooks.py    From BinderFilter with MIT License 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #7
Source File: ihooks.py    From canape with GNU General Public License v3.0 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #8
Source File: util.py    From mqttwarn with Eclipse Public License 2.0 6 votes vote down vote up
def load_functions(filepath=None):
    if not filepath:
        return None

    if not os.path.isfile(filepath):
        raise IOError("'{}' not found".format(filepath))

    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])

    if file_ext.lower() == '.py':
        py_mod = imp.load_source(mod_name, filepath)

    elif file_ext.lower() == '.pyc':
        py_mod = imp.load_compiled(mod_name, filepath)

    else:
        raise ValueError("'{}' does not have the .py or .pyc extension".format(filepath))

    return py_mod 
Example #9
Source File: ihooks.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #10
Source File: ihooks.py    From meddle with MIT License 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #11
Source File: ihooks.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #12
Source File: ihooks.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def load_module(self, name, stuff):
        file, filename, info = stuff
        (suff, mode, type) = info
        try:
            if type == BUILTIN_MODULE:
                return self.hooks.init_builtin(name)
            if type == FROZEN_MODULE:
                return self.hooks.init_frozen(name)
            if type == C_EXTENSION:
                m = self.hooks.load_dynamic(name, filename, file)
            elif type == PY_SOURCE:
                m = self.hooks.load_source(name, filename, file)
            elif type == PY_COMPILED:
                m = self.hooks.load_compiled(name, filename, file)
            elif type == PKG_DIRECTORY:
                m = self.hooks.load_package(name, filename, file)
            else:
                raise ImportError, "Unrecognized module type (%r) for %s" % \
                      (type, name)
        finally:
            if file: file.close()
        m.__file__ = filename
        return m 
Example #13
Source File: ihooks.py    From unity-python with MIT License 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #14
Source File: test_import_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_load_compiled(self):
        compiled = os.__file__
        if compiled.endswith('.py'):
            compiled = compiled[:-3] + COMPILED_SUFFIX

        os.__doc__ = 'foo'
        self.assertEqual(os, imp.load_compiled("os", compiled))
        self.assertFalse(os.__doc__ == 'foo')
        with open(compiled, 'rb') as fp:
            os.__doc__ = 'foo'
            self.assertEqual(os, imp.load_compiled("os", compiled, fp))
            self.assertFalse(os.__doc__ == 'foo') 
Example #15
Source File: test_chdir.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_imp_load_compiled(self):
        __import__(self.mod_name)
        self.assertTrue(os.path.exists(self.bytecode))
        basename = os.path.basename(self.bytecode)
        mod = imp.load_compiled(self.mod_name, basename)
        self.assertEqual(mod.__file__, basename) 
Example #16
Source File: ihooks.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #17
Source File: ihooks.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #18
Source File: ihooks.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #19
Source File: ihooks.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #20
Source File: test_import_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_load_compiled(self):
        compiled = os.__file__
        if compiled.endswith('.py'):
            compiled = compiled[:-3] + COMPILED_SUFFIX

        os.__doc__ = 'foo'
        self.assertEqual(os, imp.load_compiled("os", compiled))
        self.assertFalse(os.__doc__ == 'foo')
        with open(compiled, 'rb') as fp:
            os.__doc__ = 'foo'
            self.assertEqual(os, imp.load_compiled("os", compiled, fp))
            self.assertFalse(os.__doc__ == 'foo') 
Example #21
Source File: compat.py    From android_universal with MIT License 5 votes vote down vote up
def load_module_pyc(module_id, path):  # noqa
        with open(path, 'rb') as fp:
            mod = imp.load_compiled(module_id, path, fp)
            # no source encoding here
            del sys.modules[module_id]
            return mod 
Example #22
Source File: ihooks.py    From canape with GNU General Public License v3.0 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #23
Source File: ihooks.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #24
Source File: test_import_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_load_compiled(self):
        compiled = os.__file__
        if compiled.endswith('.py'):
            compiled = compiled[:-3] + COMPILED_SUFFIX

        os.__doc__ = 'foo'
        self.assertEqual(os, imp.load_compiled("os", compiled))
        self.assertFalse(os.__doc__ == 'foo')
        with open(compiled, 'rb') as fp:
            os.__doc__ = 'foo'
            self.assertEqual(os, imp.load_compiled("os", compiled, fp))
            self.assertFalse(os.__doc__ == 'foo') 
Example #25
Source File: test_chdir.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_imp_load_compiled(self):
        __import__(self.mod_name)
        self.assertTrue(os.path.exists(self.bytecode))
        basename = os.path.basename(self.bytecode)
        mod = imp.load_compiled(self.mod_name, basename)
        self.assertEqual(mod.__file__, basename) 
Example #26
Source File: ihooks.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file) 
Example #27
Source File: report_xml.py    From LibrERP with GNU Affero General Public License v3.0 5 votes vote down vote up
def load_from_file(self, path, dbname, key):
        class_inst = None
        expected_class = 'Parser'

        try:
            ad = os.path.abspath(os.path.join(tools.ustr(config['root_path']), u'addons'))
            mod_path_list = map(lambda m: os.path.abspath(tools.ustr(m.strip())), config['addons_path'].split(','))
            mod_path_list.append(ad)
            mod_path_list = list(set(mod_path_list))

            for mod_path in mod_path_list:
                if os.path.lexists(mod_path + os.path.sep + path.split(os.path.sep)[0]):
                    filepath = mod_path + os.path.sep + path
                    filepath = os.path.normpath(filepath)
                    sys.path.append(os.path.dirname(filepath))
                    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
                    mod_name = '%s_%s_%s' % (dbname, mod_name, key)

                    if file_ext.lower() == '.py':
                        py_mod = imp.load_source(mod_name, filepath)

                    elif file_ext.lower() == '.pyc':
                        py_mod = imp.load_compiled(mod_name, filepath)

                    if expected_class in dir(py_mod):
                        class_inst = py_mod.Parser
                    return class_inst
                elif os.path.lexists(mod_path + os.path.sep + path.split(os.path.sep)[0] + '.zip'):
                    zimp = zipimport.zipimporter(mod_path + os.path.sep + path.split(os.path.sep)[0] + '.zip')
                    return zimp.load_module(path.split(os.path.sep)[0]).parser.Parser
        except SyntaxError, e:
            raise osv.except_osv(_('Syntax Error !'), e) 
Example #28
Source File: compat.py    From jbox with MIT License 5 votes vote down vote up
def load_module_pyc(module_id, path):
        with open(path, 'rb') as fp:
            mod = imp.load_compiled(module_id, path, fp)
            # no source encoding here
            return mod 
Example #29
Source File: rmDatabaseUpdate.py    From rainmachine-developer-resources with GNU General Public License v3.0 5 votes vote down vote up
def __doUpdate(fromVersion, toVersion):

        updateScriptsDir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "dbUpdateScripts"))
        if not os.path.exists(updateScriptsDir):
            return False

        for version in range(fromVersion + 1, toVersion + 1):
            moduleName = "updateV%d" % (version)
            scriptName = moduleName + ".py"
            scriptPath = os.path.join(updateScriptsDir, scriptName)
            compiled = False

            if not os.path.exists(scriptPath):
                scriptName = moduleName + ".pyc"
                scriptPath = os.path.join(updateScriptsDir, scriptName)
                compiled = True

            log.info("... applying database upgrade: %s" % scriptPath)

            if not os.path.exists(scriptPath):
                return False

            success = False
            try:
                if compiled:
                    module = imp.load_compiled(moduleName, scriptPath)
                else:
                    module = imp.load_source(moduleName, scriptPath)

                try:
                    success = module.performUpdate()
                except Exception, e:
                    log.error(e)

                del sys.modules[moduleName]

            except Exception, e:
                log.error(e)
                return False 
Example #30
Source File: ihooks.py    From meddle with MIT License 5 votes vote down vote up
def load_compiled(self, name, filename, file=None):
        return imp.load_compiled(name, filename, file)