Skip to content

Exposing SchemaParser in Cython header#175

Merged
cjermain merged 2 commits intocapnproto:developfrom
cjermain:feature/schema-parser-header
Jan 16, 2019
Merged

Exposing SchemaParser in Cython header#175
cjermain merged 2 commits intocapnproto:developfrom
cjermain:feature/schema-parser-header

Conversation

@cjermain
Copy link
Collaborator

@cjermain cjermain commented Nov 7, 2018

This PR exposes the C_SchemaParser thisptr to any external Cython library that cimports from capnp.lib.capnp. While this is not intended for general use, the availability of the C SchemaParser in the Cython header is critical for loading a compiled type and dependencies. With this feature, external Cython libraries can load the compiled type, and then cast the DynamicStruct::Reader to the appropriate reader of that type. The following code serves as an example for taking advantage of this method.

In a Cython file:

from capnp.lib.capnp cimport SchemaParser, C_SchemaParser, _DynamicStructReader, C_DynamicStruct
import capnp.lib.capnp as capnplib

cdef extern from *:
    """ // The following code is evaluated as C

    void load_type(capnp::SchemaParser &parser) {
        parser.loadCompiledTypeAndDependencies<AddressBook>();
    }

    AddressBook::Reader cast_reader(capnp::DynamicStruct::Reader &reader) {
        return reader.as<AddressBook>();
    }
    """
    void load_type(C_SchemaParser parser)
    AddressBook.Reader cast_reader(C_DynamicStruct.Reader reader)


def initiate_schema():
    """ This should be called once to load the appropriate type,
    before using cast_to_compiled_type.
    """
    if capnplib._global_schema_parser is None:
        capnplib._global_schema_parser = SchemaParser() # Singleton

    load_type(capnplib._global_schema_parser.thisptr) # <<<<< thisptr provided by this PR

def cast_to_compiled_type(_DynamicStructReader reader):
    """ Takes a Dynamic Reader and casts it as a specific compiled type.
    """
    cdef AddressBook.Reader address_reader = cast_reader(reader.thisptr)

@cjermain
Copy link
Collaborator Author

cjermain commented Nov 8, 2018

This build fails because Python 3.3 support was dropped from setuptools (and pkg_resources). Two solutions are:

  1. Drop support for Python 3.3, since it has reached its end-of-life.
  2. Fix the setuptools version in travis.yml to one prior to version 40.

@jparyani, any thoughts? I'm happy to put up a separate PR for either.

@cjermain cjermain merged commit adfc76f into capnproto:develop Jan 16, 2019
@cjermain cjermain deleted the feature/schema-parser-header branch January 16, 2019 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant