Python imp.get_magic() Examples
The following are 30
code examples of imp.get_magic().
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: pydoc.py From unity-python with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #2
Source File: pydoc.py From oss-ftp with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #3
Source File: pydoc.py From medicare-demo with Apache License 2.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #4
Source File: pydoc.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #5
Source File: pydoc.py From Computable with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #6
Source File: pydoc.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #7
Source File: pydoc.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #8
Source File: pydoc.py From BinderFilter with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #9
Source File: main.py From mochi with MIT License | 6 votes |
def output_pyc(code, buffer=sys.stdout.buffer): import marshal import struct import time if GE_PYTHON_34: from importlib.util import MAGIC_NUMBER else: import imp MAGIC_NUMBER = imp.get_magic() buffer.write(MAGIC_NUMBER) timestamp = struct.pack('i', int(time.time())) buffer.write(timestamp) if GE_PYTHON_33: buffer.write(b'0' * 4) marshal.dump(code, buffer) buffer.flush()
Example #10
Source File: factory_module.py From marsnake with GNU General Public License v3.0 | 6 votes |
def load_compiled(self, name, filename, code, ispackage = False): #if data[:4] != imp.get_magic(): # raise ImportError('Bad magic number in %s' % filename) # Ignore timestamp in data[4:8] #code = marshal.loads(data[8:]) imp.acquire_lock() # Required in threaded applications try: mod = imp.new_module(name) sys.modules[name] = mod # To handle circular and submodule imports # it should come before exec. try: mod.__file__ = filename # Is not so important. # For package you have to set mod.__path__ here. # Here I handle simple cases only. if ispackage: mod.__path__ = [name.replace('.', '/')] exec(code in mod.__dict__) except: del sys.modules[name] raise finally: imp.release_lock() return mod
Example #11
Source File: loadMochaImport.py From NukeToolSet with MIT License | 6 votes |
def load(): supportedMagicNumbers = ['03f30d0a', 'd1f20d0a'] try: magicNumberOfThisVersion = imp.get_magic().encode('hex') if magicNumberOfThisVersion in supportedMagicNumbers: pathToThisVersion = "python/mamoworld/mochaImportPlus/version_" + magicNumberOfThisVersion nuke.pluginAddPath(pathToThisVersion) else: raise Exception( "MochaImport+ for NUKE: unsupported version of Python:" + sys.version + "(magic number:" + magicNumberOfThisVersion + ")") except Exception as e: import traceback nuke.tprint(traceback.format_exc()) # Just in case msg = 'ERROR: %s' % e if nuke.GUI: nuke.message(msg) else: nuke.tprint(msg)
Example #12
Source File: pydoc.py From RevitBatchProcessor with GNU General Public License v3.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #13
Source File: mypydoc.py From azure-linux-extensions with Apache License 2.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #14
Source File: pydoc.py From ironpython2 with Apache License 2.0 | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #15
Source File: pydoc.py From PokemonGo-DesktopMap with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #16
Source File: pydoc.py From meddle with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() file = open(path, 'r') if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.close() filename = os.path.basename(path) name, ext = os.path.splitext(filename) file = open(path, 'r') try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) file.close() return module
Example #17
Source File: pydoc.py From jawfish with MIT License | 6 votes |
def importfile(path): """Import a Python source file or compiled file given its path.""" magic = imp.get_magic() with open(path, 'rb') as file: if file.read(len(magic)) == magic: kind = imp.PY_COMPILED else: kind = imp.PY_SOURCE file.seek(0) filename = os.path.basename(path) name, ext = os.path.splitext(filename) try: module = imp.load_module(name, file, path, (ext, 'r', kind)) except: raise ErrorDuringImport(path, sys.exc_info()) return module
Example #18
Source File: compileapp.py From termite-visualizations with BSD 3-Clause "New" or "Revised" License | 5 votes |
def read_pyc(filename): """ Read the code inside a bytecode compiled file if the MAGIC number is compatible :returns: a code object """ data = read_file(filename, 'rb') if not is_gae and data[:4] != imp.get_magic(): raise SystemError('compiled code is incompatible') return marshal.loads(data[8:])
Example #19
Source File: modulefinder.py From PokemonGo-DesktopMap with MIT License | 5 votes |
def load_module(self, fqname, fp, pathname, file_info): suffix, mode, type = file_info self.msgin(2, "load_module", fqname, fp and "fp", pathname) if type == imp.PKG_DIRECTORY: m = self.load_package(fqname, pathname) self.msgout(2, "load_module ->", m) return m if type == imp.PY_SOURCE: co = compile(fp.read()+'\n', pathname, 'exec') elif type == imp.PY_COMPILED: if fp.read(4) != imp.get_magic(): self.msgout(2, "raise ImportError: Bad magic number", pathname) raise ImportError, "Bad magic number in %s" % pathname fp.read(4) co = marshal.load(fp) else: co = None m = self.add_module(fqname) m.__file__ = pathname if co: if self.replace_paths: co = self.replace_paths_in_code(co) m.__code__ = co self.scan_code(co, m) self.msgout(2, "load_module ->", m) return m
Example #20
Source File: pkgutil.py From RevitBatchProcessor with GNU General Public License v3.0 | 5 votes |
def read_code(stream): # This helper is needed in order for the PEP 302 emulation to # correctly handle compiled files import marshal magic = stream.read(4) if magic != imp.get_magic(): return None stream.read(4) # Skip timestamp return marshal.load(stream)
Example #21
Source File: modulefinder.py From RevitBatchProcessor with GNU General Public License v3.0 | 5 votes |
def load_module(self, fqname, fp, pathname, file_info): suffix, mode, type = file_info self.msgin(2, "load_module", fqname, fp and "fp", pathname) if type == imp.PKG_DIRECTORY: m = self.load_package(fqname, pathname) self.msgout(2, "load_module ->", m) return m if type == imp.PY_SOURCE: co = compile(fp.read()+'\n', pathname, 'exec') elif type == imp.PY_COMPILED: if fp.read(4) != imp.get_magic(): self.msgout(2, "raise ImportError: Bad magic number", pathname) raise ImportError, "Bad magic number in %s" % pathname fp.read(4) co = marshal.load(fp) else: co = None m = self.add_module(fqname) m.__file__ = pathname if co: if self.replace_paths: co = self.replace_paths_in_code(co) m.__code__ = co self.scan_code(co, m) self.msgout(2, "load_module ->", m) return m
Example #22
Source File: builder.py From scalyr-agent-2 with Apache License 2.0 | 5 votes |
def __init__(self, srcName): self._srcName = srcName self.__magic = imp.get_magic() self.__sfx = {} self.__inited = None for sfx, mode, typ in imp.get_suffixes(): if typ not in self.__sfx: self.__sfx[typ] = [] self.__sfx[typ].append((sfx, len(sfx), mode)) debug.logger & debug.flagBld and debug.logger('trying %s' % self)
Example #23
Source File: pkgutil.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def read_code(stream): # This helper is needed in order for the PEP 302 emulation to # correctly handle compiled files import marshal magic = stream.read(4) if magic != imp.get_magic(): return None stream.read(4) # Skip timestamp return marshal.load(stream)
Example #24
Source File: pyfile.py From scalyr-agent-2 with Apache License 2.0 | 5 votes |
def fileExists(self, mibname, mtime, rebuild=False): if rebuild: debug.logger & debug.flagSearcher and debug.logger('pretend %s is very old' % mibname) return mibname = decode(mibname) pyfile = os.path.join(self._path, mibname) for fmt in imp.PY_COMPILED, imp.PY_SOURCE: for pySfx, pyMode in self.suffixes[fmt]: f = pyfile + pySfx if not os.path.exists(f) or not os.path.isfile(f): debug.logger & debug.flagSearcher and debug.logger('%s not present or not a file' % f) continue if fmt == imp.PY_COMPILED: try: pyData = open(f, pyMode).read(8) except IOError: raise error.PySmiSearcherError('failure opening compiled file %s: %s' % (f, sys.exc_info()[1]), searcher=self) if pyData[:4] == imp.get_magic(): pyData = pyData[4:] pyTime = struct.unpack('<L', pyData[:4])[0] debug.logger & debug.flagSearcher and debug.logger('found %s, mtime %s' % (f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(pyTime)))) if pyTime >= mtime: raise error.PySmiFileNotModifiedError() else: raise error.PySmiFileNotFoundError('older file %s exists %s' % mibname, searcher=self) else: debug.logger & debug.flagSearcher and debug.logger('bad magic in %s' % f) continue else: try: pyTime = os.stat(f)[8] except OSError: raise error.PySmiSearcherError('failure opening compiled file %s: %s' % (f, sys.exc_info()[1]), searcher=self) debug.logger & debug.flagSearcher and debug.logger('found %s, mtime %s' % (f, time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(pyTime)))) if pyTime >= mtime: raise error.PySmiFileNotModifiedError() raise error.PySmiFileNotFoundError('no compiled file %s found' % mibname, searcher=self)
Example #25
Source File: modulefinder.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def load_module(self, fqname, fp, pathname, file_info): suffix, mode, type = file_info self.msgin(2, "load_module", fqname, fp and "fp", pathname) if type == imp.PKG_DIRECTORY: m = self.load_package(fqname, pathname) self.msgout(2, "load_module ->", m) return m if type == imp.PY_SOURCE: co = compile(fp.read()+'\n', pathname, 'exec') elif type == imp.PY_COMPILED: if fp.read(4) != imp.get_magic(): self.msgout(2, "raise ImportError: Bad magic number", pathname) raise ImportError, "Bad magic number in %s" % pathname fp.read(4) co = marshal.load(fp) else: co = None m = self.add_module(fqname) m.__file__ = pathname if co: if self.replace_paths: co = self.replace_paths_in_code(co) m.__code__ = co self.scan_code(co, m) self.msgout(2, "load_module ->", m) return m
Example #26
Source File: modulefinder.py From datafari with Apache License 2.0 | 5 votes |
def load_module(self, fqname, fp, pathname, file_info): suffix, mode, type = file_info self.msgin(2, "load_module", fqname, fp and "fp", pathname) if type == imp.PKG_DIRECTORY: m = self.load_package(fqname, pathname) self.msgout(2, "load_module ->", m) return m if type == imp.PY_SOURCE: co = compile(fp.read()+'\n', pathname, 'exec') elif type == imp.PY_COMPILED: if fp.read(4) != imp.get_magic(): self.msgout(2, "raise ImportError: Bad magic number", pathname) raise ImportError, "Bad magic number in %s" % pathname fp.read(4) co = marshal.load(fp) else: co = None m = self.add_module(fqname) m.__file__ = pathname if co: if self.replace_paths: co = self.replace_paths_in_code(co) m.__code__ = co self.scan_code(co, m) self.msgout(2, "load_module ->", m) return m
Example #27
Source File: test_compileall.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_mtime(self): # Test a change in mtime leads to a new .pyc. self.recreation_check(struct.pack('<4sl', imp.get_magic(), 1))
Example #28
Source File: pkgutil.py From GDMC with ISC License | 5 votes |
def read_code(stream): # This helper is needed in order for the PEP 302 emulation to # correctly handle compiled files import marshal magic = stream.read(4) if magic != imp.get_magic(): return None stream.read(4) # Skip timestamp return marshal.load(stream)
Example #29
Source File: test_compileall.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def data(self): with open(self.bc_path, 'rb') as file: data = file.read(8) mtime = int(os.stat(self.source_path).st_mtime) compare = struct.pack('<4sl', imp.get_magic(), mtime) return data, compare
Example #30
Source File: test_zipimport.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def make_pyc(co, mtime): data = marshal.dumps(co) if type(mtime) is type(0.0): # Mac mtimes need a bit of special casing if mtime < 0x7fffffff: mtime = int(mtime) else: mtime = int(-0x100000000L + long(mtime)) pyc = imp.get_magic() + struct.pack("<i", int(mtime)) + data return pyc