Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in setting __module__ #985

Closed
robertwb opened this issue Oct 17, 2009 · 8 comments
Closed

bug in setting __module__ #985

robertwb opened this issue Oct 17, 2009 · 8 comments

Comments

@robertwb
Copy link
Contributor

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 [p sprimes.primes.Simpleton.incr.__module__
None

In [19](18]:): 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 [p sprimes.primes.primes
<built-in function primes>

In [22](21]:): 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

At 2010-05-17T10:33:38Z @bhy added attachment fix_T422.patch

Migrated from http://trac.cython.org/ticket/422

@robertwb
Copy link
Contributor Author

robertwb commented Apr 2, 2010

@bhy commented

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.

@robertwb
Copy link
Contributor Author

robertwb commented Apr 3, 2010

@bhy changed cc to biahaoyu@gmail.com
commented

@robertwb
Copy link
Contributor Author

robertwb commented Apr 7, 2010

@robertwb commented

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.

@robertwb
Copy link
Contributor Author

@bhy changed owner from somebody to haoyu
commented

Patch for review:
http://codereview.appspot.com/1228043/

@robertwb
Copy link
Contributor Author

@bhy changed resolution to fixed
status from new to closed
commented

@graingert
Copy link

I seem to still see this in cython 0.29.32 (it's fixed in Cython-3.0.0a11)

@graingert
Copy link

ah this only applies to cdef classes eg:

import cython

@cython.cclass
class Function:
    @cython.ccall
    def evaluate(self, x: float) -> float:
        return 0

I get the __module__ attribute in v3a11:

Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyximport
>>> pyximport.install(reload_support=True)
(None, <pyximport._pyximport3.PyxImportMetaFinder object at 0x7efd8a6ef970>)
/home/graingert/.virtualenvs/testing310/lib/python3.10/site-packages/Cython/Compiler/Main.py:345: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: /home/graingert/projects/uvloop/foo.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
>>> foo.Function().evaluate
<bound method Function.evaluate of <foo.Function object at 0x7efd8a59d990>>
>>> foo.Function().evaluate.__module__
'foo'

but not in 0.29.32:

Python 3.10.4 (main, Jun 29 2022, 12:14:53) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyximport
>>> pyximport.install(reload_support=True)
(None, <pyximport.pyximport.PyxImporter object at 0x7f1e52ecb970>)
>>> import foo
/home/graingert/.virtualenvs/testing310/lib/python3.10/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/graingert/projects/uvloop/foo.pyx
  tree = Parsing.p_module(s, pxd, full_module_name)
>>> foo.Function()
<foo.Function object at 0x7f1e52a68870>
>>> foo.Function().evaluate
<built-in method evaluate of foo.Function object at 0x7f1e50f429f0>
>>> foo.Function().evaluate.__module__

@graingert
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants