util.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  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. #include <Python.h>
  16. #include <openssl/err.h>
  17. /*
  18. * pymemcompat written by Michael Hudson and lets you program to the
  19. * Python 2.3 memory API while keeping backwards compatibility.
  20. */
  21. #include "pymemcompat.h"
  22. /*
  23. * py3k defines macros that help with Python 2.x/3.x compatibility.
  24. */
  25. #include "py3k.h"
  26. extern PyObject *error_queue_to_list(void);
  27. extern void exception_from_error_queue(PyObject *the_Error);
  28. extern void flush_error_queue(void);
  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
  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_delete_key_value(_pyOpenSSL_tstate_key); \
  41. PyThread_set_key_value(_pyOpenSSL_tstate_key, PyEval_SaveThread());
  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. #else
  48. # define MY_BEGIN_ALLOW_THREADS(st)
  49. # define MY_END_ALLOW_THREADS(st) { st = NULL; }
  50. #endif
  51. #if !defined(PY_MAJOR_VERSION) || PY_VERSION_HEX < 0x02000000
  52. static int
  53. PyModule_AddObject(PyObject *m, char *name, PyObject *o)
  54. {
  55. PyObject *dict;
  56. if (!PyModule_Check(m) || o == NULL)
  57. return -1;
  58. dict = PyModule_GetDict(m);
  59. if (dict == NULL)
  60. return -1;
  61. if (PyDict_SetItemString(dict, name, o))
  62. return -1;
  63. Py_DECREF(o);
  64. return 0;
  65. }
  66. static int
  67. PyModule_AddIntConstant(PyObject *m, char *name, long value)
  68. {
  69. return PyModule_AddObject(m, name, PyInt_FromLong(value));
  70. }
  71. static int PyObject_AsFileDescriptor(PyObject *o)
  72. {
  73. int fd;
  74. PyObject *meth;
  75. if (PyInt_Check(o)) {
  76. fd = PyInt_AsLong(o);
  77. }
  78. else if (PyLong_Check(o)) {
  79. fd = PyLong_AsLong(o);
  80. }
  81. else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
  82. {
  83. PyObject *fno = PyEval_CallObject(meth, NULL);
  84. Py_DECREF(meth);
  85. if (fno == NULL)
  86. return -1;
  87. if (PyInt_Check(fno)) {
  88. fd = PyInt_AsLong(fno);
  89. Py_DECREF(fno);
  90. }
  91. else if (PyLong_Check(fno)) {
  92. fd = PyLong_AsLong(fno);
  93. Py_DECREF(fno);
  94. }
  95. else {
  96. PyErr_SetString(PyExc_TypeError, "fileno() returned a non-integer");
  97. Py_DECREF(fno);
  98. return -1;
  99. }
  100. }
  101. else {
  102. PyErr_SetString(PyExc_TypeError, "argument must be an int, or have a fileno() method.");
  103. return -1;
  104. }
  105. if (fd < 0) {
  106. PyErr_Format(PyExc_ValueError, "file descriptor cannot be a negative integer (%i)", fd);
  107. return -1;
  108. }
  109. return fd;
  110. }
  111. #endif
  112. #if !defined(PY_SSIZE_T_MIN)
  113. typedef int Py_ssize_t;
  114. #define PY_SSIZE_T_MAX INT_MAX
  115. #define PY_SSIZE_T_MIN INT_MIN
  116. #endif
  117. #if (PY_VERSION_HEX < 0x02600000)
  118. extern PyObject* PyOpenSSL_LongToHex(PyObject *o);
  119. #else
  120. #define PyOpenSSL_LongToHex(o) PyNumber_ToBase(o, 16)
  121. #endif
  122. #endif