Skip to content

Commit fce1c1c

Browse files
vstinnerGlyphack
authored andcommitted
pythongh-85283: Build md5 extension with limited C API (python#110967)
* Replace _Py_strhex() with few lines of code. * Replace _PyType_GetModuleState() with PyType_GetModuleState(). * Fix make check-c-globals.
1 parent 64cb622 commit fce1c1c

File tree

5 files changed

+28
-69
lines changed

5 files changed

+28
-69
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,9 @@ Build Changes
932932
* Building CPython now requires a compiler with support for the C11 atomic
933933
library, GCC built-in atomic functions, or MSVC interlocked intrinsics.
934934

935-
* The ``errno``, ``_ctypes_test``, ``_stat`` and ``_testimportmultiple`` C
936-
extensions are now built with the :ref:`limited C API <limited-c-api>`.
935+
* The ``errno``, ``md5``, ``_ctypes_test``, ``_stat`` and
936+
``_testimportmultiple`` C extensions are now built with the :ref:`limited C
937+
API <limited-c-api>`.
937938
(Contributed by Victor Stinner in :gh:`85283`.)
938939

939940

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
The ``errno``, ``_ctypes_test`` and ``_testimportmultiple`` C extensions are
2-
now built with the :ref:`limited C API <limited-c-api>`. Patch by Victor
3-
Stinner.
1+
The ``errno``, ``md5``, ``_ctypes_test`` and ``_testimportmultiple`` C
2+
extensions are now built with the :ref:`limited C API <limited-c-api>`. Patch
3+
by Victor Stinner.

Modules/clinic/md5module.c.h

Lines changed: 6 additions & 56 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/md5module.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
*/
1616

1717
/* MD5 objects */
18-
#ifndef Py_BUILD_CORE_BUILTIN
19-
# define Py_BUILD_CORE_MODULE 1
20-
#endif
18+
19+
// Need limited C API version 3.13 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
20+
#define Py_LIMITED_API 0x030d0000
2121

2222
#include "Python.h"
2323
#include "hashlib.h"
24-
#include "pycore_strhex.h" // _Py_strhex()
25-
#include "pycore_typeobject.h" // _PyType_GetModuleState()
2624

2725
/*[clinic input]
2826
module _md5
@@ -94,7 +92,7 @@ MD5_dealloc(MD5object *ptr)
9492
if (ptr->lock != NULL) {
9593
PyThread_free_lock(ptr->lock);
9694
}
97-
PyTypeObject *tp = Py_TYPE(ptr);
95+
PyTypeObject *tp = Py_TYPE((PyObject*)ptr);
9896
PyObject_GC_UnTrack(ptr);
9997
PyObject_GC_Del(ptr);
10098
Py_DECREF(tp);
@@ -115,7 +113,7 @@ static PyObject *
115113
MD5Type_copy_impl(MD5object *self, PyTypeObject *cls)
116114
/*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/
117115
{
118-
MD5State *st = _PyType_GetModuleState(cls);
116+
MD5State *st = PyType_GetModuleState(cls);
119117

120118
MD5object *newobj;
121119
if ((newobj = newMD5object(st))==NULL)
@@ -158,7 +156,16 @@ MD5Type_hexdigest_impl(MD5object *self)
158156
ENTER_HASHLIB(self);
159157
Hacl_Streaming_MD5_legacy_finish(self->hash_state, digest);
160158
LEAVE_HASHLIB(self);
161-
return _Py_strhex((const char*)digest, MD5_DIGESTSIZE);
159+
160+
const char *hexdigits = "0123456789abcdef";
161+
char digest_hex[MD5_DIGESTSIZE * 2];
162+
char *str = digest_hex;
163+
for (size_t i=0; i < MD5_DIGESTSIZE; i++) {
164+
unsigned char byte = digest[i];
165+
*str++ = hexdigits[byte >> 4];
166+
*str++ = hexdigits[byte & 0x0f];
167+
}
168+
return PyUnicode_FromStringAndSize(digest_hex, sizeof(digest_hex));
162169
}
163170

164171
static void update(Hacl_Streaming_MD5_state *state, uint8_t *buf, Py_ssize_t len) {

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,4 @@ Modules/_io/_iomodule.c - _PyIO_Module -
729729
Modules/_sqlite/module.c - _sqlite3module -
730730
Python/optimizer_analysis.c - _Py_PartitionRootNode_Type -
731731
Python/optimizer_analysis.c - _Py_UOpsAbstractInterpContext_Type -
732+
Modules/clinic/md5module.c.h _md5_md5 _keywords -

0 commit comments

Comments
 (0)