~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Modules/hashlib.h

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
 * of PyObject_GetBuffer.  Sets an exception and issues a return NULL
 
6
 * on any errors.
 
7
 */
 
8
#define GET_BUFFER_VIEW_OR_ERROUT(obj, viewp) do { \
 
9
        if (PyUnicode_Check((obj))) { \
 
10
            PyErr_SetString(PyExc_TypeError, \
 
11
                            "Unicode-objects must be encoded before hashing");\
 
12
            return NULL; \
 
13
        } \
 
14
        if (!PyObject_CheckBuffer((obj))) { \
 
15
            PyErr_SetString(PyExc_TypeError, \
 
16
                            "object supporting the buffer API required"); \
 
17
            return NULL; \
 
18
        } \
 
19
        if (PyObject_GetBuffer((obj), (viewp), PyBUF_SIMPLE) == -1) { \
 
20
            return NULL; \
 
21
        } \
 
22
        if ((viewp)->ndim > 1) { \
 
23
            PyErr_SetString(PyExc_BufferError, \
 
24
                            "Buffer must be single dimension"); \
 
25
            PyBuffer_Release((viewp)); \
 
26
            return NULL; \
 
27
        } \
 
28
    } while(0);
 
29
 
 
30
/*
 
31
 * Helper code to synchronize access to the hash object when the GIL is
 
32
 * released around a CPU consuming hashlib operation. All code paths that
 
33
 * access a mutable part of obj must be enclosed in a ENTER_HASHLIB /
 
34
 * LEAVE_HASHLIB block or explicitly acquire and release the lock inside
 
35
 * a PY_BEGIN / END_ALLOW_THREADS block if they wish to release the GIL for
 
36
 * an operation.
 
37
 */
 
38
 
 
39
#ifdef WITH_THREAD
 
40
#include "pythread.h"
 
41
    #define ENTER_HASHLIB(obj) \
 
42
        if ((obj)->lock) { \
 
43
            if (!PyThread_acquire_lock((obj)->lock, 0)) { \
 
44
                Py_BEGIN_ALLOW_THREADS \
 
45
                PyThread_acquire_lock((obj)->lock, 1); \
 
46
                Py_END_ALLOW_THREADS \
 
47
            } \
 
48
        }
 
49
    #define LEAVE_HASHLIB(obj) \
 
50
        if ((obj)->lock) { \
 
51
            PyThread_release_lock((obj)->lock); \
 
52
        }
 
53
#else
 
54
    #define ENTER_HASHLIB(obj)
 
55
    #define LEAVE_HASHLIB(obj)
 
56
#endif
 
57
 
 
58
/* TODO(gps): We should probably make this a module or EVPobject attribute
 
59
 * to allow the user to optimize based on the platform they're using. */
 
60
#define HASHLIB_GIL_MINSIZE 2048
 
61