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.
Created on 2011年09月06日 16:30 by eric.araujo, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| issue12915.diff | berker.peksag, 2012年12月10日 13:48 | review | ||
| issue12915_v2.diff | berker.peksag, 2012年12月30日 23:05 | review | ||
| Pull Requests | |||
|---|---|---|---|
| URL | Status | Linked | Edit |
| PR 18310 | merged | vinay.sajip, 2020年02月02日 10:46 | |
| PR 18517 | merged | vinay.sajip, 2020年02月15日 21:48 | |
| PR 18720 | merged | Michael.Felt, 2020年03月01日 14:25 | |
| Messages (32) | |||
|---|---|---|---|
| msg143626 - (view) | Author: Éric Araujo (eric.araujo) * (Python committer) | Date: 2011年09月06日 16:30 | |
The need to resolve a dotted name to a Python object is spreading in the stdlib: pydoc has locate and resolve, packaging has util.resolve_name, unittest has something else, etc. For the benefit of stdlib maintainers as well as the community, I think such functionality should be exposed publicly by the inspect module. |
|||
| msg143796 - (view) | Author: Terry J. Reedy (terry.reedy) * (Python committer) | Date: 2011年09月09日 19:41 | |
To be a little clearer, this is about dotted import names, not regular dotted names. pydoc.locate(path, forceload=0): """Locate an object by name or dotted path, importing as necessary""" pydoc.resolve(thing, forceload=0): """Given an object or a path to an object, get the object and its name.""" It is not completely clear to me how this is different from using __import__ but I will believe it is. It something like this is in at least 3 places, +1 on factoring it out to one place. |
|||
| msg143911 - (view) | Author: Éric Araujo (eric.araujo) * (Python committer) | Date: 2011年09月12日 15:50 | |
Sorry if I was unclear; the functions work with a dotted name as a string and resolve it to an object. __import__ works with module, whereas this kind of functions work with mod.name, pkg.mod.name.attr, etc., that is, they combine import and getattr. |
|||
| msg143960 - (view) | Author: Éric Araujo (eric.araujo) * (Python committer) | Date: 2011年09月13日 12:26 | |
In addition, error handling/reporting is not trivial to get right. We’ve had to fix the code in distutils2 and it’s still not quite right (#12703). I opened this report because I’d like to see all stdlib modules use the same functions and I’d prefer people to copy-paste the same robust code for backports. |
|||
| msg145941 - (view) | Author: Roundup Robot (python-dev) (Python triager) | Date: 2011年10月19日 19:32 | |
New changeset 1405df4a1535 by Éric Araujo in branch 'default': Expand tests and fix bugs in packaging.util.resolve_name. http://hg.python.org/cpython/rev/1405df4a1535 |
|||
| msg146020 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2011年10月20日 15:10 | |
The version in logging.config appears to be doing the same job, but is shorter:
def _resolve(name):
"""Resolve a dotted name to a global object."""
name = name.split('.')
used = name.pop(0)
found = __import__(used)
for n in name:
used = used + '.' + n
try:
found = getattr(found, n)
except AttributeError:
__import__(used)
found = getattr(found, n)
return found
The line "used = used + '.' + n" could of course be improved.
|
|||
| msg147597 - (view) | Author: Roundup Robot (python-dev) (Python triager) | Date: 2011年11月14日 14:24 | |
New changeset 5df1065ddb8b by Éric Araujo in branch 'default': Expand tests and fix bugs in util.resolve_name. http://hg.python.org/distutils2/rev/5df1065ddb8b |
|||
| msg178625 - (view) | Author: Berker Peksag (berker.peksag) * (Python committer) | Date: 2012年12月30日 23:05 | |
Attaching new patch to address Vinay Sajip's suggestions on Rietveld. Thank you, Vinay. |
|||
| msg182938 - (view) | Author: Alyssa Coghlan (ncoghlan) * (Python committer) | Date: 2013年02月25日 14:01 | |
Something to consider with these functions: it is probably desirable to also support the popular alternate notation which uses an explicit ":" to separate the module name from the reference within the module.
For example, setuptools entry points and nose test references both use that format.
The alternate notation should be fairly easy to both detect and handle through "name.partition(':')"
|
|||
| msg183109 - (view) | Author: Ezio Melotti (ezio.melotti) * (Python committer) | Date: 2013年02月27日 03:58 | |
Can you provide a couple of examples of that notation? |
|||
| msg183115 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2013年02月27日 06:36 | |
> Can you provide a couple of examples of that notation? entry_points = """ [console_scripts] pybabel = babel.messages.frontend:main [distutils.commands] compile_catalog = babel.messages.frontend:compile_catalog extract_messages = babel.messages.frontend:extract_messages init_catalog = babel.messages.frontend:init_catalog update_catalog = babel.messages.frontend:update_catalog [distutils.setup_keywords] message_extractors = babel.messages.frontend:check_message_extractors [babel.checkers] num_plurals = babel.messages.checkers:num_plurals python_format = babel.messages.checkers:python_format [babel.extractors] ignore = babel.messages.extract:extract_nothing python = babel.messages.extract:extract_python javascript = babel.messages.extract:extract_javascript """ Source: http://babel.edgewall.org/browser/trunk/setup.py#L38 |
|||
| msg209878 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2014年02月01日 08:31 | |
Another question to consider: is inspect the best place for this? I don't think it is, because (a) It's not really an inspection facility (b) Importing inspect to get this functionality would pull in lots of stuff which wouldn't be used in the typical use case. I think it makes more sense for it to be in importlib. Accordingly adding Brett to nosy, for his thoughts. |
|||
| msg209893 - (view) | Author: Brett Cannon (brett.cannon) * (Python committer) | Date: 2014年02月01日 15:45 | |
importlib.util.resolve_name() already exists for resolving an explicit relative import name to an absolute one, so closing this as out of date. |
|||
| msg209895 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2014年02月01日 16:19 | |
> importlib.util.resolve_name() already exists But that's not what the proposed functionality is for, is it? This covers finding values inside an imported module which can be accessed via a dotted path from the module globals. |
|||
| msg209902 - (view) | Author: Brett Cannon (brett.cannon) * (Python committer) | Date: 2014年02月01日 17:32 | |
Importlib already has importlib.import_module() (since Python 2.7) and that's as far as I'm willing to go for finding a module by name. Anything past that is a getarr() call on the resulting module and thus not worth adding to importlib. |
|||
| msg209903 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2014年02月01日 17:58 | |
> and thus not worth adding to importlib. Okay, fair enough. It's not purely an import function, though partly related to imports. |
|||
| msg360664 - (view) | Author: Éric Araujo (eric.araujo) * (Python committer) | Date: 2020年01月25日 00:14 | |
With the lack of support, I suggest closing this. |
|||
| msg360681 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2020年01月25日 11:48 | |
> With the lack of support, I suggest closing this. Well, the lack of support is just for adding to importlib. It still seems useful to refactor it out ... just not clear where the best place is. Perhaps pkgutil? It's definitely useful to have in the stdlib in one place. I'll come up with a PR soon. |
|||
| msg360710 - (view) | Author: Berker Peksag (berker.peksag) * (Python committer) | Date: 2020年01月25日 20:48 | |
FTR, Django has a similar helper:
from django.utils.module_loading import import_string
ValidationError = import_string('django.core.exceptions.ValidationError')
https://docs.djangoproject.com/en/3.0/ref/utils/#django.utils.module_loading.import_string
|
|||
| msg362000 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2020年02月14日 22:02 | |
New changeset 1ed61617a4a6632905ad6a0b440cd2cafb8b6414 by Vinay Sajip in branch 'master': bpo-12915: Add pkgutil.resolve_name (GH-18310) https://github.com/python/cpython/commit/1ed61617a4a6632905ad6a0b440cd2cafb8b6414 |
|||
| msg362007 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2020年02月15日 10:02 | |
The regex looks too strict. Python 3 accepts non-ASCII module names. Should existing functions be modified to reuse the new function? |
|||
| msg362022 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2020年02月15日 17:23 | |
> The regex looks too strict. Python 3 accepts non-ASCII module names. Would adding the re.U flag be enough, or are you thinking of other possibilities? |
|||
| msg362023 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2020年02月15日 17:33 | |
To be more precise, one could use ^(?!\d)(\w+)(\.(?!\d)(\w+))* for _DOTTED_WORDS and use the re.U flag when compiling the pattern. That should match any packages where segments start with a letter and continue with alphanumerics. Do you agree? |
|||
| msg362132 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2020年02月17日 09:46 | |
I reopen the issue to discuss the non-ASCII identifiers.
Currently, the code uses [a-z_]\w*, but "é" is a valid Python module name for example:
$ echo 'print("here")' > é.py
$ python3 -c 'import é'
here
I'm not sure how to design the regex. I just reported a potential issue ;-) See the PEP 3131 and str.isidentifier() method:
https://docs.python.org/dev/library/stdtypes.html#str.isidentifier
|
|||
| msg362161 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2020年02月17日 23:05 | |
> I'm not sure how to design the regex. Did you not look at the PR I added to address this (Unicode chars), with a regex change? I added you as a reviewer. |
|||
| msg362881 - (view) | Author: Vinay Sajip (vinay.sajip) * (Python committer) | Date: 2020年02月28日 14:26 | |
New changeset 4f17c5cd9a1ec50fe8de7ef68c39220a01a862cb by Vinay Sajip in branch 'master': bpo-12915: Improve Unicode support for package names and attributes. (GH-18517) https://github.com/python/cpython/commit/4f17c5cd9a1ec50fe8de7ef68c39220a01a862cb |
|||
| msg362987 - (view) | Author: Michael Felt (Michael.Felt) * | Date: 2020年02月29日 16:27 | |
PR18517 has, likely, a utf-8 dependency. AIX, default latin-1 does not accept the new test. Starting with this merge AIX bot fails with: ====================================================================== ERROR: test_name_resolution (test.test_pkgutil.PkgutilTests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/buildbot/buildarea/3.x.aixtools-aix-power6/build/Lib/test/test_pkgutil.py", line 249, in test_name_resolution os.makedirs(d, exist_ok=True) File "/home/buildbot/buildarea/3.x.aixtools-aix-power6/build/Lib/os.py", line 223, in makedirs mkdir(name, mode) UnicodeEncodeError: 'latin-1' codec can't encode characters in position 17-19: ordinal not in range(256) Not completely resolved. |
|||
| msg363023 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2020年02月29日 22:35 | |
> File "/home/buildbot/buildarea/3.x.aixtools-aix-power6/build/Lib/test/test_pkgutil.py", line 249, in test_name_resolution > os.makedirs(d, exist_ok=True) (...) > UnicodeEncodeError: 'latin-1' codec can't encode characters in position 17-19: ordinal not in range(256) The test should be modified to skip the current uw value in the loop ("continue") if the filesystem encoding cannot encode the Unicode string (catch UnicodeEncodeError on the makedirs() call). |
|||
| msg363044 - (view) | Author: Michael Felt (Michael.Felt) * | Date: 2020年03月01日 07:12 | |
I am very busy with normal work this week. However I’ll attempt to add a pr with your (Victor"s) suggestion. Sent from my iPhone > On 29 Feb 2020, at 23:36, STINNER Victor <report@bugs.python.org> wrote: > > > STINNER Victor <vstinner@python.org> added the comment: > >> File "/home/buildbot/buildarea/3.x.aixtools-aix-power6/build/Lib/test/test_pkgutil.py", line 249, in test_name_resolution >> os.makedirs(d, exist_ok=True) > (...) >> UnicodeEncodeError: 'latin-1' codec can't encode characters in position 17-19: ordinal not in range(256) > > The test should be modified to skip the current uw value in the loop ("continue") if the filesystem encoding cannot encode the Unicode string (catch UnicodeEncodeError on the makedirs() call). > > ---------- > > _______________________________________ > Python tracker <report@bugs.python.org> > <https://bugs.python.org/issue12915> > _______________________________________ > |
|||
| msg363250 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2020年03月03日 10:11 | |
New changeset e0acec15412203359d59db33e447698ee51e07fc by Michael Felt in branch 'master': bpo-12915: Skip test_pkgutil.test_name_resolution() non-encodable filenames (GH-18720) https://github.com/python/cpython/commit/e0acec15412203359d59db33e447698ee51e07fc |
|||
| msg363251 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2020年03月03日 10:12 | |
I understand that all known issues are now fixed, so I close the issue. |
|||
| msg372552 - (view) | Author: Ned Deily (ned.deily) * (Python committer) | Date: 2020年06月29日 07:26 | |
See bpo-41154 for possible regression introduced by these changes. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:21 | admin | set | github: 57124 |
| 2020年12月31日 14:16:33 | RJ722 | set | nosy:
+ RJ722 |
| 2020年06月29日 10:35:17 | vstinner | set | nosy:
- vstinner |
| 2020年06月29日 07:26:34 | ned.deily | set | nosy:
+ ned.deily messages: + msg372552 |
| 2020年03月03日 10:12:12 | vstinner | set | status: open -> closed resolution: fixed messages: + msg363251 stage: patch review -> resolved |
| 2020年03月03日 10:11:21 | vstinner | set | messages: + msg363250 |
| 2020年03月01日 14:25:35 | Michael.Felt | set | stage: resolved -> patch review pull_requests: + pull_request18078 |
| 2020年03月01日 07:12:09 | Michael.Felt | set | messages: + msg363044 |
| 2020年02月29日 22:35:39 | vstinner | set | messages: + msg363023 |
| 2020年02月29日 16:27:53 | Michael.Felt | set | nosy:
+ Michael.Felt messages: + msg362987 |
| 2020年02月28日 14:26:49 | vinay.sajip | set | messages: + msg362881 |
| 2020年02月17日 23:05:18 | vinay.sajip | set | messages: + msg362161 |
| 2020年02月17日 09:46:33 | vstinner | set | status: closed -> open resolution: fixed -> (no value) messages: + msg362132 |
| 2020年02月15日 21:48:15 | vinay.sajip | set | pull_requests: + pull_request17895 |
| 2020年02月15日 17:33:13 | vinay.sajip | set | messages: + msg362023 |
| 2020年02月15日 17:23:21 | vinay.sajip | set | messages: + msg362022 |
| 2020年02月15日 10:02:24 | vstinner | set | nosy:
+ vstinner messages: + msg362007 |
| 2020年02月14日 22:07:09 | vinay.sajip | set | status: open -> closed stage: patch review -> resolved resolution: fixed versions: + Python 3.9, - Python 3.5 |
| 2020年02月14日 22:02:21 | vinay.sajip | set | messages: + msg362000 |
| 2020年02月02日 10:46:34 | vinay.sajip | set | pull_requests: + pull_request17686 |
| 2020年01月25日 20:48:50 | berker.peksag | set | messages: + msg360710 |
| 2020年01月25日 11:48:44 | vinay.sajip | set | messages: + msg360681 |
| 2020年01月25日 00:14:07 | eric.araujo | set | messages: + msg360664 |
| 2020年01月24日 23:32:27 | brett.cannon | set | nosy:
- brett.cannon |
| 2014年02月01日 17:58:05 | vinay.sajip | set | messages: + msg209903 |
| 2014年02月01日 17:32:30 | brett.cannon | set | nosy:
brett.cannon, terry.reedy, vinay.sajip, ncoghlan, ezio.melotti, eric.araujo, Trundle, meador.inge, daniel.urban, python-dev, eric.snow, berker.peksag, yselivanov messages: + msg209902 |
| 2014年02月01日 16:19:23 | vinay.sajip | set | status: closed -> open resolution: out of date -> (no value) messages: + msg209895 |
| 2014年02月01日 15:45:57 | brett.cannon | set | status: open -> closed resolution: out of date messages: + msg209893 |
| 2014年02月01日 08:31:17 | vinay.sajip | set | nosy:
+ brett.cannon messages: + msg209878 |
| 2014年01月31日 23:07:01 | yselivanov | set | nosy:
+ yselivanov versions: + Python 3.5, - Python 3.4 |
| 2013年02月27日 06:36:21 | vinay.sajip | set | messages: + msg183115 |
| 2013年02月27日 03:58:42 | ezio.melotti | set | messages: + msg183109 |
| 2013年02月25日 14:01:54 | ncoghlan | set | nosy:
+ ncoghlan messages: + msg182938 |
| 2013年01月01日 03:28:22 | meador.inge | set | nosy:
+ meador.inge |
| 2012年12月30日 23:05:28 | berker.peksag | set | files:
+ issue12915_v2.diff messages: + msg178625 |
| 2012年12月10日 14:48:57 | daniel.urban | set | stage: test needed -> patch review |
| 2012年12月10日 13:48:06 | berker.peksag | set | files:
+ issue12915.diff nosy: + berker.peksag keywords: + patch versions: + Python 3.4, - Python 3.3 |
| 2011年11月14日 14:24:20 | python-dev | set | messages: + msg147597 |
| 2011年10月20日 15:10:16 | vinay.sajip | set | nosy:
+ vinay.sajip messages: + msg146020 |
| 2011年10月19日 19:41:48 | eric.snow | set | nosy:
+ eric.snow |
| 2011年10月19日 19:32:55 | python-dev | set | nosy:
+ python-dev messages: + msg145941 |
| 2011年09月13日 12:26:01 | eric.araujo | set | messages: + msg143960 |
| 2011年09月12日 15:50:00 | eric.araujo | set | messages: + msg143911 |
| 2011年09月09日 19:41:57 | terry.reedy | set | nosy:
+ terry.reedy messages: + msg143796 stage: test needed |
| 2011年09月09日 16:32:42 | daniel.urban | set | nosy:
+ daniel.urban |
| 2011年09月08日 16:22:03 | Trundle | set | nosy:
+ Trundle |
| 2011年09月08日 10:25:32 | ezio.melotti | set | nosy:
+ ezio.melotti |
| 2011年09月06日 16:30:32 | eric.araujo | create | |