1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#include "pycurl.h"
/*************************************************************************
// python utility functions
**************************************************************************/
PYCURL_INTERNAL int
PyText_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length, PyObject **encoded_obj)
{
if (PyByteStr_Check(obj)) {
*encoded_obj = NULL;
return PyByteStr_AsStringAndSize(obj, buffer, length);
} else {
int rv;
assert(PyUnicode_Check(obj));
*encoded_obj = PyUnicode_AsEncodedString(obj, "ascii", "strict");
if (*encoded_obj == NULL) {
return -1;
}
rv = PyByteStr_AsStringAndSize(*encoded_obj, buffer, length);
if (rv != 0) {
/* If we free the object, pointer must be reset to NULL */
Py_CLEAR(*encoded_obj);
}
return rv;
}
}
/* Like PyString_AsString(), but set an exception if the string contains
* embedded NULs. Actually PyString_AsStringAndSize() already does that for
* us if the `len' parameter is NULL - see Objects/stringobject.c.
*/
PYCURL_INTERNAL char *
PyText_AsString_NoNUL(PyObject *obj, PyObject **encoded_obj)
{
char *s = NULL;
Py_ssize_t r;
r = PyText_AsStringAndSize(obj, &s, NULL, encoded_obj);
if (r != 0)
return NULL; /* exception already set */
assert(s != NULL);
return s;
}
/* Returns true if the object is of a type that can be given to
* curl_easy_setopt and such - either a byte string or a Unicode string
* with ASCII code points only.
*/
#if PY_MAJOR_VERSION >= 3
PYCURL_INTERNAL int
PyText_Check(PyObject *o)
{
return PyUnicode_Check(o) || PyBytes_Check(o);
}
#else
PYCURL_INTERNAL int
PyText_Check(PyObject *o)
{
return PyUnicode_Check(o) || PyString_Check(o);
}
#endif
PYCURL_INTERNAL PyObject *
PyText_FromString_Ignore(const char *string)
{
PyObject *v;
#if PY_MAJOR_VERSION >= 3
PyObject *u;
v = Py_BuildValue("y", string);
if (v == NULL) {
return NULL;
}
u = PyUnicode_FromEncodedObject(v, NULL, "replace");
Py_DECREF(v);
return u;
#else
v = Py_BuildValue("s", string);
return v;
#endif
}
|