[New-bugs-announce] [issue16740] Types created with PyType_FromSpec lack a __module__ attribute.
Bradley Froehle
report at bugs.python.org
Fri Dec 21 00:23:29 CET 2012
New submission from Bradley Froehle:
Types created using PyType_FromSpec do not have a __module__ attribute by default. This caught me off guard.
$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import xxlimited
>>> xxlimited.Null.__module__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: __module__
Do we expect module authors to set the __module__ attribute immediately after calling PyType_FromSpec?
To refresh your memory, non-heap types determine the module/name combo according to something like::
try:
__module__, __name__ = tp_name.rsplit('.', 1)
except ValueError:
__module__, __name__ = 'builtins', tp_name
whereas heap types use something like::
__name__ = tp_name
@property
def __module__(self):
return self.__dict__['__module__']
I think this is unnecessarily confusing, and, as far as I know, not documented anywhere.
I see from reading the commit logs that it was felt that by allowing users to set __name__ (and therefore tp_name), it could have an unintended impact on the value __module__. If so, why didn't we just disallow setting __name__?
There are likely some unintended impacts of this decision, for example weird error message in other library functions:
>>> import inspect
>>> inspect.getfile(xxlimited.Null)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/inspect.py", line 415, in getfile
object = sys.modules.get(object.__module__)
AttributeError: __module__
Is there anything we can do here?
----------
components: Interpreter Core
messages: 177860
nosy: bfroehle
priority: normal
severity: normal
status: open
title: Types created with PyType_FromSpec lack a __module__ attribute.
type: behavior
versions: Python 3.2, Python 3.3, Python 3.4
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16740>
_______________________________________
More information about the New-bugs-announce
mailing list