Hello,
In the documentation it is written that Cython defaults to the built-in scope
when it can't determine the scope of a variable, which is different from
Python, which defaults to the module scope.
As a consequence, Cython needs module variables to be declared global to behave
like under Python, resulting in a compatibility issue.
Would it be possible to implement a compatibility mode, where Cython would
behave as much as possible like Python?
Concerning undetermined variables, this could:
- not cache them,
- use a version of __Pyx_GetName() like below:
static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
PyObject *result;
result = PyObject_GetAttr(dict, name);
// If not found and the dictionary is the module, try in builtin.
if (!result && dict == __pyx_m) {
PyErr_Clear();
result = PyObject_GetAttr(__pyx_b, name);
}
if (!result)
PyErr_SetObject(PyExc_NameError, name);
return result;
}
An improvement could be:
- declare them (static PyObject *__pyx_builtin_xxx = NULL) but do not
initialize them through __Pyx_InitCachedBuiltins(),
- access them through a function that first checks if the variable is
initialised (!= NULL), so return it, otherwise do something like above,
- if the attribute is got from builtin, update the pointer.
During the tests, I remarked that:
import __builtin__
print __builtin__.__name__
__name__ is not cached. A possible improvement ;-)
Cheers,
Stephane
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev