| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6658e2c commit cd2ca74
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1255,15 +1255,23 @@ class Spec2: | |||
| 1255 | 1255 | ||
| 1256 | 1256 | def test_create_builtin(self): | |
| 1257 | 1257 | class Spec: | |
| 1258 | - name = None | ||
| 1258 | + pass | ||
| 1259 | 1259 | spec = Spec() | |
| 1260 | 1260 | ||
| 1261 | + spec.name = "sys" | ||
| 1262 | + self.assertIs(_imp.create_builtin(spec), sys) | ||
| 1263 | + | ||
| 1264 | + spec.name = None | ||
| 1261 | 1265 | with self.assertRaisesRegex(TypeError, 'name must be string, not NoneType'): | |
| 1262 | 1266 | _imp.create_builtin(spec) | |
| 1263 | 1267 | ||
| 1264 | - spec.name = "" | ||
| 1268 | + # gh-142029 | ||
| 1269 | + spec.name = "nonexistent_lib" | ||
| 1270 | + with self.assertRaises(ModuleNotFoundError): | ||
| 1271 | + _imp.create_builtin(spec) | ||
| 1265 | 1272 | ||
| 1266 | 1273 | # gh-142029 | |
| 1274 | + spec.name = "" | ||
| 1267 | 1275 | with self.assertRaisesRegex(ValueError, 'name must not be empty'): | |
| 1268 | 1276 | _imp.create_builtin(spec) | |
| 1269 | 1277 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Raise :exc:`ModuleNotFoundError` instead of crashing when a nonexistent module | ||
| 2 | + is used as a name in ``_imp.create_builtin()``. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2383,12 +2383,12 @@ is_builtin(PyObject *name) | |||
| 2383 | 2383 | return 0; | |
| 2384 | 2384 | } | |
| 2385 | 2385 | ||
| 2386 | - static PyModInitFunction | ||
| 2387 | - lookup_inittab_initfunc(const struct _Py_ext_module_loader_info* info) | ||
| 2386 | + static struct _inittab* | ||
| 2387 | + lookup_inittab_entry(const struct _Py_ext_module_loader_info* info) | ||
| 2388 | 2388 | { | |
| 2389 | 2389 | for (struct _inittab *p = INITTAB; p->name != NULL; p++) { | |
| 2390 | 2390 | if (_PyUnicode_EqualToASCIIString(info->name, p->name)) { | |
| 2391 | - return (PyModInitFunction)p->initfunc; | ||
| 2391 | + return p; | ||
| 2392 | 2392 | } | |
| 2393 | 2393 | } | |
| 2394 | 2394 | // not found | |
@@ -2430,16 +2430,28 @@ create_builtin( | |||
| 2430 | 2430 | _extensions_cache_delete(info.path, info.name); | |
| 2431 | 2431 | } | |
| 2432 | 2432 | ||
| 2433 | - PyModInitFunction p0 = initfunc; | ||
| 2434 | - if (p0 == NULL) { | ||
| 2435 | - p0 = lookup_inittab_initfunc(&info); | ||
| 2436 | - if (p0 == NULL) { | ||
| 2437 | - /* Cannot re-init internal module ("sys" or "builtins") */ | ||
| 2438 | - assert(is_core_module(tstate->interp, info.name, info.path)); | ||
| 2439 | - mod = import_add_module(tstate, info.name); | ||
| 2433 | + PyModInitFunction p0 = NULL; | ||
| 2434 | + if (initfunc == NULL) { | ||
| 2435 | + struct _inittab *entry = lookup_inittab_entry(&info); | ||
| 2436 | + if (entry == NULL) { | ||
| 2437 | + mod = NULL; | ||
| 2438 | + _PyErr_SetModuleNotFoundError(name); | ||
| 2440 | 2439 | goto finally; | |
| 2441 | 2440 | } | |
| 2441 | + | ||
| 2442 | + p0 = (PyModInitFunction)entry->initfunc; | ||
| 2442 | 2443 | } | |
| 2444 | + else { | ||
| 2445 | + p0 = initfunc; | ||
| 2446 | + } | ||
| 2447 | + | ||
| 2448 | + if (p0 == NULL) { | ||
| 2449 | + /* Cannot re-init internal module ("sys" or "builtins") */ | ||
| 2450 | + assert(is_core_module(tstate->interp, info.name, info.path)); | ||
| 2451 | + mod = import_add_module(tstate, info.name); | ||
| 2452 | + goto finally; | ||
| 2453 | + } | ||
| 2454 | + | ||
| 2443 | 2455 | ||
| 2444 | 2456 | #ifdef Py_GIL_DISABLED | |
| 2445 | 2457 | // This call (and the corresponding call to _PyImport_CheckGILForModule()) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments