mirror of
https://github.com/capnproto/pycapnp.git
synced 2025-03-04 08:24:43 +01:00
Change import hook to require modules to end in '_capnp'
This commit is contained in:
parent
98c9b959f7
commit
891f0d28e5
2 changed files with 26 additions and 2 deletions
|
@ -1165,6 +1165,11 @@ class _Importer:
|
|||
module_name = mod_parts[-1]
|
||||
else:
|
||||
module_name = fullname
|
||||
|
||||
if not module_name.endswith('_capnp'):
|
||||
return None
|
||||
|
||||
module_name = module_name.rstrip('_capnp')
|
||||
capnp_module_name = module_name + self.extension
|
||||
|
||||
if package_path:
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import pytest
|
||||
import capnp
|
||||
import os
|
||||
import sys
|
||||
|
||||
this_dir = os.path.dirname(__file__)
|
||||
|
||||
|
@ -57,5 +58,23 @@ def test_failed_import():
|
|||
def test_add_import_hook():
|
||||
capnp.add_import_hook([this_dir])
|
||||
|
||||
import addressbook
|
||||
addressbook.AddressBook.new_message()
|
||||
import addressbook_capnp
|
||||
addressbook_capnp.AddressBook.new_message()
|
||||
|
||||
def test_multiple_add_import_hook():
|
||||
capnp.add_import_hook()
|
||||
capnp.add_import_hook()
|
||||
capnp.add_import_hook([this_dir])
|
||||
|
||||
import addressbook_capnp
|
||||
addressbook_capnp.AddressBook.new_message()
|
||||
|
||||
def test_remove_import_hook():
|
||||
capnp.add_import_hook([this_dir])
|
||||
capnp.remove_import_hook()
|
||||
|
||||
if 'addressbook_capnp' in sys.modules:
|
||||
del sys.modules['addressbook_capnp'] # hack to deal with it being imported already
|
||||
|
||||
with pytest.raises(ImportError):
|
||||
import addressbook_capnp
|
||||
|
|
Loading…
Add table
Reference in a new issue