Search code examples
pythonruntimegrpccython

Modifying cython instance read-only properties in runtime


I am using python aio grpc implementation, which is using cython. One of grpc features is interceptor, this is a class which get the request before the grpc's Server instance, and can modify the requests as it wants.

The proper way to use interceptors is to pass them to the Server constructor on __init__.

I need to do so in runtime, that means that I need to modify the server._interceptors list after the instance is already created.

it's working perfect when using pure-python implementations, but not when using Cython.

This is the cython AioServer implementation.

when trying to access _interceptors field, I get 'grpc._cython.cygrpc.AioServer' object has no attribute 'interceptors'. When trying to replace any function, I get - 'grpc._cython.cygrpc.AioServer' object attribute '_request_call' is read-only.

is there anyway to modify cython implemented instances on runtime ? or it's just not possible ? I want to modify the property or monkey patch some function which will modify the property, but the important part is that should happen on RUNTIME.

Thanks!


Solution

  • It isn't really "read only" - it's simply inaccessible from Python, since no-one has asked Cython to generate accessor functions for that attribute (either read-write or read-only). However, you can access it from Cython when the type is known. For example (in Cython):

    def change_interceptors(AioServer server, new_interceptors):
        server._interceptors = new_interceptors
    

    So, if you're prepared to write Cython code then you can modify this property. If you aren't then you can't.