Skip to content

Commit 46f11b3

Browse files
gh-76007: Deprecate zlib.__version__ attribute (#140130)
1 parent 6416e6e commit 46f11b3

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

Doc/deprecations/pending-removal-in-3.20.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ Pending removal in Python 3.20
1919
- :mod:`tabnanny`
2020
- :mod:`tkinter.font`
2121
- :mod:`tkinter.ttk`
22+
- :mod:`zlib`
2223

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

Doc/whatsnew/3.15.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,9 @@ New deprecations
828828
- :mod:`tabnanny`
829829
- :mod:`tkinter.font`
830830
- :mod:`tkinter.ttk`
831+
- :mod:`zlib`
831832

832-
(Contributed by Hugo van Kemenade in :gh:`76007`.)
833+
(Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.)
833834

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

Lib/test/test_zlib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,5 +1222,15 @@ def __index__(self):
12221222
return 100
12231223

12241224

1225+
class TestModule(unittest.TestCase):
1226+
def test_deprecated__version__(self):
1227+
with self.assertWarnsRegex(
1228+
DeprecationWarning,
1229+
"'__version__' is deprecated and slated for removal in Python 3.20",
1230+
) as cm:
1231+
getattr(zlib, "__version__")
1232+
self.assertEqual(cm.filename, __file__)
1233+
1234+
12251235
if __name__ == "__main__":
12261236
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`zlib`: Deprecate ``__version__`` and schedule for removal in Python
2+
3.20.

Modules/zlibmodule.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,27 @@ zlib_crc32_combine_impl(PyObject *module, unsigned int crc1,
20152015
return crc32_combine(crc1, crc2, len);
20162016
}
20172017

2018+
static PyObject *
2019+
zlib_getattr(PyObject *self, PyObject *args)
2020+
{
2021+
PyObject *name;
2022+
if (!PyArg_UnpackTuple(args, "__getattr__", 1, 1, &name)) {
2023+
return NULL;
2024+
}
2025+
2026+
if (PyUnicode_Check(name) && PyUnicode_EqualToUTF8(name, "__version__")) {
2027+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2028+
"'__version__' is deprecated and slated for removal in Python 3.20",
2029+
1) < 0) {
2030+
return NULL;
2031+
}
2032+
return PyUnicode_FromString("1.0");
2033+
}
2034+
2035+
PyErr_Format(PyExc_AttributeError, "module 'zlib' has no attribute %R", name);
2036+
return NULL;
2037+
}
2038+
20182039
static PyMethodDef zlib_methods[] =
20192040
{
20202041
ZLIB_ADLER32_METHODDEF
@@ -2025,6 +2046,7 @@ static PyMethodDef zlib_methods[] =
20252046
ZLIB_CRC32_COMBINE_METHODDEF
20262047
ZLIB_DECOMPRESS_METHODDEF
20272048
ZLIB_DECOMPRESSOBJ_METHODDEF
2049+
{"__getattr__", zlib_getattr, METH_VARARGS, "Module __getattr__"},
20282050
{NULL, NULL}
20292051
};
20302052

@@ -2221,9 +2243,6 @@ zlib_exec(PyObject *mod)
22212243
return -1;
22222244
}
22232245
#endif
2224-
if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
2225-
return -1;
2226-
}
22272246
return 0;
22282247
}
22292248

0 commit comments

Comments
 (0)