gh-76007: Deprecate zlib.__version__ attribute (#140130)

This commit is contained in:
Stan Ulbrych 2025-10-15 12:18:48 +01:00 committed by GitHub
parent 6416e6ebe5
commit 46f11b36ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 38 additions and 5 deletions

View file

@ -19,5 +19,6 @@ Pending removal in Python 3.20
- :mod:`tabnanny` - :mod:`tabnanny`
- :mod:`tkinter.font` - :mod:`tkinter.font`
- :mod:`tkinter.ttk` - :mod:`tkinter.ttk`
- :mod:`zlib`
(Contributed by Hugo van Kemenade in :gh:`76007`.) (Contributed by Hugo van Kemenade and Stan Ulbrych in :gh:`76007`.)

View file

@ -828,8 +828,9 @@ New deprecations
- :mod:`tabnanny` - :mod:`tabnanny`
- :mod:`tkinter.font` - :mod:`tkinter.font`
- :mod:`tkinter.ttk` - :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. .. Add deprecations above alphabetically, not here at the end.

View file

@ -1222,5 +1222,15 @@ def __index__(self):
return 100 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__": if __name__ == "__main__":
unittest.main() unittest.main()

View file

@ -0,0 +1,2 @@
:mod:`zlib`: Deprecate ``__version__`` and schedule for removal in Python
3.20.

View file

@ -2015,6 +2015,27 @@ zlib_crc32_combine_impl(PyObject *module, unsigned int crc1,
return crc32_combine(crc1, crc2, len); return crc32_combine(crc1, crc2, len);
} }
static PyObject *
zlib_getattr(PyObject *self, PyObject *args)
{
PyObject *name;
if (!PyArg_UnpackTuple(args, "__getattr__", 1, 1, &name)) {
return NULL;
}
if (PyUnicode_Check(name) && PyUnicode_EqualToUTF8(name, "__version__")) {
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 PyMethodDef zlib_methods[] = static PyMethodDef zlib_methods[] =
{ {
ZLIB_ADLER32_METHODDEF ZLIB_ADLER32_METHODDEF
@ -2025,6 +2046,7 @@ static PyMethodDef zlib_methods[] =
ZLIB_CRC32_COMBINE_METHODDEF ZLIB_CRC32_COMBINE_METHODDEF
ZLIB_DECOMPRESS_METHODDEF ZLIB_DECOMPRESS_METHODDEF
ZLIB_DECOMPRESSOBJ_METHODDEF ZLIB_DECOMPRESSOBJ_METHODDEF
{"__getattr__", zlib_getattr, METH_VARARGS, "Module __getattr__"},
{NULL, NULL} {NULL, NULL}
}; };
@ -2221,9 +2243,6 @@ zlib_exec(PyObject *mod)
return -1; return -1;
} }
#endif #endif
if (PyModule_AddStringConstant(mod, "__version__", "1.0") < 0) {
return -1;
}
return 0; return 0;
} }