Skip to content

Commit ffe7db7

Browse files
committed
use header files only
1 parent c7f7776 commit ffe7db7

File tree

9 files changed

+86
-136
lines changed

9 files changed

+86
-136
lines changed

Makefile.pre.in

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ ENSUREPIP= @ENSUREPIP@
227227
# Internal static libraries
228228
LIBMPDEC_A= Modules/_decimal/libmpdec/libmpdec.a
229229
LIBEXPAT_A= Modules/expat/libexpat.a
230-
LIBHASHLIB_INTERNAL_A=Modules/_hashlib/libhashlib.a
231230

232231
# HACL* build configuration
233232
LIBHACL_CFLAGS=@LIBHACL_CFLAGS@
@@ -764,9 +763,13 @@ LIBHACL_HMAC_HEADERS= \
764763

765764
##########################################################################
766765
# Internal library for cryptographic primitives
767-
768-
LIBHASHLIB_INTERNAL_OBJS= \
769-
Modules/_hashlib/hashlib_buffer.o
766+
#
767+
# NOTE(picnixz): Ideally, we want to have .c files but having multiple
768+
# extension modules sharing a common static or dynamic library is not
769+
# well-supported by the current build system. It would have been fine
770+
# if only one module needed it but all cryptographic modules need it.
771+
#
772+
# See https://github.com/python/cpython/issues/131876.
770773

771774
LIBHASHLIB_INTERNAL_HEADERS= \
772775
Modules/_hashlib/hashlib_buffer.h \
@@ -1527,17 +1530,6 @@ $(LIBEXPAT_A): $(LIBEXPAT_OBJS)
15271530
-rm -f $@
15281531
$(AR) $(ARFLAGS) $@ $(LIBEXPAT_OBJS)
15291532

1530-
##########################################################################
1531-
# '_hashlib', '_hmac' and HACL*-based modules helpers
1532-
LIBHASHLIB_INTERNAL_CFLAGS=@LIBHASHLIB_INTERNAL_CFLAGS@ $(PY_STDMODULE_CFLAGS) $(CCSHARED)
1533-
1534-
Modules/_hashlib/hashlib_buffer.o: Modules/_hashlib/hashlib_buffer.c $(LIBHASHLIB_INTERNAL_HEADERS) $(PYTHON_HEADERS)
1535-
$(CC) -I$(srcdir)/Modules/_hashlib -c $(LIBHASHLIB_INTERNAL_CFLAGS) -o $@ $(srcdir)/Modules/_hashlib/hashlib_buffer.c
1536-
1537-
$(LIBHASHLIB_INTERNAL_A): $(LIBHASHLIB_INTERNAL_OBJS)
1538-
-rm -f $@
1539-
$(AR) $(ARFLAGS) $@ $(LIBHASHLIB_INTERNAL_OBJS)
1540-
15411533
##########################################################################
15421534
# HACL* library build
15431535
#
@@ -3380,7 +3372,7 @@ MODULE__CTYPES_TEST_DEPS=$(srcdir)/Modules/_ctypes/_ctypes_test_generated.c.h
33803372
MODULE__CTYPES_MALLOC_CLOSURE=@MODULE__CTYPES_MALLOC_CLOSURE@
33813373
MODULE__DECIMAL_DEPS=$(srcdir)/Modules/_decimal/docstrings.h @LIBMPDEC_INTERNAL@
33823374
MODULE__ELEMENTTREE_DEPS=$(srcdir)/Modules/pyexpat.c @LIBEXPAT_INTERNAL@
3383-
MODULE__HASHLIB_DEPS=@LIBHASHLIB_INTERNAL@
3375+
MODULE__HASHLIB_DEPS=$(LIBHASHLIB_INTERNAL_HEADERS)
33843376
MODULE__IO_DEPS=$(srcdir)/Modules/_io/_iomodule.h
33853377

33863378
# HACL*-based cryptographic primitives

Modules/_hashlib/hashlib_buffer.c

Lines changed: 0 additions & 65 deletions
This file was deleted.

Modules/_hashlib/hashlib_buffer.h

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,76 @@
1111
* - If 'data' and 'string' are both NULL, set '*res' to NULL and return 0.
1212
* - Otherwise, set '*res' to 'data' or 'string' and return 1. A deprecation
1313
* warning is set when 'string' is specified.
14-
*
15-
* The symbol is exported for '_hashlib' and HACL*-based extension modules.
1614
*/
17-
extern int
18-
_Py_hashlib_data_argument(PyObject **res, PyObject *data, PyObject *string);
15+
static inline int
16+
_Py_hashlib_data_argument(PyObject **res, PyObject *data, PyObject *string)
17+
{
18+
if (data != NULL && string == NULL) {
19+
// called as H(data) or H(data=...)
20+
*res = data;
21+
return 1;
22+
}
23+
else if (data == NULL && string != NULL) {
24+
// called as H(string=...)
25+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
26+
"the 'string' keyword parameter is deprecated since "
27+
"Python 3.15 and slated for removal in Python 3.19; "
28+
"use the 'data' keyword parameter or pass the data "
29+
"to hash as a positional argument instead", 1) < 0)
30+
{
31+
*res = NULL;
32+
return -1;
33+
}
34+
*res = string;
35+
return 1;
36+
}
37+
else if (data == NULL && string == NULL) {
38+
// fast path when no data is given
39+
assert(!PyErr_Occurred());
40+
*res = NULL;
41+
return 0;
42+
}
43+
else {
44+
// called as H(data=..., string)
45+
*res = NULL;
46+
PyErr_SetString(PyExc_TypeError,
47+
"'data' and 'string' are mutually exclusive "
48+
"and support for 'string' keyword parameter "
49+
"is slated for removal in a future version.");
50+
return -1;
51+
}
52+
}
1953

2054
/*
2155
* Obtain a buffer view from a buffer-like object 'obj'.
2256
*
2357
* On success, store the result in 'view' and return 0.
2458
* On error, set an exception and return -1.
25-
*
26-
* The symbol is exported for '_hashlib' and HACL*-based extension modules.
2759
*/
28-
extern int
29-
_Py_hashlib_get_buffer_view(PyObject *obj, Py_buffer *view);
60+
static inline int
61+
_Py_hashlib_get_buffer_view(PyObject *obj, Py_buffer *view)
62+
{
63+
if (PyUnicode_Check(obj)) {
64+
PyErr_SetString(PyExc_TypeError,
65+
"Strings must be encoded before hashing");
66+
return -1;
67+
}
68+
if (!PyObject_CheckBuffer(obj)) {
69+
PyErr_SetString(PyExc_TypeError,
70+
"object supporting the buffer API required");
71+
return -1;
72+
}
73+
if (PyObject_GetBuffer(obj, view, PyBUF_SIMPLE) == -1) {
74+
return -1;
75+
}
76+
if (view->ndim > 1) {
77+
PyErr_SetString(PyExc_BufferError,
78+
"Buffer must be single dimension");
79+
PyBuffer_Release(view);
80+
return -1;
81+
}
82+
return 0;
83+
}
3084

3185
/*
3286
* Call _Py_hashlib_get_buffer_view() and check if it succeeded.

PCbuild/_hashlib.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
</ItemDefinitionGroup>
100100
<ItemGroup>
101101
<ClCompile Include="..\Modules\_hashopenssl.c" />
102-
<ClCompile Include="..\Modules\_hashlib\hashlib_buffer.c" />
103102
</ItemGroup>
104103
<ItemGroup>
105104
<ClInclude Include="..\Modules\_hashlib\hashlib_buffer.h" />

PCbuild/_hashlib.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
<ClCompile Include="..\Modules\_hashopenssl.c">
1313
<Filter>Source Files</Filter>
1414
</ClCompile>
15-
<ClCompile Include="..\Modules\_hashlib\hashlib_buffer.c">
16-
<Filter>Source Files</Filter>
17-
</ClCompile>
1815
</ItemGroup>
1916
<ItemGroup>
2017
<ResourceCompile Include="..\PC\python_nt.rc">

PCbuild/pythoncore.vcxproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@
447447
<PreprocessorDefinitions>HACL_CAN_COMPILE_VEC128;%(PreprocessorDefinitions)</PreprocessorDefinitions>
448448
<AdditionalOptions>/arch:AVX %(AdditionalOptions)</AdditionalOptions>
449449
</ClCompile>
450-
<ClCompile Include="..\Modules\_hashlib\hashlib_buffer.c" />
451450
<ClInclude Include="..\Modules\_hashlib\hashlib_buffer.h" />
452451
<ClInclude Include="..\Modules\_hashlib\hashlib_fetch.h" />
453452
<ClInclude Include="..\Modules\_hashlib\hashlib_mutex.h" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,6 @@
980980
<ClCompile Include="..\Modules\_hacl\Hacl_Streaming_HMAC.c">
981981
<Filter>Modules</Filter>
982982
</ClCompile>
983-
<ClCompile Include="..\Modules\_hashlib\hashlib_buffer.c">
984-
<Filter>Modules\_hashlib</Filter>
985-
</ClCompile>
986983
<ClCompile Include="..\Modules\_heapqmodule.c">
987984
<Filter>Modules</Filter>
988985
</ClCompile>

configure

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

configure.ac

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7958,15 +7958,6 @@ PY_STDLIB_MOD_SIMPLE([_codecs_tw])
79587958
PY_STDLIB_MOD_SIMPLE([_multibytecodec])
79597959
PY_STDLIB_MOD_SIMPLE([unicodedata])
79607960

7961-
###############################################################################
7962-
# Cryptographic primitives
7963-
LIBHASHLIB_INTERNAL_CFLAGS="-I\$(srcdir)/Modules/_hashlib"
7964-
LIBHASHLIB_INTERNAL_LDFLAGS="-lm \$(LIBHASHLIB_INTERNAL_A)"
7965-
LIBHASHLIB_INTERNAL="\$(LIBHASHLIB_INTERNAL_HEADERS) \$(LIBHASHLIB_INTERNAL_A)"
7966-
7967-
AC_SUBST([LIBHASHLIB_INTERNAL_CFLAGS])
7968-
AC_SUBST([LIBHASHLIB_INTERNAL])
7969-
79707961
###############################################################################
79717962
# HACL* compilation and linking configuration (contact: @picnixz)
79727963
#
@@ -8103,9 +8094,7 @@ dnl The EXTNAME is the name of the extension module being built.
81038094
AC_DEFUN([PY_HACL_CREATE_MODULE], [
81048095
AS_VAR_PUSHDEF([v], [[LIBHACL_][$1][_LDFLAGS]])
81058096
AS_VAR_SET([v], [[LIBHACL_][$1][_LIB_${LIBHACL_LDEPS_LIBTYPE}]])
8106-
PY_STDLIB_MOD([$2], [$3], [],
8107-
[$LIBHACL_CFLAGS $LIBHASHLIB_INTERNAL_CFLAGS],
8108-
[\$($v) $LIBHASHLIB_INTERNAL_LDFLAGS])
8097+
PY_STDLIB_MOD([$2], [$3], [], [$LIBHACL_CFLAGS], [\$($v)])
81098098
AS_VAR_POPDEF([v])
81108099
])
81118100

@@ -8186,8 +8175,7 @@ dnl OpenSSL bindings
81868175
PY_STDLIB_MOD([_ssl], [], [test "$ac_cv_working_openssl_ssl" = yes],
81878176
[$OPENSSL_INCLUDES], [$OPENSSL_LDFLAGS $OPENSSL_LDFLAGS_RPATH $OPENSSL_LIBS])
81888177
PY_STDLIB_MOD([_hashlib], [], [test "$ac_cv_working_openssl_hashlib" = yes],
8189-
[$OPENSSL_INCLUDES $LIBHASHLIB_INTERNAL_CFLAGS],
8190-
[$OPENSSL_LDFLAGS $OPENSSL_LDFLAGS_RPATH $LIBCRYPTO_LIBS $LIBHASHLIB_INTERNAL_LDFLAGS])
8178+
[$OPENSSL_INCLUDES], [$OPENSSL_LDFLAGS $OPENSSL_LDFLAGS_RPATH $LIBCRYPTO_LIBS])
81918179

81928180
dnl test modules
81938181
PY_STDLIB_MOD([_testcapi],

0 commit comments

Comments
 (0)