forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 3
Add warning for implicit relative imports issues #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Eddy114514
wants to merge
4
commits into
softdevteam:migration
Choose a base branch
from
Eddy114514:import_improve
base: migration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2236,6 +2236,10 @@ static int mark_miss(char *name); | |
| static int ensure_fromlist(PyObject *mod, PyObject *fromlist, | ||
| char *buf, Py_ssize_t buflen, int recursive); | ||
| static PyObject * import_submodule(PyObject *mod, char *name, char *fullname); | ||
| static int warn_implicit_relative_sibling(PyObject *module, | ||
| const char *imported_name, | ||
| const char *package_name, | ||
| int from_import); | ||
|
|
||
| /* The Magnum Opus of dotted-name import :-) */ | ||
|
|
||
|
|
@@ -2246,6 +2250,10 @@ import_module_level(char *name, PyObject *globals, PyObject *locals, | |
| char *buf; | ||
| Py_ssize_t buflen = 0; | ||
| PyObject *parent, *head, *next, *tail; | ||
| int from_import = 0; | ||
| int warn_if_relative_sibling = 0; | ||
| char package_name[MAXPATHLEN + 1]; | ||
| char imported_name[MAXPATHLEN + 1]; | ||
|
|
||
| if (strchr(name, '/') != NULL | ||
| #ifdef MS_WINDOWS | ||
|
|
@@ -2265,6 +2273,13 @@ import_module_level(char *name, PyObject *globals, PyObject *locals, | |
| if (parent == NULL) | ||
| goto error_exit; | ||
|
|
||
| if (level < 0 && parent != Py_None && strchr(name, '.') == NULL && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How could |
||
| strlen(name) < sizeof(imported_name)) { | ||
| strcpy(package_name, buf); | ||
| strcpy(imported_name, name); | ||
| warn_if_relative_sibling = 1; | ||
| } | ||
|
|
||
| Py_INCREF(parent); | ||
| head = load_next(parent, level < 0 ? Py_None : parent, &name, buf, | ||
| &buflen); | ||
|
|
@@ -2303,6 +2318,16 @@ import_module_level(char *name, PyObject *globals, PyObject *locals, | |
| } | ||
| if (!b) | ||
| fromlist = NULL; | ||
| else | ||
| from_import = 1; | ||
| } | ||
|
|
||
| if (warn_if_relative_sibling && | ||
| warn_implicit_relative_sibling(head, imported_name, | ||
| package_name, from_import) < 0) { | ||
| Py_DECREF(tail); | ||
| Py_DECREF(head); | ||
| goto error_exit; | ||
| } | ||
|
|
||
| if (fromlist == NULL) { | ||
|
|
@@ -2325,6 +2350,66 @@ import_module_level(char *name, PyObject *globals, PyObject *locals, | |
| return NULL; | ||
| } | ||
|
|
||
| static int | ||
| warn_implicit_relative_sibling(PyObject *module, const char *imported_name, | ||
| const char *package_name, int from_import) | ||
| { | ||
| PyObject *msg = NULL; | ||
| PyObject *fix = NULL; | ||
| const char *resolved_name; | ||
| size_t package_name_len; | ||
| int result; | ||
|
|
||
| if (!Py_Py3kWarningFlag || module == NULL || !PyModule_Check(module)) | ||
| return 0; | ||
| if (imported_name == NULL || package_name == NULL) | ||
| return 0; | ||
|
|
||
| resolved_name = PyModule_GetName(module); | ||
| if (resolved_name == NULL) { | ||
| PyErr_Clear(); | ||
| return 0; | ||
| } | ||
|
|
||
| package_name_len = strlen(package_name); | ||
| if (strncmp(resolved_name, package_name, package_name_len) != 0 || | ||
| resolved_name[package_name_len] != '.' || | ||
| strcmp(resolved_name + package_name_len + 1, imported_name) != 0) | ||
| return 0; | ||
|
|
||
| if (from_import) { | ||
| msg = PyString_FromFormat( | ||
| "implicit relative import from '%.200s' resolved to package sibling '%.200s'; " | ||
| "in 3.x imports are absolute by default and this will resolve differently or fail", | ||
| imported_name, resolved_name); | ||
| fix = PyString_FromFormat( | ||
| "use 'from .%.200s import ...' if the package sibling is intended", | ||
| imported_name); | ||
| } else { | ||
| msg = PyString_FromFormat( | ||
| "implicit relative import of '%.200s' resolved to package sibling '%.200s'; " | ||
| "in 3.x imports are absolute by default and this will resolve differently or fail", | ||
| imported_name, resolved_name); | ||
| fix = PyString_FromFormat( | ||
| "use 'from . import %.200s' if the package sibling is intended", | ||
| imported_name); | ||
| } | ||
| if (msg == NULL || fix == NULL) | ||
| goto error; | ||
|
|
||
| result = PyErr_WarnEx_WithFix(PyExc_DeprecationWarning, | ||
| PyString_AsString(msg), | ||
| PyString_AsString(fix), 1); | ||
| Py_DECREF(msg); | ||
| Py_DECREF(fix); | ||
| return result; | ||
|
|
||
| error: | ||
| Py_XDECREF(msg); | ||
| Py_XDECREF(fix); | ||
| return -1; | ||
| } | ||
|
|
||
| PyObject * | ||
| PyImport_ImportModuleLevel(char *name, PyObject *globals, PyObject *locals, | ||
| PyObject *fromlist, int level) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are package names allowed to be
MAXPATHLEN + 1normally in CPython?