[issue33268] iteration over attrs in metaclass' __new__ affects class' __name__

2018-04-12 Thread tkhyn

tkhyn <thomas.khyn_pyt...@m4x.org> added the comment:

oops,  indeed. Sometimes the obvious things are not so obvious ..
Sorry for the noise!

--

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33268>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33268] iteration over attrs in metaclass' __new__ affects class' __name__

2018-04-12 Thread tkhyn

New submission from tkhyn <thomas.khyn_pyt...@m4x.org>:

The following script, run with python 3.6.5, highlights an issue with the 
class' __name__ attribute being set incorrectly just because of a loop in the 
metaclass' __new__ method:

class MC(type):
def __new__(mcs, name, bases, attrs):
for name, attr in attrs.items():
pass
return super(MC, mcs).__new__(mcs, name, bases, attrs)

class C(metaclass=MC):
a = None

print(C.__name__)


Expected output: "C"
Actual output: "a"

Comment the for loop and you get the expected output!

On Python 2.7.13, the amended code exposes the same bug:

class MC(type):
def __new__(mcs, name, bases, attrs):
for name, attr in attrs.items():
pass
return super(MC, mcs).__new__(mcs, name, bases, attrs)

class C(object):
__metaclass__ = MC
a = None

print C.__name__

output is "__metaclass__" and should be "C"

--
components: Interpreter Core
messages: 315218
nosy: tkhyn
priority: normal
severity: normal
status: open
title: iteration over attrs in metaclass' __new__ affects class' __name__
versions: Python 2.7, Python 3.6

___
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33268>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30436] importlib.find_spec raises AttributeError/ModuleNotFoundError when parent is not a package/module

2017-05-23 Thread tkhyn

tkhyn added the comment:

Ok, thanks for the reply. Actually the thing that bothered me was the 
AttributeError exception. I would probably not have opened a ticket should 
find_spec have raised a ModuleNotFoundError (in line with import_module).

Would you consider catching the AttributeError (which means detecting if 
parent_name relates to a package) to raise a ModuleNotFoundError instead more 
appropriate?

--

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30436>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30436] importlib.find_spec raises AttributeError/ModuleNotFoundError when parent is not a package/module

2017-05-22 Thread tkhyn

New submission from tkhyn:

Hello, I stumbled upon this issue when using the module_has_submodule function 
in Django, which raised an exception when trying to import a dotted path such 
as ``parent.module`` when ``parent`` does not exist or is not a package. I 
would expect (as well as the django devs, apparently) find_spec to return None 
instead of raising an AttributeError or ModuleNotFoundError. 

Unless you think Django or any package making use of importlib.find_spec should 
handle these exceptions, the fix is quite simple.

Steps to reproduce (with Python 3.6.1):

touch parent.py
python3.6
>>> from importlib.util import find_spec
>>> find_spec('parent.module')
  File "C:\Python\3.6\Lib\importlib\util.py", line 89, in find_spec
return _find_spec(fullname, parent.__path__)
AttributeError: module 'parent' has no attribute '__path__'
>>> find_spec('invalid_parent.module')
  File "C:\Python\3.6\Lib\importlib\util.py", line 88, in find_spec
parent = __import__(parent_name, fromlist=['__path__'])
ModuleNotFoundError: No module named 'invalid_parent'

The fix is quite simple, replacing

if fullname not in sys.modules:
parent_name = fullname.rpartition('.')[0]
if parent_name:
# Use builtins.__import__() in case someone replaced it.

parent = __import__(parent_name, fromlist=['__path__'])
return _find_spec(fullname, parent.__path__)



else:

return _find_spec(fullname, None)
by:

if fullname not in sys.modules:
parent_name = fullname.rpartition('.')[0]
if parent_name:
# Use builtins.__import__() in case someone replaced it.
try:
parent = __import__(parent_name, fromlist=['__path__']).__path__

except (AttributeError, ModuleNotFoundError):
# parent is not a package
return None
else:
parent = None
return _find_spec(fullname, parent)


in importlib.util.find_spec.

--
components: Library (Lib)
messages: 294209
nosy: tkhyn
priority: normal
severity: normal
status: open
title: importlib.find_spec raises AttributeError/ModuleNotFoundError when 
parent is not a package/module
versions: Python 3.6

___
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30436>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com