capnproto / pycapnp

Cap'n Proto serialization/RPC system - Python bindings
BSD 2-Clause "Simplified" License
458 stars 125 forks source link

Parsing compiled schema from CodeGeneratorRequest; _NodeReader vs _DynamicListReader #321

Open ahupp opened 11 months ago

ahupp commented 11 months ago

I'm writing some python with pycapnp to reflect on arbitrary capnp RPC endpoints. These endpoints have a method that returns their corresponding CodeGeneratorRequest, and I was hoping to load that at runtime. However, I'm hitting this error. Setup for the example to get a compiled schema:

$ cat example.capnp
@0xc9b9b8866c80620b;

struct Example {
  value @0 :Int64;
}

$ capnp compile -o- -I /usr/include example.capnp > example.bin

Then this script to load the compiled schema:

import capnp
import capnp.schema_capnp

cgr = capnp.schema_capnp.CodeGeneratorRequest.read(open("repro.bin", "rb"))
loader = capnp.SchemaLoader()
loader.load(cgr.nodes)

This fails with:

Traceback (most recent call last):
  File "example.py", line 6, in <module>
    loader.load(cgr.nodes)
TypeError: Argument 'reader' has incorrect type (expected capnp.lib.capnp._NodeReader, got capnp.lib.capnp._DynamicListReader)

Am I doing this wrong? Is there a way to work around it?

ahupp commented 11 months ago

It kind of looks like I can use:

loader = capnp.SchemaLoader()
for node in cgr.nodes:
   loader.load_dynamic(node)

Along with copying some of the Schema -> python module logic in SchemaParser. Still doesn't quite work though, I'm seeing a wide range of errors. Happy to share a more detailed repro if this is unexpected.