Fix up bug with import hook on python3/pypy

This commit is contained in:
Jason Paryani
2013-09-01 22:24:46 -07:00
parent 5600f9939a
commit 98c9b959f7
2 changed files with 10 additions and 10 deletions

View File

@@ -850,6 +850,10 @@ cdef class SchemaParser:
fileSchema = parser._parse_disk_file(display_name, file_name, imports) fileSchema = parser._parse_disk_file(display_name, file_name, imports)
_load(fileSchema, module) _load(fileSchema, module)
abs_path = _os.path.abspath(file_name)
module.__path__ = _os.path.dirname(abs_path)
module.__file__ = abs_path
return module return module
cdef class _MessageBuilder: cdef class _MessageBuilder:
@@ -1143,7 +1147,10 @@ class _Loader:
self.fullname, fullname)) self.fullname, fullname))
imports = self.additional_paths + _sys.path imports = self.additional_paths + _sys.path
return load(self.path, fullname, imports=imports) module = load(self.path, fullname, imports=imports)
_sys.modules[fullname] = module
return module
class _Importer: class _Importer:
def __init__(self, additional_paths): def __init__(self, additional_paths):
@@ -1198,7 +1205,7 @@ def add_import_hook(additional_paths=[]):
""" """
global _importer global _importer
if _importer is not None: if _importer is not None:
return remove_import_hook()
_importer = _Importer(additional_paths) _importer = _Importer(additional_paths)
_sys.meta_path.append(_importer) _sys.meta_path.append(_importer)

View File

@@ -56,13 +56,6 @@ def test_failed_import():
def test_add_import_hook(): def test_add_import_hook():
capnp.add_import_hook([this_dir]) capnp.add_import_hook([this_dir])
import addressbook import addressbook
addressbook.AddressBook.new_message() addressbook.AddressBook.new_message()
def test_remove_import_hook():
capnp.add_import_hook([this_dir])
capnp.remove_import_hook()
with pytest.raises(ImportError):
import addressbook