Clone this wiki locally
Per-instance extra scope variables
- Author: DagSverreSeljebotn
- Status: Idea
NOT DONE!, only draft, I got another idea... :-)
The reason for this madness
- To access NumPy arrays, strides must be looked up and divided by the datatype size. I can see three :
- A custom parse tree translation for NumPy (whether pluggable in by a plugin, specified in pxd, or otherwise)
- A simple approach of
This can mostly be considered a hack.
, in order to be able to implement [:enhancements/operators/ambitious] for NumPy arrays without expecting too complex optimizations of the Cython compiler. The full NumPy problem will be described at the bottom, but the spec comes first.
Please be patient and you'll hopefully see why at the bottom...
- This is done:
- A new scopevars block can be defined on a type (in pxd or pyx, not sure)
- The variables in this block is instantiated in the local scope
- Variables in the block can be accessed using the scope() function from within the class, and any method calling scope() must be inlined by the Cython compiler.
To allow a scopevars block within a cdef class:
ctypedef class Foo: char* complex_buffer scopevars: int info
cdef class Foo: def __enterscope__(self): scope(self).info = self.complex_buffer // 4 def bar(self): print scope(self).info
When using this type now, the following happens:
Foo f = ... f.info()
is transformed by the ScopeVarsTransformer to:
Foo f = ... int _scopetmp_f_info _scopetmp_f_info = f.complex_buffer // 34 print _scopetmp_f_info