Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 1 | /* Common code for use by all hashlib related modules. */ |
| 2 | |
| 3 | /* |
| 4 | * Given a PyObject* obj, fill in the Py_buffer* viewp with the result |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 5 | * of PyObject_GetBuffer. Sets an exception and issues the erraction |
| 6 | * on any errors, e.g. 'return NULL' or 'goto error'. |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 7 | */ |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 8 | #define GET_BUFFER_VIEW_OR_ERROR(obj, viewp, erraction) do { \ |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 9 | if (PyUnicode_Check((obj))) { \ |
| 10 | PyErr_SetString(PyExc_TypeError, \ |
| 11 | "Unicode-objects must be encoded before hashing");\ |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 12 | erraction; \ |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 13 | } \ |
| 14 | if (!PyObject_CheckBuffer((obj))) { \ |
| 15 | PyErr_SetString(PyExc_TypeError, \ |
| 16 | "object supporting the buffer API required"); \ |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 17 | erraction; \ |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 18 | } \ |
| 19 | if (PyObject_GetBuffer((obj), (viewp), PyBUF_SIMPLE) == -1) { \ |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 20 | erraction; \ |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 21 | } \ |
| 22 | if ((viewp)->ndim > 1) { \ |
| 23 | PyErr_SetString(PyExc_BufferError, \ |
| 24 | "Buffer must be single dimension"); \ |
| 25 | PyBuffer_Release((viewp)); \ |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 26 | erraction; \ |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 27 | } \ |
Christian Heimes | 121b948 | 2016-09-06 22:03:25 +0200 | [diff] [blame] | 28 | } while(0) |
| 29 | |
| 30 | #define GET_BUFFER_VIEW_OR_ERROUT(obj, viewp) \ |
| 31 | GET_BUFFER_VIEW_OR_ERROR(obj, viewp, return NULL) |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Helper code to synchronize access to the hash object when the GIL is |
| 35 | * released around a CPU consuming hashlib operation. All code paths that |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 36 | * access a mutable part of obj must be enclosed in an ENTER_HASHLIB / |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 37 | * LEAVE_HASHLIB block or explicitly acquire and release the lock inside |
| 38 | * a PY_BEGIN / END_ALLOW_THREADS block if they wish to release the GIL for |
| 39 | * an operation. |
| 40 | */ |
| 41 | |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 42 | #include "pythread.h" |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 43 | #define ENTER_HASHLIB(obj) \ |
| 44 | if ((obj)->lock) { \ |
| 45 | if (!PyThread_acquire_lock((obj)->lock, 0)) { \ |
| 46 | Py_BEGIN_ALLOW_THREADS \ |
| 47 | PyThread_acquire_lock((obj)->lock, 1); \ |
| 48 | Py_END_ALLOW_THREADS \ |
| 49 | } \ |
| 50 | } |
| 51 | #define LEAVE_HASHLIB(obj) \ |
| 52 | if ((obj)->lock) { \ |
| 53 | PyThread_release_lock((obj)->lock); \ |
| 54 | } |
Christian Heimes | 4a0270d | 2012-10-06 02:23:36 +0200 | [diff] [blame] | 55 | |
| 56 | /* TODO(gps): We should probably make this a module or EVPobject attribute |
| 57 | * to allow the user to optimize based on the platform they're using. */ |
| 58 | #define HASHLIB_GIL_MINSIZE 2048 |
| 59 | |