[pypy-dev] Functions `.__module__` attribute

cool-RR cool-rr at cool-rr.com
Tue May 10 20:39:23 CEST 2011


On Tue, May 10, 2011 at 2:18 PM, Antonio Cuni <anto.cuni at gmail.com> wrote:

> On 10/05/11 18:50, cool-RR wrote:
> > Hey,
> >
> > It seems that in CPython a function gets a `.__module__` attribute
> according
> > to the current value of `__name__` when the function is being defined. In
> Pypy
> > this seems not to be working, since I changed `__name__` in globals (as
> part
> > of a test) but it still didn't change the function's `.__module__`. Is
> this a
> > deficiency in Pypy?
>
> could you please write the smallest possible test that shows the behavior?
>
> The source of the problem might be that pypy's Function object caches its
> w_module attribute: so, if you get func.__module__ and *then* change
> __name__,
> the modification is not seen.
>
> Also, it seems that CPython sets the module name at function-definition
> time,
> while pypy does it only the first time that __module__ is actually got. So,
> for example, the following code:
>
> def one(): pass
> def two(): pass
> print one.__module__,
> __name__ = 'foo'
> print two.__module__
>
> prints '__main__ __main__' on cpython, and '__main__ foo' on pypy.
>
> Not sure whether it's a bug or an implementation detail.
>
> ciao,
> Anto
>

Ah, then my problem is easily solved by accessing `my_function.__module__`
before I change `__name__` back to its original value. I tested and it
works.

I can still make a test case if you have interest in it.


Ram.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20110510/6adbbd91/attachment.html>


More information about the pypy-dev mailing list