blob: b95e75b73c575e44b2c130346f041e60f630deb7 [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) \
40 PyThread_set_key_value(_pyOpenSSL_tstate_key, PyEval_SaveThread());
41
42/*
43 * Get the previous Python threadstate and restore it.
44 */
45# define MY_END_ALLOW_THREADS(ignored) \
46 PyEval_RestoreThread(PyThread_get_key_value(_pyOpenSSL_tstate_key));
47
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050048#else
49# define MY_BEGIN_ALLOW_THREADS(st)
50# define MY_END_ALLOW_THREADS(st) { st = NULL; }
51#endif
52
53#if !defined(PY_MAJOR_VERSION) || PY_VERSION_HEX < 0x02000000
54static int
55PyModule_AddObject(PyObject *m, char *name, PyObject *o)
56{
57 PyObject *dict;
58 if (!PyModule_Check(m) || o == NULL)
59 return -1;
60 dict = PyModule_GetDict(m);
61 if (dict == NULL)
62 return -1;
63 if (PyDict_SetItemString(dict, name, o))
64 return -1;
65 Py_DECREF(o);
66 return 0;
67}
68
69static int
70PyModule_AddIntConstant(PyObject *m, char *name, long value)
71{
72 return PyModule_AddObject(m, name, PyInt_FromLong(value));
73}
74
75static int PyObject_AsFileDescriptor(PyObject *o)
76{
77 int fd;
78 PyObject *meth;
79
80 if (PyInt_Check(o)) {
81 fd = PyInt_AsLong(o);
82 }
83 else if (PyLong_Check(o)) {
84 fd = PyLong_AsLong(o);
85 }
86 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
87 {
88 PyObject *fno = PyEval_CallObject(meth, NULL);
89 Py_DECREF(meth);
90 if (fno == NULL)
91 return -1;
92
93 if (PyInt_Check(fno)) {
94 fd = PyInt_AsLong(fno);
95 Py_DECREF(fno);
96 }
97 else if (PyLong_Check(fno)) {
98 fd = PyLong_AsLong(fno);
99 Py_DECREF(fno);
100 }
101 else {
102 PyErr_SetString(PyExc_TypeError, "fileno() returned a non-integer");
103 Py_DECREF(fno);
104 return -1;
105 }
106 }
107 else {
108 PyErr_SetString(PyExc_TypeError, "argument must be an int, or have a fileno() method.");
109 return -1;
110 }
111
112 if (fd < 0) {
113 PyErr_Format(PyExc_ValueError, "file descriptor cannot be a negative integer (%i)", fd);
114 return -1;
115 }
116 return fd;
117}
118#endif
119
120
121
122#endif