gh-131876: extract `_hashlib` helpers into a separate directory [WIP] by picnixz · Pull Request #135341 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
553 changes: 0 additions & 553 deletions .github/workflows/build.yml

Large diffs are not rendered by default.

28 changes: 0 additions & 28 deletions .github/workflows/lint.yml

This file was deleted.

41 changes: 34 additions & 7 deletions Makefile.pre.in
40 changes: 40 additions & 0 deletions Modules/_hashlib/hashlib_buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "hashlib_buffer.h"

int
_Py_hashlib_data_argument(PyObject **res, PyObject *data, PyObject *string)
{
if (data != NULL && string == NULL) {
// called as H(data) or H(data=...)
*res = data;
return 1;
}
else if (data == NULL && string != NULL) {
// called as H(string=...)
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"the 'string' keyword parameter is deprecated since "
"Python 3.15 and slated for removal in Python 3.19; "
"use the 'data' keyword parameter or pass the data "
"to hash as a positional argument instead", 1) < 0)
{
*res = NULL;
return -1;
}
*res = string;
return 1;
}
else if (data == NULL && string == NULL) {
// fast path when no data is given
assert(!PyErr_Occurred());
*res = NULL;
return 0;
}
else {
// called as H(data=..., string)
*res = NULL;
PyErr_SetString(PyExc_TypeError,
"'data' and 'string' are mutually exclusive "
"and support for 'string' keyword parameter "
"is slated for removal in a future version.");
return -1;
}
}
60 changes: 60 additions & 0 deletions Modules/_hashlib/hashlib_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef _HASHLIB_HASHLIB_BUFFER_H
#define _HASHLIB_HASHLIB_BUFFER_H

#include "Python.h"

/*
* Given an buffer-like OBJ, fill in the buffer VIEW with the result
* of PyObject_GetBuffer.
*
* On error, set an exception and execute the ERRACTION statements,
* e.g. 'return NULL' or 'goto error'.
*
* Parameters
*
* OBJ An object supporting the buffer API.
* VIEW A Py_buffer pointer to fill.
* ERRACTION The statements to execute on error.
*/
#define GET_BUFFER_VIEW_OR_ERROR(OBJ, VIEW, ERRACTION) \
do { \
if (PyUnicode_Check((OBJ))) { \
PyErr_SetString(PyExc_TypeError, \
"strings must be encoded before hashing"); \
ERRACTION; \
} \
if (!PyObject_CheckBuffer((OBJ))) { \
PyErr_SetString(PyExc_TypeError, \
"object supporting the buffer API required"); \
ERRACTION; \
} \
if (PyObject_GetBuffer((OBJ), (VIEW), PyBUF_SIMPLE) == -1) { \
ERRACTION; \
} \
if ((VIEW)->ndim > 1) { \
PyErr_SetString(PyExc_BufferError, \
"buffer must be one-dimensional"); \
PyBuffer_Release((VIEW)); \
ERRACTION; \
} \
} while(0)

/* Specialization of GET_BUFFER_VIEW_OR_ERROR() returning NULL on error. */
#define GET_BUFFER_VIEW_OR_ERROUT(OBJ, VIEW) \
GET_BUFFER_VIEW_OR_ERROR(OBJ, VIEW, return NULL)

/*
* Allow to use the 'data' or 'string' keyword in hashlib.new()
* and other hash functions named constructors.
*
* - If 'data' and 'string' are both non-NULL, set an exception and return -1.
* - If 'data' and 'string' are both NULL, set '*res' to NULL and return 0.
* - Otherwise, set '*res' to 'data' or 'string' and return 1. A deprecation
* warning is set when 'string' is specified.
*
* The symbol is exported for '_hashlib' and HACL*-based extension modules.
*/
PyAPI_FUNC(int)
_Py_hashlib_data_argument(PyObject **res, PyObject *data, PyObject *string);

#endif // !_HASHLIB_HASHLIB_BUFFER_H
1 change: 1 addition & 0 deletions Modules/_hashlib/hashlib_fetch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "hashlib_fetch.h"
140 changes: 140 additions & 0 deletions Modules/_hashlib/hashlib_fetch.h
Loading