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

Delta Between Two Patch Sets: Python/ceval.c

Issue 23447: Import fails when doing a circular import involving an `import *`
Left Patch Set: Created 3 years, 8 months ago
Right Patch Set: Created 3 years, 6 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_import/__init__.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 5067 matching lines...) Expand 10 before | Expand all | Expand 10 after
5078 PyObject *fullmodname, *pkgname; 5078 PyObject *fullmodname, *pkgname;
5079 5079
5080 x = PyObject_GetAttr(v, name); 5080 x = PyObject_GetAttr(v, name);
5081 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError)) 5081 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5082 return x; 5082 return x;
5083 /* Issue #17636: in case this failed because of a circular relative 5083 /* Issue #17636: in case this failed because of a circular relative
5084 import, try to fallback on reading the module directly from 5084 import, try to fallback on reading the module directly from
5085 sys.modules. */ 5085 sys.modules. */
5086 PyErr_Clear(); 5086 PyErr_Clear();
5087 pkgname = _PyObject_GetAttrId(v, &PyId___name__); 5087 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
5088 if (pkgname == NULL) 5088 if (pkgname == NULL) {
5089 return NULL; 5089 goto error;
5090 }
5090 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); 5091 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5091 Py_DECREF(pkgname); 5092 Py_DECREF(pkgname);
5092 if (fullmodname == NULL) 5093 if (fullmodname == NULL) {
5093 return NULL; 5094 return NULL;
5095 }
5094 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname); 5096 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
5095 if (x == NULL)
5096 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5097 else
5098 Py_INCREF(x);
5099 Py_DECREF(fullmodname); 5097 Py_DECREF(fullmodname);
5098 if (x == NULL) {
5099 goto error;
5100 }
5101 Py_INCREF(x);
5100 return x; 5102 return x;
5103 error:
5104 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5105 return NULL;
5101 } 5106 }
5102 5107
5103 static int 5108 static int
5104 import_all_from(PyObject *locals, PyObject *v) 5109 import_all_from(PyObject *locals, PyObject *v)
5105 { 5110 {
5106 _Py_IDENTIFIER(__all__); 5111 _Py_IDENTIFIER(__all__);
5107 _Py_IDENTIFIER(__dict__); 5112 _Py_IDENTIFIER(__dict__);
5108 _Py_IDENTIFIER(__name__); 5113 _Py_IDENTIFIER(__name__);
5109 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__); 5114 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
5110 PyObject *dict, *name, *value; 5115 PyObject *dict, *name, *value;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
5144 PyUnicode_Check(name) && 5149 PyUnicode_Check(name) &&
5145 PyUnicode_READY(name) != -1 && 5150 PyUnicode_READY(name) != -1 &&
5146 PyUnicode_READ_CHAR(name, 0) == '_') 5151 PyUnicode_READ_CHAR(name, 0) == '_')
5147 { 5152 {
5148 Py_DECREF(name); 5153 Py_DECREF(name);
5149 continue; 5154 continue;
5150 } 5155 }
5151 value = PyObject_GetAttr(v, name); 5156 value = PyObject_GetAttr(v, name);
5152 if (value == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) 5157 if (value == NULL && PyErr_ExceptionMatches(PyExc_AttributeError))
5153 { 5158 {
5154 /* Issue #23477: if a circular import happens within a wildcard 5159 /* Issue #23477: if a circular import happens within a wildcard
5155 import, some partially loaded modules may not have been saved 5160 import, some partially loaded modules may not have been saved
5156 to the package's __dict__ yet, so we'll try getting them from 5161 to the package's __dict__ yet, so we'll try getting them from
5157 sys.modules before giving up. 5162 sys.modules before giving up.
5158 */ 5163 */
5159 PyErr_Clear(); 5164 PyErr_Clear();
5160 pkgname = _PyObject_GetAttrId(v, &PyId___name__); 5165 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
5161 if (pkgname != NULL) 5166 if (pkgname != NULL && PyUnicode_Check(pkgname))
5162 { 5167 {
5163 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name); 5168 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5164 if (fullmodname != NULL) 5169 if (fullmodname != NULL)
5165 { 5170 {
5166 value = PyDict_GetItem(PyImport_GetModuleDict(), 5171 value = PyDict_GetItem(PyImport_GetModuleDict(),
5167 fullmodname); 5172 fullmodname);
5168 if (value == NULL) 5173 Py_XINCREF(value);
5169 PyErr_Format(PyExc_ImportError,
5170 "cannot import name %R",
5171 name);
5172 else
5173 Py_INCREF(value);
5174 Py_DECREF(fullmodname); 5174 Py_DECREF(fullmodname);
5175 } 5175 }
5176 Py_DECREF(pkgname); 5176 Py_DECREF(pkgname);
5177 } 5177 }
5178 } 5178 }
5179 if (value == NULL) 5179 if (value == NULL) {
5180 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5180 err = -1; 5181 err = -1;
5182 }
5181 else if (PyDict_CheckExact(locals)) 5183 else if (PyDict_CheckExact(locals))
5182 err = PyDict_SetItem(locals, name, value); 5184 err = PyDict_SetItem(locals, name, value);
5183 else 5185 else
5184 err = PyObject_SetItem(locals, name, value); 5186 err = PyObject_SetItem(locals, name, value);
5185 Py_DECREF(name); 5187 Py_DECREF(name);
5186 Py_XDECREF(value); 5188 Py_XDECREF(value);
5187 if (err != 0) 5189 if (err != 0)
5188 break; 5190 break;
5189 } 5191 }
5190 Py_DECREF(all); 5192 Py_DECREF(all);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
5314 Py_DECREF(l); 5316 Py_DECREF(l);
5315 return NULL; 5317 return NULL;
5316 } 5318 }
5317 PyList_SetItem(l, i, x); 5319 PyList_SetItem(l, i, x);
5318 } 5320 }
5319 return l; 5321 return l;
5320 #endif 5322 #endif
5321 } 5323 }
5322 5324
5323 #endif 5325 #endif
LEFTRIGHT

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