Skip to content

Commit

Permalink
gh-85283: Build md5 extension with limited C API
Browse files Browse the repository at this point in the history
* Replace _Py_strhex() with few lines of code.
* Replace _PyType_GetModuleState() with PyType_GetModuleState().
* Fix make check-c-globals
  • Loading branch information
vstinner committed Oct 17, 2023
1 parent db15fc2 commit fcad07b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 69 deletions.
5 changes: 3 additions & 2 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,9 @@ Build Changes
* Building CPython now requires a compiler with support for the C11 atomic
library, GCC built-in atomic functions, or MSVC interlocked intrinsics.

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


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
The ``errno``, ``_ctypes_test`` and ``_testimportmultiple`` C extensions are
now built with the :ref:`limited C API <limited-c-api>`. Patch by Victor
Stinner.
The ``errno``, ``md5``, ``_ctypes_test`` and ``_testimportmultiple`` C
extensions are now built with the :ref:`limited C API <limited-c-api>`. Patch
by Victor Stinner.
62 changes: 6 additions & 56 deletions Modules/clinic/md5module.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions Modules/md5module.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/

/* MD5 objects */
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

// Need limited C API version 3.13 for Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
#define Py_LIMITED_API 0x030d0000

#include "Python.h"
#include "hashlib.h"
#include "pycore_strhex.h" // _Py_strhex()
#include "pycore_typeobject.h" // _PyType_GetModuleState()

/*[clinic input]
module _md5
Expand Down Expand Up @@ -94,7 +92,7 @@ MD5_dealloc(MD5object *ptr)
if (ptr->lock != NULL) {
PyThread_free_lock(ptr->lock);
}
PyTypeObject *tp = Py_TYPE(ptr);
PyTypeObject *tp = Py_TYPE((PyObject*)ptr);
PyObject_GC_UnTrack(ptr);
PyObject_GC_Del(ptr);
Py_DECREF(tp);
Expand All @@ -115,7 +113,7 @@ static PyObject *
MD5Type_copy_impl(MD5object *self, PyTypeObject *cls)
/*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/
{
MD5State *st = _PyType_GetModuleState(cls);
MD5State *st = PyType_GetModuleState(cls);

MD5object *newobj;
if ((newobj = newMD5object(st))==NULL)
Expand Down Expand Up @@ -158,7 +156,16 @@ MD5Type_hexdigest_impl(MD5object *self)
ENTER_HASHLIB(self);
Hacl_Streaming_MD5_legacy_finish(self->hash_state, digest);
LEAVE_HASHLIB(self);
return _Py_strhex((const char*)digest, MD5_DIGESTSIZE);

const char *hexdigits = "0123456789abcdef";
char digest_hex[MD5_DIGESTSIZE * 2];
char *str = digest_hex;
for (size_t i=0; i < MD5_DIGESTSIZE; i++) {
unsigned char byte = digest[i];
*str++ = hexdigits[byte >> 4];
*str++ = hexdigits[byte & 0x0f];
}
return PyUnicode_FromStringAndSize(digest_hex, sizeof(digest_hex));
}

static void update(Hacl_Streaming_MD5_state *state, uint8_t *buf, Py_ssize_t len) {
Expand Down
1 change: 1 addition & 0 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,4 @@ Modules/_io/_iomodule.c - _PyIO_Module -
Modules/_sqlite/module.c - _sqlite3module -
Python/optimizer_analysis.c - _Py_PartitionRootNode_Type -
Python/optimizer_analysis.c - _Py_UOpsAbstractInterpContext_Type -
Modules/clinic/md5module.c.h _md5_md5 _keywords -

0 comments on commit fcad07b

Please sign in to comment.