blob: 6724f5a57ed47b808340fea36a207b03ffc22bc6 [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);
Rick Deand369c932009-07-08 11:48:33 -050026extern void exception_from_error_queue(PyObject *the_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050027extern void flush_error_queue(void);
28
29/*
30 * These are needed because there is no "official" way to specify
31 * WHERE to save the thread state.
32 */
33#ifdef WITH_THREAD
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040034
35/*
36 * Get the current Python threadstate and put it somewhere any code running
37 * in this thread can get it, if it needs to restore the threadstate to run
38 * some Python.
39 */
40# define MY_BEGIN_ALLOW_THREADS(ignored) \
Jean-Paul Calderoned3feb012009-03-22 12:21:17 -040041 PyThread_delete_key_value(_pyOpenSSL_tstate_key); \
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040042 PyThread_set_key_value(_pyOpenSSL_tstate_key, PyEval_SaveThread());
43
44/*
45 * Get the previous Python threadstate and restore it.
46 */
47# define MY_END_ALLOW_THREADS(ignored) \
48 PyEval_RestoreThread(PyThread_get_key_value(_pyOpenSSL_tstate_key));
49
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050050#else
51# define MY_BEGIN_ALLOW_THREADS(st)
52# define MY_END_ALLOW_THREADS(st) { st = NULL; }
53#endif
54
55#if !defined(PY_MAJOR_VERSION) || PY_VERSION_HEX < 0x02000000
56static int
57PyModule_AddObject(PyObject *m, char *name, PyObject *o)
58{
59 PyObject *dict;
60 if (!PyModule_Check(m) || o == NULL)
61 return -1;
62 dict = PyModule_GetDict(m);
63 if (dict == NULL)
64 return -1;
65 if (PyDict_SetItemString(dict, name, o))
66 return -1;
67 Py_DECREF(o);
68 return 0;
69}
70
71static int
72PyModule_AddIntConstant(PyObject *m, char *name, long value)
73{
74 return PyModule_AddObject(m, name, PyInt_FromLong(value));
75}
76
77static int PyObject_AsFileDescriptor(PyObject *o)
78{
79 int fd;
80 PyObject *meth;
81
82 if (PyInt_Check(o)) {
83 fd = PyInt_AsLong(o);
84 }
85 else if (PyLong_Check(o)) {
86 fd = PyLong_AsLong(o);
87 }
88 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
89 {
90 PyObject *fno = PyEval_CallObject(meth, NULL);
91 Py_DECREF(meth);
92 if (fno == NULL)
93 return -1;
94
95 if (PyInt_Check(fno)) {
96 fd = PyInt_AsLong(fno);
97 Py_DECREF(fno);
98 }
99 else if (PyLong_Check(fno)) {
100 fd = PyLong_AsLong(fno);
101 Py_DECREF(fno);
102 }
103 else {
104 PyErr_SetString(PyExc_TypeError, "fileno() returned a non-integer");
105 Py_DECREF(fno);
106 return -1;
107 }
108 }
109 else {
110 PyErr_SetString(PyExc_TypeError, "argument must be an int, or have a fileno() method.");
111 return -1;
112 }
113
114 if (fd < 0) {
115 PyErr_Format(PyExc_ValueError, "file descriptor cannot be a negative integer (%i)", fd);
116 return -1;
117 }
118 return fd;
119}
120#endif
121
Ziga Seilnacht679c4262009-09-01 01:32:29 +0200122#if !defined(PY_SSIZE_T_MIN)
123typedef int Py_ssize_t;
124#define PY_SSIZE_T_MAX INT_MAX
125#define PY_SSIZE_T_MIN INT_MIN
126#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500127
128#endif