[Cython] (Possible) bug: module level global vars end up in upper scope?

Yury V. Zaytsev yury at shurup.com
Wed Jul 27 16:05:59 EDT 2016


Hi Stefan,

On Wed, 27 Jul 2016, Stefan Behnel wrote:

> I wonder if we shouldn't consider the module init function a special
> (enough) case here that is never performance critical, and just always
> generate a frame for it. Later frame lookups would then still fail (so we'd
> create somewhat of an inconsistency), but the case above looks like a
> legitimate use case, and namedtuples are often (I guess in *most* cases)
> created at module init time.
>
> I created a ticket.
>
> https://github.com/cython/cython/issues/536

Thank you very much for the insightful analysis, makes total sense!

I agree that creating a frame for the init function sounds like a most 
reasonable solution, the only drawback that I can see is the inconsistency 
you mentioned, but apparently that's as good as it gets...

> As a work-around, I could only come up with a hack. You could create a 
> Python module, import and call into it from your Cython module, create 
> the namedtuple in Python, and then fix the __module__ reference of the 
> namedtuple class after the fact. Although I wonder when the insertion 
> into the module namespace happens. I couldn't find it on a quick look.

Now that you've explained the root cause, I believe that there is a less 
disgusting workaround one could possibly go for, what do you think?

     class PyTest(namedtuple('Test', 'test')):
         pass

-- 
Sincerely yours,
Yury V. Zaytsev


More information about the cython-devel mailing list