Skip to content
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
3 changes: 2 additions & 1 deletion Doc/deprecations/pending-removal-in-3.20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ Pending removal in Python 3.20
- :mod:`tabnanny`
- :mod:`tkinter.font`
- :mod:`tkinter.ttk`
- :mod:`zlib`

(Contributed by Hugo van Kemenade in :gh:`76007`.)
(Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.)
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,9 @@ New deprecations
- :mod:`tabnanny`
- :mod:`tkinter.font`
- :mod:`tkinter.ttk`
- :mod:`zlib`

(Contributed by Hugo van Kemenade in :gh:`76007`.)
(Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.)

.. Add deprecations above alphabetically, not here at the end.

Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,5 +1222,15 @@ def __index__(self):
return 100


class TestModule(unittest.TestCase):
def test_deprecated__version__(self):
with self.assertWarnsRegex(
DeprecationWarning,
"'__version__' is deprecated and slated for removal in Python 3.20",
) as cm:
getattr(zlib, "__version__")
self.assertEqual(cm.filename, __file__)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`zlib`: Deprecate ``__version__`` and schedule for removal in Python
3.20.
34 changes: 33 additions & 1 deletion Modules/zlibmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,30 @@ zlib_free(void *mod)
zlib_clear((PyObject *)mod);
}

static PyObject *
zlib_getattr(PyObject *self, PyObject *args)
{
PyObject *name;
if (!PyArg_UnpackTuple(args, "__getattr__", 1, 1, &name)) {
return NULL;
}

if (PyUnicode_Check(name)) {
const char *name_str = PyUnicode_AsUTF8(name);
if (name_str && strcmp(name_str, "__version__") == 0) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"'__version__' is deprecated and slated for removal in Python 3.20",
1) < 0) {
return NULL;
}
return PyUnicode_FromString("1.0");
}
}

PyErr_Format(PyExc_AttributeError, "module 'zlib' has no attribute %R", name);
return NULL;
}

static int
zlib_exec(PyObject *mod)
{
Expand Down Expand Up @@ -2221,9 +2245,17 @@ zlib_exec(PyObject *mod)
return -1;
}
#endif
if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
static PyMethodDef getattr_method = {"__getattr__", zlib_getattr, METH_VARARGS, "Module __getattr__"};
PyObject *getattr_func = PyCFunction_New(&getattr_method, mod);
if (getattr_func == NULL) {
return -1;
}

if (PyModule_AddObjectRef(mod, "__getattr__", getattr_func) < 0) {
Py_DECREF(getattr_func);
return -1;
}
Py_DECREF(getattr_func);
return 0;
}

Expand Down
Loading