annotate Modules/hashlib.h @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 454e9db1c750
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
1 /* Common code for use by all hashlib related modules. */
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
2
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
3 /*
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
4 * Given a PyObject* obj, fill in the Py_buffer* viewp with the result
84971
3716d64f846b Issue #18627: Fix typo noticed by Vajrasky Kok.
Ned Deily <nad@acm.org>
parents: 51574
diff changeset
5 * of PyObject_GetBuffer. Sets an exception and issues a return NULL
51574
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
6 * on any errors.
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
7 */
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
8 #define GET_BUFFER_VIEW_OR_ERROUT(obj, viewp) do { \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
9 if (PyUnicode_Check((obj))) { \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
10 PyErr_SetString(PyExc_TypeError, \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
11 "Unicode-objects must be encoded before hashing");\
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
12 return NULL; \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
13 } \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
14 if (!PyObject_CheckBuffer((obj))) { \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
15 PyErr_SetString(PyExc_TypeError, \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
16 "object supporting the buffer API required"); \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
17 return NULL; \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
18 } \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19 if (PyObject_GetBuffer((obj), (viewp), PyBUF_SIMPLE) == -1) { \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
20 return NULL; \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21 } \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
22 if ((viewp)->ndim > 1) { \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
23 PyErr_SetString(PyExc_BufferError, \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
24 "Buffer must be single dimension"); \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
25 PyBuffer_Release((viewp)); \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
26 return NULL; \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
27 } \
ce29faed7f69 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
28 } while(0);
79502
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
29
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
30 /*
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
31 * Helper code to synchronize access to the hash object when the GIL is
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
32 * released around a CPU consuming hashlib operation. All code paths that
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
33 * access a mutable part of obj must be enclosed in a ENTER_HASHLIB /
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
34 * LEAVE_HASHLIB block or explicitly acquire and release the lock inside
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
35 * a PY_BEGIN / END_ALLOW_THREADS block if they wish to release the GIL for
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
36 * an operation.
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
37 */
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
38
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
39 #ifdef WITH_THREAD
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
40 #include "pythread.h"
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
41 #define ENTER_HASHLIB(obj) \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
42 if ((obj)->lock) { \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
43 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
44 Py_BEGIN_ALLOW_THREADS \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
45 PyThread_acquire_lock((obj)->lock, 1); \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
46 Py_END_ALLOW_THREADS \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
47 } \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
48 }
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
49 #define LEAVE_HASHLIB(obj) \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
50 if ((obj)->lock) { \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
51 PyThread_release_lock((obj)->lock); \
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
52 }
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
53 #else
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
54 #define ENTER_HASHLIB(obj)
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
55 #define LEAVE_HASHLIB(obj)
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
56 #endif
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
57
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
58 /* TODO(gps): We should probably make this a module or EVPobject attribute
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
59 * to allow the user to optimize based on the platform they're using. */
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
60 #define HASHLIB_GIL_MINSIZE 2048
11c9a894680e Issue #16113: integrade SHA-3 (Keccak) patch from http://hg.python.org/sandbox/cheimes
Christian Heimes <christian@cheimes.de>
parents: 51574
diff changeset
61