Some fixes to the magic import system

- Stop adding the directory of every .capnp file to the import path. If a .capnp
  file wants to import a file in its own directory, it should use a relative
  import. Fixes #278
- Stop using /usr/include/capnp as an import path. This is incorrect. It should
  only be /usr/include.
- Stop allowing additional paths to be specified for magic imports. This leads
  to inconsistencies. More specifically, the way that a nested import like
  `ma.mb.mc_capnp` gets imported by python, is to first import `ma`, then import
  `ma.mb`, and finally `ma.mb.mc_capnp`. Pycapnp's magic importing is only
  involved in the last step. So any additional paths specified don't work for
  nested imports. It is very confusing to only have this for non-nested imports.
  Users with folder layouts that don't follow pythons import paths can still use
  `capnp.load(.., .., imports=[blah])`.
This commit is contained in:
Lasse Blaauwbroek
2023-11-24 23:34:27 +01:00
committed by Jacob Alexander
parent b6ea909e9a
commit 3aade70bfa
4 changed files with 47 additions and 64 deletions

5
test/schemas/child.capnp Normal file
View File

@@ -0,0 +1,5 @@
@0x9afc0f7513269df3;
struct Child {
name @0 :Text;
}

View File

@@ -0,0 +1,7 @@
@0x95c41c96183b9c2f;
using import "/schemas/child.capnp".Child;
struct Parent {
child @0 :List(Child);
}

View File

@@ -80,7 +80,7 @@ def test_spaces_import():
def test_add_import_hook():
capnp.add_import_hook([this_dir])
capnp.add_import_hook()
# Make sure any previous imports of addressbook_capnp are gone
capnp.cleanup_global_schema_parser()
@@ -93,7 +93,6 @@ def test_add_import_hook():
def test_multiple_add_import_hook():
capnp.add_import_hook()
capnp.add_import_hook()
capnp.add_import_hook([this_dir])
# Make sure any previous imports of addressbook_capnp are gone
capnp.cleanup_global_schema_parser()
@@ -104,7 +103,7 @@ def test_multiple_add_import_hook():
def test_remove_import_hook():
capnp.add_import_hook([this_dir])
capnp.add_import_hook()
capnp.remove_import_hook()
if "addressbook_capnp" in sys.modules:
@@ -118,7 +117,12 @@ def test_remove_import_hook():
def test_bundled_import_hook():
# stream.capnp should be bundled, or provided by the system capnproto
capnp.add_import_hook()
import stream_capnp # noqa: F401
from capnp import stream_capnp # noqa: F401
def test_nested_import():
import schemas.parent_capnp # noqa: F401
import schemas.child_capnp # noqa: F401
async def test_load_capnp(foo):