Ticket #422 (closed defect: fixed)

Opened 5 years ago

Last modified 4 years ago

bug in setting __module__

Reported by: Fernando Perez Owned by: haoyu
Priority: major Milestone: wishlist
Component: Code Generation Keywords:
Cc: biahaoyu@…

Description

Hi folks,

I'm wondering if this is expected behavior.  Consider some trivial
little class like

class Simpleton:
   def __str__(self):
       return "A simpleton"

   def incr(self,x):
       """Increment x by one.

       Examples:

s = Simpleton()
s.incr(1)
       2
s.incr(10)
       12
       """
       return x+1

If I build this thing in cython, the Simpleton.incr.__module__
attribute is set to None, while if I make it a pure python module,
it's correctly set to the module name:


In [18]: p sprimes.primes.Simpleton.incr.__module__
None

In [19]: p sprimes.pyprimes.Simpleton.incr.__module__
sprimes.pyprimes

This difference causes the doctest to miss any doctest examples that
might be included in the methods.

Note that for *top-level* functions in the extension module, the
__module__ attribute is correctly set:

In [21]: p sprimes.primes.primes
<built-in function primes>

In [22]: p sprimes.primes.primes.__module__
sprimes.primes

so the problem appears to be only for classes.

This is running cython 0.9.8.

Thanks for any feedback,

f

Attachments

fix_T422.patch Download (5.0 KB) - added by haoyu 4 years ago.
Revised version of the patch which using PyStringConst?

Change History

Changed 4 years ago by haoyu

This is due to PyFunction?_New creates the PyFunction? with the module set as NULL. We should call PyFunction?_NewEx, which accepts a PyObject? as module name.

Changed 4 years ago by haoyu

  • cc biahaoyu@… added

Changed 4 years ago by robertwb

It may be worth creating the PyObject?? module name at module load time (do we do that already), and simply re-using that, rather than re-creating it for every function.

Binding functions need to be changed as well.

Changed 4 years ago by haoyu

Revised version of the patch which using PyStringConst?

Changed 4 years ago by haoyu

  • owner changed from somebody to haoyu

Changed 4 years ago by haoyu

  • status changed from new to closed
  • resolution set to fixed
Note: See TracTickets for help on using tickets.