Skip to content

Commit 4aeea1a

Browse files
Victor's review
1 parent dcda91a commit 4aeea1a

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

Modules/zlibmodule.c

Lines changed: 22 additions & 34 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

@@ -2137,29 +2159,6 @@ zlib_free(void *mod)
21372159
zlib_clear((PyObject *)mod);
21382160
}
21392161

2140-
static PyObject *
2141-
zlib_getattr(PyObject *self, PyObject *args)
2142-
{
2143-
PyObject *name;
2144-
if (!PyArg_UnpackTuple(args, "__getattr__", 1, 1, &name)) {
2145-
return NULL;
2146-
}
2147-
2148-
if (PyUnicode_Check(name)) {
2149-
const char *name_str = PyUnicode_AsUTF8(name);
2150-
if (name_str && strcmp(name_str, "__version__") == 0) {
2151-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
2152-
"'__version__' is deprecated and slated for removal in Python 3.20",
2153-
1) < 0) {
2154-
return NULL;
2155-
}
2156-
return PyUnicode_FromString("1.0");
2157-
}
2158-
}
2159-
2160-
PyErr_Format(PyExc_AttributeError, "module 'zlib' has no attribute %R", name);
2161-
return NULL;
2162-
}
21632162

21642163
static int
21652164
zlib_exec(PyObject *mod)
@@ -2245,17 +2244,6 @@ zlib_exec(PyObject *mod)
22452244
return -1;
22462245
}
22472246
#endif
2248-
static PyMethodDef getattr_method = {"__getattr__", zlib_getattr, METH_VARARGS, "Module __getattr__"};
2249-
PyObject *getattr_func = PyCFunction_New(&getattr_method, mod);
2250-
if (getattr_func == NULL) {
2251-
return -1;
2252-
}
2253-
2254-
if (PyModule_AddObjectRef(mod, "__getattr__", getattr_func) < 0) {
2255-
Py_DECREF(getattr_func);
2256-
return -1;
2257-
}
2258-
Py_DECREF(getattr_func);
22592247
return 0;
22602248
}
22612249

0 commit comments

Comments
 (0)