blob: 1774956846d31a51dbbae3da979de02963382a76 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * util.h
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
5 *
6 * Export utility functions and macros.
7 * See the file RATIONALE for a short explanation of why this module was written.
8 *
9 * Reviewed 2001-07-23
10 *
11 * @(#) $Id: util.h,v 1.8 2002/08/16 10:08:09 martin Exp $
12 */
13#ifndef PyOpenSSL_UTIL_H_
14#define PyOpenSSL_UTIL_H_
15
16#include <Python.h>
17#include <openssl/err.h>
18
19/*
20 * pymemcompat written by Michael Hudson and lets you program to the
21 * Python 2.3 memory API while keeping backwards compatability.
22 */
23#include "pymemcompat.h"
24
25extern PyObject *error_queue_to_list(void);
26extern void flush_error_queue(void);
27
28/*
29 * These are needed because there is no "official" way to specify
30 * WHERE to save the thread state.
31 */
32#ifdef WITH_THREAD
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040033
34/*
35 * Get the current Python threadstate and put it somewhere any code running
36 * in this thread can get it, if it needs to restore the threadstate to run
37 * some Python.
38 */
39# define MY_BEGIN_ALLOW_THREADS(ignored) \
Jean-Paul Calderoned3feb012009-03-22 12:21:17 -040040 PyThread_delete_key_value(_pyOpenSSL_tstate_key); \
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040041 PyThread_set_key_value(_pyOpenSSL_tstate_key, PyEval_SaveThread());
42
43/*
44 * Get the previous Python threadstate and restore it.
45 */
46# define MY_END_ALLOW_THREADS(ignored) \
47 PyEval_RestoreThread(PyThread_get_key_value(_pyOpenSSL_tstate_key));
48
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050049#else
50# define MY_BEGIN_ALLOW_THREADS(st)
51# define MY_END_ALLOW_THREADS(st) { st = NULL; }
52#endif
53
54#if !defined(PY_MAJOR_VERSION) || PY_VERSION_HEX < 0x02000000
55static int
56PyModule_AddObject(PyObject *m, char *name, PyObject *o)
57{
58 PyObject *dict;
59 if (!PyModule_Check(m) || o == NULL)
60 return -1;
61 dict = PyModule_GetDict(m);
62 if (dict == NULL)
63 return -1;
64 if (PyDict_SetItemString(dict, name, o))
65 return -1;
66 Py_DECREF(o);
67 return 0;
68}
69
70static int
71PyModule_AddIntConstant(PyObject *m, char *name, long value)
72{
73 return PyModule_AddObject(m, name, PyInt_FromLong(value));
74}
75
76static int PyObject_AsFileDescriptor(PyObject *o)
77{
78 int fd;
79 PyObject *meth;
80
81 if (PyInt_Check(o)) {
82 fd = PyInt_AsLong(o);
83 }
84 else if (PyLong_Check(o)) {
85 fd = PyLong_AsLong(o);
86 }
87 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
88 {
89 PyObject *fno = PyEval_CallObject(meth, NULL);
90 Py_DECREF(meth);
91 if (fno == NULL)
92 return -1;
93
94 if (PyInt_Check(fno)) {
95 fd = PyInt_AsLong(fno);
96 Py_DECREF(fno);
97 }
98 else if (PyLong_Check(fno)) {
99 fd = PyLong_AsLong(fno);
100 Py_DECREF(fno);
101 }
102 else {
103 PyErr_SetString(PyExc_TypeError, "fileno() returned a non-integer");
104 Py_DECREF(fno);
105 return -1;
106 }
107 }
108 else {
109 PyErr_SetString(PyExc_TypeError, "argument must be an int, or have a fileno() method.");
110 return -1;
111 }
112
113 if (fd < 0) {
114 PyErr_Format(PyExc_ValueError, "file descriptor cannot be a negative integer (%i)", fd);
115 return -1;
116 }
117 return fd;
118}
119#endif
120
121
122
123#endif