1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* util.h
*
* Copyright (C) AB Strakt
* See LICENSE for details.
*
* Export utility functions and macros.
* See the file RATIONALE for a short explanation of why this module was written.
*
* Reviewed 2001-07-23
*
*/
#ifndef PyOpenSSL_UTIL_H_
#define PyOpenSSL_UTIL_H_
#include <Python.h>
#include <openssl/err.h>
/*
* pymemcompat written by Michael Hudson and lets you program to the
* Python 2.3 memory API while keeping backwards compatibility.
*/
#include "pymemcompat.h"
/*
* py3k defines macros that help with Python 2.x/3.x compatibility.
*/
#include "py3k.h"
extern PyObject *error_queue_to_list(void);
extern void exception_from_error_queue(PyObject *the_Error);
extern void flush_error_queue(void);
/*
* These are needed because there is no "official" way to specify
* WHERE to save the thread state.
*/
#ifdef WITH_THREAD
/*
* Get the current Python threadstate and put it somewhere any code running
* in this thread can get it, if it needs to restore the threadstate to run
* some Python.
*/
# define MY_BEGIN_ALLOW_THREADS(ignored) \
PyThread_delete_key_value(_pyOpenSSL_tstate_key); \
PyThread_set_key_value(_pyOpenSSL_tstate_key, PyEval_SaveThread());
/*
* Get the previous Python threadstate and restore it.
*/
# define MY_END_ALLOW_THREADS(ignored) \
PyEval_RestoreThread(PyThread_get_key_value(_pyOpenSSL_tstate_key));
#else
# define MY_BEGIN_ALLOW_THREADS(st)
# define MY_END_ALLOW_THREADS(st) { st = NULL; }
#endif
#if !defined(PY_MAJOR_VERSION) || PY_VERSION_HEX < 0x02000000
static int
PyModule_AddObject(PyObject *m, char *name, PyObject *o)
{
PyObject *dict;
if (!PyModule_Check(m) || o == NULL)
return -1;
dict = PyModule_GetDict(m);
if (dict == NULL)
return -1;
if (PyDict_SetItemString(dict, name, o))
return -1;
Py_DECREF(o);
return 0;
}
static int
PyModule_AddIntConstant(PyObject *m, char *name, long value)
{
return PyModule_AddObject(m, name, PyInt_FromLong(value));
}
static int PyObject_AsFileDescriptor(PyObject *o)
{
int fd;
PyObject *meth;
if (PyInt_Check(o)) {
fd = PyInt_AsLong(o);
}
else if (PyLong_Check(o)) {
fd = PyLong_AsLong(o);
}
else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
{
PyObject *fno = PyEval_CallObject(meth, NULL);
Py_DECREF(meth);
if (fno == NULL)
return -1;
if (PyInt_Check(fno)) {
fd = PyInt_AsLong(fno);
Py_DECREF(fno);
}
else if (PyLong_Check(fno)) {
fd = PyLong_AsLong(fno);
Py_DECREF(fno);
}
else {
PyErr_SetString(PyExc_TypeError, "fileno() returned a non-integer");
Py_DECREF(fno);
return -1;
}
}
else {
PyErr_SetString(PyExc_TypeError, "argument must be an int, or have a fileno() method.");
return -1;
}
if (fd < 0) {
PyErr_Format(PyExc_ValueError, "file descriptor cannot be a negative integer (%i)", fd);
return -1;
}
return fd;
}
#endif
#if !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
#if (PY_VERSION_HEX < 0x02600000)
extern PyObject* PyOpenSSL_LongToHex(PyObject *o);
#else
#define PyOpenSSL_LongToHex(o) PyNumber_ToBase(o, 16)
#endif
#endif
|