homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: importlib simplification
Type: enhancement Stage: needs patch
Components: Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: Jim.Jewett, brett.cannon, eric.snow, meador.inge
Priority: normal Keywords:

Created on 2012年02月09日 17:53 by Jim.Jewett, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (2)
msg152970 - (view) Author: Jim Jewett (Jim.Jewett) * (Python triager) Date: 2012年02月09日 17:53
http://hg.python.org/cpython/file/aba513307f78/Lib/importlib/_bootstrap.py#l974
 974 # The hell that is fromlist ...
 975 if not fromlist:
 976 # Return up to the first dot in 'name'. This is
complicated by the fact
 977 # that 'name' may be relative.
 978 if level == 0:
 979 return sys.modules[name.partition('.')[0]]
 980 elif not name:
 981 return module
 982 else:
 983 cut_off = len(name) - len(name.partition('.')[0])
 984 return sys.modules[module.__name__[:-cut_off]]
If level is 0, should name == module.__name__?
Yes.
 
If so, then I think that simplifies to
 if not name:
 return module
 genericname=module.__name__.rpartition(".")[0]
 return sys.modules[genericname]
Seems right. Can you file a bug and assign it to me?
msg153547 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2012年02月17日 14:20
So I simply swapped out the code and the tests fail. Then I realized why: while the assumption is right, that does not mean that that name passed to __import__() isn't relative and thus shifts what need to be returned (the else clause case). That's why it's a slice off of __name__ based on name itself; name is some funky tail section of __name__ for relative imports.
History
Date User Action Args
2022年04月11日 14:57:26adminsetgithub: 58185
2012年02月17日 14:20:51brett.cannonsetstatus: open -> closed
resolution: not a bug
messages: + msg153547
2012年02月16日 05:05:48meador.ingesetnosy: + meador.inge
2012年02月10日 06:51:49eric.snowsetnosy: + eric.snow
2012年02月09日 17:55:16ezio.melottisetassignee: brett.cannon
stage: needs patch
type: enhancement
versions: + Python 3.3
2012年02月09日 17:53:52Jim.Jewettcreate

AltStyle によって変換されたページ (->オリジナル) /