修改Cython实例的只读属性在运行时

huangapple go评论68阅读模式
英文:

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 gets 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 perfectly 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 any way to modify Cython implemented instances at runtime? Or is it 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 it should happen at RUNTIME.

Thanks!

英文:

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!

答案1

得分: 1

不是真正的"只读" - 只是从Python中无法访问,因为没有人要求Cython生成该属性的访问器函数(无论是读写还是只读)。但是,当类型已知时,你可以从Cython中访问它。例如(在Cython中):

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

因此,如果你愿意编写Cython代码,你可以修改这个属性。如果不愿意,那就不能。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月10日 21:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定