Skip to content

Commit 074a510

Browse files
committed
Apply review feedback and CAPI WG decision
- use `PyImport_CreateModuleFromInitfunc` for the new API - no need to introduce `create_builtin_ex` - update `create_builtin` and all callers - update exception message
1 parent 403c7e2 commit 074a510

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

Include/cpython/import.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ PyAPI_FUNC(int) PyImport_ExtendInittab(struct _inittab *newtab);
1414
// extension modules directly from a spec and init function,
1515
// without needing to go through inittab
1616
PyAPI_FUNC(PyObject *)
17-
PyImport_CreateBuiltinFromSpecAndInitfunc(
17+
PyImport_CreateModuleFromInitfunc(
1818
PyObject *spec,
1919
PyObject* (*initfunc)(void)
2020
);

Python/import.c

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,18 +2368,15 @@ lookup_inittab_initfunc(const struct _Py_ext_module_loader_info* info)
23682368
struct _inittab *found = NULL;
23692369
for (struct _inittab *p = INITTAB; p->name != NULL; p++) {
23702370
if (_PyUnicode_EqualToASCIIString(info->name, p->name)) {
2371-
found = p;
2371+
return (PyModInitFunction)p->initfunc;
23722372
}
23732373
}
2374-
if (found == NULL) {
2375-
// not found
2376-
return NULL;
2377-
}
2378-
return (PyModInitFunction)found->initfunc;
2374+
// not found
2375+
return NULL;
23792376
}
23802377

23812378
static PyObject*
2382-
create_builtin_ex(
2379+
create_builtin(
23832380
PyThreadState *tstate, PyObject *name,
23842381
PyObject *spec,
23852382
PyModInitFunction initfunc)
@@ -2447,14 +2444,8 @@ create_builtin_ex(
24472444
return mod;
24482445
}
24492446

2450-
static PyObject*
2451-
create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
2452-
{
2453-
return create_builtin_ex(tstate, name, spec, NULL);
2454-
}
2455-
24562447
PyObject*
2457-
PyImport_CreateBuiltinFromSpecAndInitfunc(
2448+
PyImport_CreateModuleFromInitfunc(
24582449
PyObject *spec, PyObject* (*initfunc)(void))
24592450
{
24602451
PyThreadState *tstate = _PyThreadState_GET();
@@ -2466,13 +2457,12 @@ PyImport_CreateBuiltinFromSpecAndInitfunc(
24662457

24672458
if (!PyUnicode_Check(name)) {
24682459
PyErr_Format(PyExc_TypeError,
2469-
"name must be string, not %.200s",
2470-
Py_TYPE(name)->tp_name);
2460+
"spec name must be string, not %T", name);
24712461
Py_DECREF(name);
24722462
return NULL;
24732463
}
24742464

2475-
PyObject *mod = create_builtin_ex(tstate, name, spec, initfunc);
2465+
PyObject *mod = create_builtin(tstate, name, spec, initfunc);
24762466
Py_DECREF(name);
24772467
return mod;
24782468
}
@@ -3245,7 +3235,7 @@ bootstrap_imp(PyThreadState *tstate)
32453235
}
32463236

32473237
// Create the _imp module from its definition.
3248-
PyObject *mod = create_builtin(tstate, name, spec);
3238+
PyObject *mod = create_builtin(tstate, name, spec, NULL);
32493239
Py_CLEAR(name);
32503240
Py_DECREF(spec);
32513241
if (mod == NULL) {
@@ -4405,7 +4395,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
44054395
return NULL;
44064396
}
44074397

4408-
PyObject *mod = create_builtin(tstate, name, spec);
4398+
PyObject *mod = create_builtin(tstate, name, spec, NULL);
44094399
Py_DECREF(name);
44104400
return mod;
44114401
}

0 commit comments

Comments
 (0)