Commit graph

337 commits

Author SHA1 Message Date
Rowan Reeve
a5c29a74d2 Schema loading from the wire
Cap'n Proto provides a schema loader, which can be used to dynamically
load schemas during runtime. To port this functionality to pycapnp,
a new class is provided `C_SchemaLoader`, which exposes the Cap'n
Proto C++ interface, and `SchemaLoader`, which is part of the pycapnp
library.

The specific use case for this is when a capnp message contains
a Node.Reader: The schema for a yet unseen message can be loaded
dynamically, allowing the future message to be properly processed.

If the message is a struct containing other structs, all the schemas for
every struct must be loaded to correctly parse the message. See
https://github.com/DaneSlattery/capnp_generic_poc for a
proof-of-concept.

Add docs and cleanup

Add more docs

Reduce changes

Fix flake8 formatting

Fix get datatype
2023-06-12 14:10:13 +02:00
Lasse Blaauwbroek
1e94f2e321 Remove the option to create servers through _new_server.
Inheritance is required now to create a server
2023-06-11 04:09:03 +02:00
Lasse Blaauwbroek
83d610c116 Remove some more c++ helper functions 2023-06-11 03:50:54 +02:00
Lasse Blaauwbroek
0483596da1 Miscellaneous 2023-06-09 22:05:12 +02:00
Lasse Blaauwbroek
d6261b6d79 Manually handle deallocation of some objects for the benefit of p3.7
Python 3.7 seems to have trouble dealocating objects in a timely fashion. We
rely on this, because the c++ destructors need to run before the kj event loop
is closed. Hence, we do it manually.
2023-06-09 22:05:12 +02:00
Lasse Blaauwbroek
74ebaff4e3 Make older python versions work 2023-06-09 22:05:05 +02:00
Lasse Blaauwbroek
770be41b6d Bugfix: Attach server to on_disconnect to prevent early closing 2023-06-08 08:11:23 +02:00
Lasse Blaauwbroek
7a8175ed84 Get rid of VoidPromise and (almost) Promise
We only retain RemotePromise for its pipelining capabilities.
2023-06-08 08:02:07 +02:00
Lasse Blaauwbroek
da6a07efd5 No more need for promise joining 2023-06-08 05:34:04 +02:00
Lasse Blaauwbroek
20868d7db0 Get rid of dead code 2023-06-08 04:13:59 +02:00
Lasse Blaauwbroek
4b5c4211f1 Force server methods to be async and client calls to use await 2023-06-08 03:56:57 +02:00
Lasse Blaauwbroek
a69bc72a0b Remove a bunch of unused code 2023-06-08 02:42:04 +02:00
Lasse Blaauwbroek
97bdeaea12 Disable option to run capnp without asyncio 2023-06-08 02:27:38 +02:00
Lasse Blaauwbroek
6e011cfe78 Get rid of capnp timer functionality.
The asyncio timer should now be used
2023-06-07 20:55:43 +02:00
Lasse Blaauwbroek
8feb377217 Allow reading and writing messages from sockets in async mode 2023-06-07 11:24:43 -07:00
Lasse Blaauwbroek
d53aa24733 Allow async capability implementation methods to return a tuple 2023-06-07 10:51:53 -07:00
Lasse Blaauwbroek
c037342615 Allow capability implementation methods to be async 2023-06-07 10:51:53 -07:00
Lasse Blaauwbroek
d32854eb00
Integrate the KJ event loop into Python's asyncio event loop (#310)
* Integrate the KJ event loop into Python's asyncio event loop

Fix #256

This PR attempts to remove the slow and expensive polling behavior for asyncio
in favor of proper linking of the KJ event loop to the asyncio event loop.

* Don't memcopy buffer

* Improve promise cancellation and prepare for timer implementation

* Add attribution for asyncProvider.cpp

* Implement timeout

* Cleanup

* First round of simplifications

* Add more a_wait functions and a shutdown function

* Fix edge-cases with loop shutdown

* Clean up calculator examples

* Cleanup

* Cleanup

* Reformat

* Fix warnings

* Reformat again

* Compatibility with macos

* Inline the asyncio loop in some places where this is feasible

* Add todo

* Fix

* Remove synchronous wait

* Wrap fd listening callbacks in a class

* Remove poll_forever

* Remove the thread-local/thread-global optimization

This will not matter much soon anyway, and simplifies things

* Share promise code by using fused types

* Improve refcounting of python objects in promises

We replace many instances of PyObject* by Own<PyRefCounter> for more automatic
reference management.

* Code wrapPyFunc in a similar way to wrapPyFuncNoArg

* Refactor capabilityHelper, fix several memory bugs for promises and add __await__

* Improve promise ownership, reduce memory leaks

Promise wrappers now hold a Own<Promise<Own<PyRefCounter>>> object. This might
seem like excessive nesting of objects (which to some degree it is, but with
good reason):
- The outer Own is needed because Cython cannot allocate objects without a
  nullary constructor on the stack (Promise doesn't have a nullary constructor).
  Additionally, I believe it would be difficult or impossible to detect when a
  promise is cancelled/moved if we use a bare Promise.
- Every promise returns a Owned PyRefCounter. PyRefCounter makes sure that a
  reference to the returned object keeps existing until the promise is fulfilled
  or cancelled. Previously, this was attempted using attach, which is redundant
  and makes reasoning about PyINCREF and PyDECREF very difficult.
- Because a promise holds a Own<Promise<...>>, when we perform any kind of
  action on that promise (a_wait, then, ...), we have to explicitly move() the
  ownership around. This will leave the original promise with a NULL-pointer,
  which we can easily detect as a cancelled promise.

Promises now only hold references to their 'parents' when strictly needed. This
should reduce memory pressure.

* Simplify and test the promise joining functionality

* Attach forgotten parent

* Catch exceptions in add_reader and friends

* Further cleanup of memory leaks

* Get rid of a_wait() in examples

* Cancel all fd read operations when the python asyncio loop is closed

* Formatting

* Remove support for capnp < 7000

* Bring asyncProvider.cpp more in line with upstream async-io-unix.c++

It was originally copied from the nodejs implementation, which in turn copied
from async-io-unix.c++. But that copy is pretty old.

* Fix a bug that caused file descriptors to never be closed

* Implement AsyncIoStream based on Python transports and protocols

* Get rid of asyncProvider

All asyncio now goes through _AsyncIoStream

* Formatting

* Add __dict__ to  PyAsyncIoStreamProtocol for python 3.7

* Reintroduce strange ipv4/ipv6 selection code to make ci happy

* Extra pause_reading()

* Work around more python bugs

* Be careful to only close transport when this is still possible

* Move pause_reading() workaround
2023-06-06 11:08:15 -07:00
Lasse Blaauwbroek
701cabe61d
Make pycapnp more GIL friendly (#308)
* Mark c++ capnp api with nogil

* Release gil before blocking in capnp during message reading
2023-03-13 09:56:02 -07:00
Sergey Dmitriev
e614da1025
Avoid reading random values for reader options from dangling reference (#300)
This patch fixes a problem of reading random values for reader options
in pycapnp. The code which adds task to the list captures 'opts' by
reference and that causes a problem in case when 'opts' is allocated on
on the caller's stack. By the time when task is handled the stack frame
holding the 'opts' is gone which leaves dangling reference to 'opts' in
lambda's captures. As a result pycapnp reads random values for reader
options which sometimes causes unexpected errors (for example an error
that nesting level ius negative).
2022-11-01 13:00:01 -07:00
Madhava Jay
fc617142c0 Fixed issue where pickle helpers didnt use new from_bytes with context
- Added with context to benchmarks that use with_bytes
2022-05-24 11:38:27 +10:00
Bernhard Liebl
4b75fe3b5e fix for unreleased buffers under mmap (issue 280) 2022-01-17 15:48:27 +01:00
Timothy Hobbs
edb4dc647d
Fix doc string for _DynamicResizableListBuilder 2021-10-06 10:11:43 +02:00
Jacob Alexander
6e7fffd7de
Applying black formatting
- Fixing flake8 configuration to agree with black
- Adding black validation check to github actions
2021-10-01 11:12:21 -07:00
John Vandenberg
4797654323 remove_event_loop: Allow ignoring specific errors
Replaces a bare except with Exception, and updates the
test case to specify only a single exception that is
allowed to occur.

Related to https://github.com/capnproto/pycapnp/issues/254
2021-06-01 16:30:35 +08:00
John Vandenberg
bd91c52d57 Fix bare exception from str(obj)
There is no test coverage for these exception clauses,
however the invocation of obj.__str__() for client
objects could raise any exception, hence the very broad
exception catch.
2021-06-01 15:51:23 +08:00
John Vandenberg
a20e69dbe9 Replace bare exception from getfullargspec
getfullargspec is based on signature, which is documented to
raise TypeError and ValueError.

Related to https://github.com/capnproto/pycapnp/issues/254
2021-06-01 15:45:57 +08:00
John Vandenberg
0f6df849cd Refine exception invoking which on non-union type
Related to https://github.com/capnproto/pycapnp/issues/254
2021-06-01 15:40:35 +08:00
John Vandenberg
c179c72087 Allow enum to be used with .init 2021-05-31 19:31:53 +08:00
John Vandenberg
1795cac230 _StructModuleWhich: Use enum 2021-05-31 18:57:40 +08:00
John Vandenberg
0c904443bd Add Union on top level union messages
Closes https://github.com/capnproto/pycapnp/issues/247
2021-05-31 16:33:10 +08:00
Pavol Vargovcik
c0109662d2 _SegmentArrayMessageReader: release each view 2021-05-13 13:33:16 +02:00
John Vandenberg
8c15feb4eb Remove many lint warnings
Mostly whitespace changes, with max-line-length set to 120
and double-quotes used, adopting black settings.

Related to https://github.com/capnproto/pycapnp/issues/128
2021-05-03 17:02:30 +08:00
John Vandenberg
a730554030 Dont crash if __file__ is not set by importer 2021-04-30 10:41:15 +08:00
Timothy Hobbs
0353044587
bool → cbool (probably just a typo)
Fixes https://github.com/capnproto/pycapnp/issues/249
2021-04-25 21:51:12 +02:00
Sergii Vystoropskyi
31ffe4ae2b minor bug fixes for python3 + .c++ file support that is not destructive 2021-04-08 00:53:45 -07:00
Keith Rothman
480a079a16 Add missing inhertance to _Schema for _StructSchema.
Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
2020-12-14 11:11:38 -08:00
Jacob Alexander
a4ef16e831
Fixing flake8 linting errors
- Enabling builds to fail on simple lint failures now
2020-11-20 23:38:13 -08:00
Jacob Alexander
5e8ccba536
Updating docs for v1.0.0b2
- Full cleanup of all the docs
- General sphinx housekeeping
- Updated all the old/bad links
- More reliable tests
- Updated Changelog
- Removed dead/deprecated code
- Added documentation generation test
2020-06-14 17:05:45 -07:00
Jacob Alexander
49fd4854d8 Minor code cleanup
- Reducing linter errors
- Removing old Python 2-only code (__future__ print)
2020-06-08 23:51:07 -07:00
Jacob Alexander
255119b838 Updating to capnproto v0.8.0
- Includes some test stabilization
- Fixes manylinux2010 build issues (linker flag order due to old gcc)
- More rigorous python setup.py clean
- Requires capnproto v0.8.0 or greater
- Including system libcapnp include path for import (e.g. import
  stream_capnp)
- Bundle libcapnp .capnp files when not using system libcapnp
- Removing more distutils usage. Now using pkg-config to determine the
  system version of libcapnp (mainly for Linux, but should work on macOS
  with brew)
- Removed dead code

Resolves issues #215 #216 #217
Lots of fixes for Issue #218 (all sorts of retry methods needed for
GitHub Actions)
2020-06-08 11:49:17 -07:00
Floris van Nee
8bbb0f47ae Avoid infinite loop on to_dict() for RPC results 2020-04-24 19:49:50 +02:00
Keith Rothman
dbc9debbbf Move exported functions used in capabilityHelper.h from extern "C" to api.
Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
2020-03-02 06:49:59 -08:00
Jacob Alexander
9de385900a
Merge pull request #205 from litghost/remove_circular_imports
Rearrange declarations to avoid circular imports.
2020-02-28 18:43:16 -08:00
Keith Rothman
20d6c4eef7 Rename capnp.lib.capnp.Promise to capnp.lib.capnp._Promise.
This is to prevent aliasing with capnp.includes.capnp_cpp.Promise. This
aliasing is problematic if capnp/lib/capnp.pxd uses the template definition
from capnp.includes.capnp_cpp.Promise.

Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
2020-02-28 16:53:52 -08:00
Keith Rothman
3b2eedb741 Rearrange declarations to avoid circular imports.
Circular imports make import pxd's order sensitive, and violating this
order results in hard to understand errors.  As far as I can tell, this
rearrangement removes the circular nature, and has no other side
affects.

Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
2020-02-28 16:43:28 -08:00
Keith Rothman
707cda97f4 Make imports absolute instead of relative.
This enables Cython modules outside of pycapnp to use these imports.

Signed-off-by: Keith Rothman <537074+litghost@users.noreply.github.com>
2020-02-28 12:02:59 -08:00
Jacob Alexander
ac6cdec2d0
Fixes Issue #202
- __path__ was being set to a string
- It's supposed to be a list of strings or None

For pycapnp pydoc isn't super useful at the module level as pydoc will
scan all the files in the directory and just list them.

Here's thread_capnp for example, notice how it doesn't show any
information on the .capnp files.

Help on package thread_capnp:

NAME
    thread_capnp

PACKAGE CONTENTS
    addressbook
    async_calculator_client
    async_calculator_server
    async_client
    async_reconnecting_ssl_client
    async_server
    async_ssl_client
    async_ssl_server
    calculator_client
    calculator_server
    thread_client
    thread_server

DATA
    Example = <capnp.lib.capnp._InterfaceModule object>
    schema = <capnp.lib.capnp._ParsedSchema object>
2020-02-07 16:44:41 -08:00
Pavol Vargovcik
f3ae35eedd fix: package_path is _NamespacePath, not list 2020-01-20 17:51:41 +01:00
Jacob Alexander
dd3551f978
Adding poll_once() to TwoPartyServer API
- poll_forever() doesn't allow for checking socket connection to client
  * Need to check for client, if eof received then we can close the
  connection and cleanup pycapnp cpu resources (async tasks)
- Updated async examples to fix bugs
  * Add checking code for socket connection to the client and gracefully
  cleanup resources once the client socket connection closes
  * Instanciate a new TwoPartyServer per connection (allows for multiple
  connections)
- Should resolve Issue #198
2020-01-08 18:06:26 -08:00