| 1 |
#include "pycurl.h" |
| 2 |
|
| 3 |
PYCURL_INTERNAL int |
| 4 |
check_pending_python_signal(void) |
| 5 |
{ |
| 6 |
if (PyErr_CheckSignals() != 0) { |
| 7 |
return -1; |
| 8 |
} |
| 9 |
return 0; |
| 10 |
} |
| 11 |
|
| 12 |
PYCURL_INTERNAL int |
| 13 |
check_pending_python_exception_or_signal(void) |
| 14 |
{ |
| 15 |
if (PyErr_Occurred()) { |
| 16 |
return -1; |
| 17 |
} |
| 18 |
return check_pending_python_signal(); |
| 19 |
} |
| 20 |
|
| 21 |
PYCURL_INTERNAL void |
| 22 |
warn_failed_to_acquire_thread(const char *warning_message) |
| 23 |
{ |
| 24 |
PyGILState_STATE tmp_warn_state = PyGILState_Ensure(); |
| 25 |
PyErr_WarnEx(PyExc_RuntimeWarning, warning_message, 1); |
| 26 |
PyGILState_Release(tmp_warn_state); |
| 27 |
} |
| 28 |
|
| 29 |
PYCURL_INTERNAL void |
| 30 |
print_callback_error_if_regular_exception(void) |
| 31 |
{ |
| 32 |
if (PyErr_ExceptionMatches(PyExc_Exception)) { |
| 33 |
PyErr_Print(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
PYCURL_INTERNAL PyObject * |
| 38 |
PyLong_FromCurlSocket(curl_socket_t sockfd) |
| 39 |
{ |
| 40 |
#if defined(WIN32) |
| 41 |
if (sockfd == CURL_SOCKET_BAD) { |
| 42 |
return PyLong_FromLong(-1); |
| 43 |
} |
| 44 |
return PyLong_FromUnsignedLongLong((unsigned long long) sockfd); |
| 45 |
#else |
| 46 |
return PyLong_FromLongLong((long long) sockfd); |
| 47 |
#endif |
| 48 |
} |
| 49 |
|
| 50 |
PYCURL_INTERNAL int |
| 51 |
PyLong_AsCurlSocket(PyObject *obj, curl_socket_t *sockfd) |
| 52 |
{ |
| 53 |
#if defined(WIN32) |
| 54 |
const unsigned long long max_socket = |
| 55 |
(unsigned long long) ((curl_socket_t) ~(curl_socket_t) 0); |
| 56 |
long long ll; |
| 57 |
unsigned long long ull; |
| 58 |
#else |
| 59 |
long long ll; |
| 60 |
#endif |
| 61 |
|
| 62 |
assert(sockfd != NULL); |
| 63 |
|
| 64 |
#if defined(WIN32) |
| 65 |
ll = PyLong_AsLongLong(obj); |
| 66 |
if (!PyErr_Occurred()) { |
| 67 |
if (ll == -1) { |
| 68 |
*sockfd = CURL_SOCKET_BAD; |
| 69 |
return 0; |
| 70 |
} |
| 71 |
if (ll < 0) { |
| 72 |
PyErr_SetString(PyExc_OverflowError, |
| 73 |
"socket value must be -1 or non-negative"); |
| 74 |
return -1; |
| 75 |
} |
| 76 |
if ((unsigned long long) ll > max_socket) { |
| 77 |
PyErr_SetString(PyExc_OverflowError, "socket value is out of range"); |
| 78 |
return -1; |
| 79 |
} |
| 80 |
*sockfd = (curl_socket_t) ll; |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
if (!PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 85 |
return -1; |
| 86 |
} |
| 87 |
PyErr_Clear(); |
| 88 |
|
| 89 |
ull = PyLong_AsUnsignedLongLong(obj); |
| 90 |
if (PyErr_Occurred()) { |
| 91 |
return -1; |
| 92 |
} |
| 93 |
if (ull > max_socket) { |
| 94 |
PyErr_SetString(PyExc_OverflowError, "socket value is out of range"); |
| 95 |
return -1; |
| 96 |
} |
| 97 |
*sockfd = (curl_socket_t) ull; |
| 98 |
return 0; |
| 99 |
#else |
| 100 |
ll = PyLong_AsLongLong(obj); |
| 101 |
if (PyErr_Occurred()) { |
| 102 |
return -1; |
| 103 |
} |
| 104 |
if (ll < (long long) CURL_SOCKET_BAD || ll > INT_MAX) { |
| 105 |
PyErr_SetString(PyExc_OverflowError, "socket value is out of range"); |
| 106 |
return -1; |
| 107 |
} |
| 108 |
*sockfd = (curl_socket_t) ll; |
| 109 |
return 0; |
| 110 |
#endif |
| 111 |
} |
| 112 |
|
| 113 |
static PyObject * |
| 114 |
create_error_object(CurlObject *self, int code) |
| 115 |
{ |
| 116 |
PyObject *s, *v; |
| 117 |
|
| 118 |
if (strlen(self->error)) { |
| 119 |
s = PyText_FromString_Ignore(self->error); |
| 120 |
if (s == NULL) { |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
} else { |
| 124 |
s = PyText_FromString_Ignore(curl_easy_strerror(code)); |
| 125 |
if (s == NULL) { |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
} |
| 129 |
v = Py_BuildValue("(iO)", code, s); |
| 130 |
if (v == NULL) { |
| 131 |
Py_DECREF(s); |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
return v; |
| 135 |
} |
| 136 |
|
| 137 |
PYCURL_INTERNAL void |
| 138 |
create_and_set_error_object(CurlObject *self, int code) |
| 139 |
{ |
| 140 |
PyObject *e; |
| 141 |
|
| 142 |
self->error[sizeof(self->error) - 1] = 0; |
| 143 |
e = create_error_object(self, code); |
| 144 |
if (e != NULL) { |
| 145 |
PyErr_SetObject(ErrorObject, e); |
| 146 |
Py_DECREF(e); |
| 147 |
} |
| 148 |
} |