[pypy-issue] Issue #2818: __module__ read-only for PyCFunction objects (pypy/pypy)

Hrvoje Nikšić issues-reply at bitbucket.org
Thu May 3 10:52:55 EDT 2018

New issue 2818: __module__ read-only for PyCFunction objects

Hrvoje Nikšić:

Our CPython extensions sometimes create functions in C and modify their `__module__` later. This is needed because those functions are referenced by `__reduce__`, and we don't want their internal implementation module to leak into pickled files. Instead, we change `fn.__module__` from `bla._internal` to `bla`, which is correct because `bla.fn` really exists. This works in CPython, but fails in PyPy with a `TypeError`:

TypeError: readonly attribute '__module__'

The functions are created using `Py_InitModule3` and retrieved using `PyObject_GetAttr` or equivalent. A minimal example of code that works under CPython, but fails under PyPy is attached. Under CPython it successfully sets `fn.__module__`, while on PyPy it fails as described above.

We could probably work around the issue by replacing the `PyCFunction` with a custom-created callable with a writable `__module__`, but I am reporting this as an incompatibility with CPython's API.

More information about the pypy-issue mailing list