Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(180982)

Side by Side Diff: Python/ceval.c

Issue 23447: Import fails when doing a circular import involving an `import *`
Patch Set: Created 3 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_import/__init__.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* Execute compiled code */ 2 /* Execute compiled code */
3 3
4 /* XXX TO DO: 4 /* XXX TO DO:
5 XXX speed up searching for keywords by using a dictionary 5 XXX speed up searching for keywords by using a dictionary
6 XXX document it! 6 XXX document it!
7 */ 7 */
8 8
9 /* enable more aggressive intra-module optimizations, where available */ 9 /* enable more aggressive intra-module optimizations, where available */
10 #define PY_LOCAL_AGGRESSIVE 10 #define PY_LOCAL_AGGRESSIVE
(...skipping 5092 matching lines...) Expand 10 before | Expand all | Expand 10 after
5103 error: 5103 error:
5104 PyErr_Format(PyExc_ImportError, "cannot import name %R", name); 5104 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5105 return NULL; 5105 return NULL;
5106 } 5106 }
5107 5107
5108 static int 5108 static int
5109 import_all_from(PyObject *locals, PyObject *v) 5109 import_all_from(PyObject *locals, PyObject *v)
5110 { 5110 {
5111 _Py_IDENTIFIER(__all__); 5111 _Py_IDENTIFIER(__all__);
5112 _Py_IDENTIFIER(__dict__); 5112 _Py_IDENTIFIER(__dict__);
5113 _Py_IDENTIFIER(__name__);
5113 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); 5114 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
5114 PyObject *dict, *name, *value; 5115 PyObject *dict, *name, *value;
5116 PyObject *fullmodname, *pkgname;
5115 int skip_leading_underscores = 0; 5117 int skip_leading_underscores = 0;
5116 int pos, err; 5118 int pos, err;
5117 5119
5118 if (all == NULL) { 5120 if (all == NULL) {
5119 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) 5121 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5120 return -1; /* Unexpected error */ 5122 return -1; /* Unexpected error */
5121 PyErr_Clear(); 5123 PyErr_Clear();
5122 dict = _PyObject_GetAttrId(v, &PyId___dict__); 5124 dict = _PyObject_GetAttrId(v, &PyId___dict__);
5123 if (dict == NULL) { 5125 if (dict == NULL) {
5124 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) 5126 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
(...skipping 20 matching lines...) Expand all
5145 } 5147 }
5146 if (skip_leading_underscores && 5148 if (skip_leading_underscores &&
5147 PyUnicode_Check(name) && 5149 PyUnicode_Check(name) &&
5148 PyUnicode_READY(name) != -1 && 5150 PyUnicode_READY(name) != -1 &&
5149 PyUnicode_READ_CHAR(name, 0) == '_') 5151 PyUnicode_READ_CHAR(name, 0) == '_')
5150 { 5152 {
5151 Py_DECREF(name); 5153 Py_DECREF(name);
5152 continue; 5154 continue;
5153 } 5155 }
5154 value = PyObject_GetAttr(v, name); 5156 value = PyObject_GetAttr(v, name);
5155 if (value == NULL) 5157 if (value == NULL && PyErr_ExceptionMatches(PyExc_AttributeError))
5158 {
5159 /* Issue #23477: if a circular import happens within a wildcard
5160 import, some partially loaded modules may not have been saved
5161 to the package's __dict__ yet, so we'll try getting them from
5162 sys.modules before giving up.
5163 */
5164 PyErr_Clear();
5165 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
5166 if (pkgname != NULL && PyUnicode_Check(pkgname))
5167 {
5168 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5169 if (fullmodname != NULL)
5170 {
5171 value = PyDict_GetItem(PyImport_GetModuleDict(),
5172 fullmodname);
5173 Py_XINCREF(value);
5174 Py_DECREF(fullmodname);
5175 }
5176 Py_DECREF(pkgname);
5177 }
5178 }
5179 if (value == NULL) {
5180 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5156 err = -1; 5181 err = -1;
5182 }
5157 else if (PyDict_CheckExact(locals)) 5183 else if (PyDict_CheckExact(locals))
5158 err = PyDict_SetItem(locals, name, value); 5184 err = PyDict_SetItem(locals, name, value);
5159 else 5185 else
5160 err = PyObject_SetItem(locals, name, value); 5186 err = PyObject_SetItem(locals, name, value);
5161 Py_DECREF(name); 5187 Py_DECREF(name);
5162 Py_XDECREF(value); 5188 Py_XDECREF(value);
5163 if (err != 0) 5189 if (err != 0)
5164 break; 5190 break;
5165 } 5191 }
5166 Py_DECREF(all); 5192 Py_DECREF(all);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
5290 Py_DECREF(l); 5316 Py_DECREF(l);
5291 return NULL; 5317 return NULL;
5292 } 5318 }
5293 PyList_SetItem(l, i, x); 5319 PyList_SetItem(l, i, x);
5294 } 5320 }
5295 return l; 5321 return l;
5296 #endif 5322 #endif
5297 } 5323 }
5298 5324
5299 #endif 5325 #endif
OLDNEW
« no previous file with comments | « Lib/test/test_import/__init__.py ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+