Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-42327: Add PyModule_Add() (smaller). #23443

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

Merged
merged 12 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename PyModule_AddNew() back to PyModule_Add().
  • Loading branch information
serhiy-storchaka committed Jul 17, 2023
commit 5be0f6a6767701239034999da1d220add9d194c9
6 changes: 3 additions & 3 deletions 6 Doc/c-api/module.rst
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ state:
.. versionadded:: 3.10


.. c:function:: int PyModule_AddNew(PyObject *module, const char *name, PyObject *value)
.. c:function:: int PyModule_Add(PyObject *module, const char *name, PyObject *value)

Similar to :c:func:`PyModule_AddObjectRef`, but "steals" a reference
to *value*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyModule_AddObjectRef() must not be called with NULL. Can you please elaborate the behavior when value is NULL? Something like:

If value is NULL, just return -1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyModule_AddObjectRef() can be called with NULL if an exception is set. Actually, some of my fixes which use PyModule_AddObjectRef() rely on this.

Expand All @@ -495,7 +495,7 @@ state:

Example usage::

if (PyModule_AddNew(module, "spam", PyBytes_FromString(value)) < 0) {
if (PyModule_Add(module, "spam", PyBytes_FromString(value)) < 0) {
goto error;
}

Expand All @@ -507,7 +507,7 @@ state:
Similar to :c:func:`PyModule_AddObjectRef`, but steals a reference to
*value* on success (if it returns ``0``).

The new :c:func:`PyModule_AddNew` or :c:func:`PyModule_AddObjectRef`
The new :c:func:`PyModule_Add` or :c:func:`PyModule_AddObjectRef`
functions are recommended, since it is
easy to introduce reference leaks by misusing the
:c:func:`PyModule_AddObject` function.
Expand Down
2 changes: 1 addition & 1 deletion 2 Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 2 Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ New Features
If the assertion fails, make sure that the size is set before.
(Contributed by Victor Stinner in :gh:`106168`.)

* Add :c:func:`PyModule_AddNew` function: similar to
* Add :c:func:`PyModule_Add` function: similar to
:c:func:`PyModule_AddObjectRef` and :c:func:`PyModule_AddObject` but
always steals a reference to the value.
(Contributed by Serhiy Storchaka in :gh:`86493`.)
Expand Down
4 changes: 2 additions & 2 deletions 4 Include/modsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ PyAPI_FUNC(int) PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000
// Similar to PyModule_AddObjectRef() but steal a reference to 'value'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to add "and value can be NULL".

PyAPI_FUNC(int) PyModule_AddNew(PyObject *mod, const char *name, PyObject *value);
PyAPI_FUNC(int) PyModule_Add(PyObject *mod, const char *name, PyObject *value);
#endif /* Py_LIMITED_API */

// Similar to PyModule_AddObjectRef() and PyModule_AddNew() but steal
// Similar to PyModule_AddObjectRef() and PyModule_Add() but steal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not similar to PyModule_AddObjectRef(): value can be NULL.

Suggested change
// Similar to PyModule_AddObjectRef() and PyModule_Add() but steal
// Similar to PyModule_Add() but steal

// a reference to 'value' on success and only on success.
// Errorprone. Should not be used in new code.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's important important to put it in the documentation. This kind of definition fits perfectly with the Soft Deprecated status. Maybe also soft deprecate this API, similar to getopt and optparse soft deprecation. Example with getopt:

.. deprecated:: 3.13
   The :mod:`getopt` module is :term:`soft deprecated` and will not be
   developed further; development will continue with the :mod:`argparse`
   module.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you suggest to convert the note block into deprecated?

PyAPI_FUNC(int) PyModule_AddObject(PyObject *mod, const char *, PyObject *value);
Expand Down
2 changes: 1 addition & 1 deletion 2 Lib/test/test_stable_abi_ctypes.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add :func:`PyModule_AddNew` function: similar to :c:func:`PyModule_AddObjectRef` and :c:func:`PyModule_AddObject`, but always steals a reference to the value.
Add :func:`PyModule_Add` function: similar to :c:func:`PyModule_AddObjectRef` and :c:func:`PyModule_AddObject`, but always steals a reference to the value.
2 changes: 1 addition & 1 deletion 2 Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2444,5 +2444,5 @@
added = '3.13'
[function.PyMapping_GetOptionalItemString]
added = '3.13'
[function.PyModule_AddNew]
[function.PyModule_Add]
added = '3.13'
8 changes: 4 additions & 4 deletions 8 Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -6085,22 +6085,22 @@ sslmodule_init_versioninfo(PyObject *m)
*/
libver = OpenSSL_version_num();
r = PyLong_FromUnsignedLong(libver);
if (PyModule_AddNew(m, "OPENSSL_VERSION_NUMBER", r) < 0)
if (PyModule_Add(m, "OPENSSL_VERSION_NUMBER", r) < 0)
return -1;

parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
if (PyModule_AddNew(m, "OPENSSL_VERSION_INFO", r) < 0)
if (PyModule_Add(m, "OPENSSL_VERSION_INFO", r) < 0)
return -1;

r = PyUnicode_FromString(OpenSSL_version(OPENSSL_VERSION));
if (PyModule_AddNew(m, "OPENSSL_VERSION", r) < 0)
if (PyModule_Add(m, "OPENSSL_VERSION", r) < 0)
return -1;

libver = OPENSSL_VERSION_NUMBER;
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
if (PyModule_AddNew(m, "_OPENSSL_API_VERSION", r) < 0)
if (PyModule_Add(m, "_OPENSSL_API_VERSION", r) < 0)
return -1;

return 0;
Expand Down
6 changes: 3 additions & 3 deletions 6 Modules/_stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,15 @@ stat_exec(PyObject *module)
ADD_INT_MACRO(module, FILE_ATTRIBUTE_TEMPORARY);
ADD_INT_MACRO(module, FILE_ATTRIBUTE_VIRTUAL);

if (PyModule_AddNew(module, "IO_REPARSE_TAG_SYMLINK",
if (PyModule_Add(module, "IO_REPARSE_TAG_SYMLINK",
PyLong_FromUnsignedLong(IO_REPARSE_TAG_SYMLINK)) < 0) {
return -1;
}
if (PyModule_AddNew(module, "IO_REPARSE_TAG_MOUNT_POINT",
if (PyModule_Add(module, "IO_REPARSE_TAG_MOUNT_POINT",
PyLong_FromUnsignedLong(IO_REPARSE_TAG_MOUNT_POINT)) < 0) {
return -1;
}
if (PyModule_AddNew(module, "IO_REPARSE_TAG_APPEXECLINK",
if (PyModule_Add(module, "IO_REPARSE_TAG_APPEXECLINK",
PyLong_FromUnsignedLong(IO_REPARSE_TAG_APPEXECLINK)) < 0) {
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1493,12 +1493,12 @@ static PyMethodDef module_functions[] = {
static int
module_exec(PyObject *module)
{
if (PyModule_AddNew(module, "SIZEOF_PYGC_HEAD",
if (PyModule_Add(module, "SIZEOF_PYGC_HEAD",
PyLong_FromSsize_t(sizeof(PyGC_Head))) < 0) {
return 1;
}

if (PyModule_AddNew(module, "SIZEOF_TIME_T",
if (PyModule_Add(module, "SIZEOF_TIME_T",
PyLong_FromSsize_t(sizeof(time_t))) < 0) {
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion 2 Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ thread_module_exec(PyObject *module)
// Round towards minus infinity
timeout_max = floor(timeout_max);

if (PyModule_AddNew(module, "TIMEOUT_MAX",
if (PyModule_Add(module, "TIMEOUT_MAX",
PyFloat_FromDouble(timeout_max)) < 0) {
return -1;
}
Expand Down
14 changes: 7 additions & 7 deletions 14 Modules/cmathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1217,29 +1217,29 @@ static PyMethodDef cmath_methods[] = {
static int
cmath_exec(PyObject *mod)
{
if (PyModule_AddNew(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
if (PyModule_Add(mod, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
return -1;
}
if (PyModule_AddNew(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
if (PyModule_Add(mod, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
return -1;
}
// 2pi
if (PyModule_AddNew(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
if (PyModule_Add(mod, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
return -1;
}
if (PyModule_AddNew(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
if (PyModule_Add(mod, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
return -1;
}

Py_complex infj = {0.0, Py_INFINITY};
if (PyModule_AddNew(mod, "infj", PyComplex_FromCComplex(infj)) < 0) {
if (PyModule_Add(mod, "infj", PyComplex_FromCComplex(infj)) < 0) {
return -1;
}
if (PyModule_AddNew(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
if (PyModule_Add(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
return -1;
}
Py_complex nanj = {0.0, fabs(Py_NAN)};
if (PyModule_AddNew(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
if (PyModule_Add(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
return -1;
}

Expand Down
10 changes: 5 additions & 5 deletions 10 Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4037,20 +4037,20 @@ math_exec(PyObject *module)
if (state->str___trunc__ == NULL) {
return -1;
}
if (PyModule_AddNew(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
if (PyModule_Add(module, "pi", PyFloat_FromDouble(Py_MATH_PI)) < 0) {
return -1;
}
if (PyModule_AddNew(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
if (PyModule_Add(module, "e", PyFloat_FromDouble(Py_MATH_E)) < 0) {
return -1;
}
// 2pi
if (PyModule_AddNew(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
if (PyModule_Add(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
return -1;
}
if (PyModule_AddNew(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
if (PyModule_Add(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
return -1;
}
if (PyModule_AddNew(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
if (PyModule_Add(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
return -1;
}
return 0;
Expand Down
6 changes: 3 additions & 3 deletions 6 Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13466,7 +13466,7 @@ setup_confname_table(struct constdef *table, size_t tablesize,
}
Py_DECREF(o);
}
return PyModule_AddNew(module, tablename, d);
return PyModule_Add(module, tablename, d);
}

/* Return -1 on failure, 0 on success. */
Expand Down Expand Up @@ -16781,7 +16781,7 @@ posixmodule_exec(PyObject *m)
#endif

/* Initialize environ dictionary */
if (PyModule_AddNew(m, "environ", convertenviron()) != 0) {
if (PyModule_Add(m, "environ", convertenviron()) != 0) {
return -1;
}

Expand Down Expand Up @@ -16907,7 +16907,7 @@ posixmodule_exec(PyObject *m)
Py_DECREF(unicode);
}

return PyModule_AddNew(m, "_have_functions", list);
return PyModule_Add(m, "_have_functions", list);
}


Expand Down
4 changes: 2 additions & 2 deletions 4 Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ init_timezone(PyObject *m)
return -1;
}
#endif // MS_WINDOWS
if (PyModule_AddNew(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)) < 0) {
if (PyModule_Add(m, "tzname", Py_BuildValue("(NN)", otz0, otz1)) < 0) {
return -1;
}
#else // !HAVE_DECL_TZNAME
Expand Down Expand Up @@ -1835,7 +1835,7 @@ init_timezone(PyObject *m)
PyModule_AddIntConstant(m, "daylight", janzone != julyzone);
tzname_obj = Py_BuildValue("(zz)", janname, julyname);
}
if (PyModule_AddNew(m, "tzname", tzname_obj) < 0) {
if (PyModule_Add(m, "tzname", tzname_obj) < 0) {
return -1;
}
#endif // !HAVE_DECL_TZNAME
Expand Down
2 changes: 1 addition & 1 deletion 2 PC/python3dll.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions 6 Python/modsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
}

int
PyModule_AddNew(PyObject *mod, const char *name, PyObject *value)
PyModule_Add(PyObject *mod, const char *name, PyObject *value)
{
int res = PyModule_AddObjectRef(mod, name, value);
Py_XDECREF(value);
Expand All @@ -630,13 +630,13 @@ PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
int
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
return PyModule_AddNew(m, name, PyLong_FromLong(value));
return PyModule_Add(m, name, PyLong_FromLong(value));
}

int
PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
{
return PyModule_AddNew(m, name, PyUnicode_FromString(value));
return PyModule_Add(m, name, PyUnicode_FromString(value));
}

int
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.