On Oct 31, 2008, at 6:00 AM, Stephane DROUARD wrote:

> 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.

The documentation needs to be fixed then. If the scope of a variable  
can't be determined, then it actually raises a compile-time error.

> As a consequence, Cython needs module variables to be declared  
> global to behave like under Python, resulting in a compatibility  
> issue.

Could you give an example?

> Would it be possible to implement a compatibility mode, where  
> Cython would behave as much as possible like Python?

This is certainly possible, but would have a significant performance  
hit.

>
> 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.

Again, this is a performance hit, and wouldn't handle the case that a  
module-level variable with the name of a module global is assigned at  
some later point in the program.

> During the tests, I remarked that:
>
> import __builtin__
> print __builtin__.__name__
>
> __name__ is not cached. A possible improvement ;-)

But what if I did

import __builtin__
print __builtin__.__name__
foo()
print __builtin__.__name__

where foo (defined elsewhere) is

def foo():
     import __builtin__
     __builtin__.__name__ = "something else"

Thanks for your comments.

- Robert


_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to