[Cython] Optimising dict manipulation in extension types
Stefan Behnel
stefan_ml at behnel.de
Sun Nov 9 11:56:56 CET 2008
Hi,
tav wrote:
> Stefan Behnel wrote:
>> Who needs setters when you can modify "this" directly?
>>
>> >>> def test():
>> ... a = []
>> ... def get(i):
>> ... return a[i]
>> ... return get
>> >>> get = test()
>> >>> get.func_closure[0].cell_contents.append(123)
>> >>> get(0)
>> 123
>
> Hehe =)
>
> This bit of code protects against that: http://paste.lisp.org/display/70003
Nice. Having PJE in there makes me suspect that it might actually work.
>> Use a "cdef class" in Cython instead. It's implemented in C, so you need to
>> modify the class instance memory at the C level in order to change the object
>> in other ways than you allow. That's not secure, untrusted code may do that,
>> but it's not trivial and therefore pretty unlikely at least.
>
> Untrusted code is pure Python only =)
ctypes?
> On a separate note, I am having trouble with dynamically returning a
> method (function) via __getattribute__. I'd like to be able to
> dynamically return attributes like __str__/__len__ -- and for them to
> be used by the likes of str(), len(), etc.
My guess is that this is because __str__ and friends are exposed at the C
level as part of the type struct. This likely overrides the __getattribute__
lookup mechanism.
Maybe you can just implement them and let them delegate to the right functions
instead.
Stefan
More information about the Cython-dev
mailing list