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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#include "pycurl.h"
#if PY_MAJOR_VERSION >= 3
PYCURL_INTERNAL PyObject *
my_getattro(PyObject *co, PyObject *name, PyObject *dict1, PyObject *dict2, PyMethodDef *m)
{
PyObject *v = NULL;
if( dict1 != NULL )
v = PyDict_GetItem(dict1, name);
if( v == NULL && dict2 != NULL )
v = PyDict_GetItem(dict2, name);
if( v != NULL )
{
Py_INCREF(v);
return v;
}
PyErr_Format(PyExc_AttributeError, "trying to obtain a non-existing attribute: %U", name);
return NULL;
}
PYCURL_INTERNAL int
my_setattro(PyObject **dict, PyObject *name, PyObject *v)
{
if( *dict == NULL )
{
*dict = PyDict_New();
if( *dict == NULL )
return -1;
}
if (v != NULL)
return PyDict_SetItem(*dict, name, v);
else {
int v = PyDict_DelItem(*dict, name);
if (v != 0) {
/* need to convert KeyError to AttributeError */
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_Format(PyExc_AttributeError, "trying to delete a non-existing attribute: %U", name);
}
}
return v;
}
}
#else /* PY_MAJOR_VERSION >= 3 */
PYCURL_INTERNAL int
my_setattr(PyObject **dict, char *name, PyObject *v)
{
if (v == NULL) {
int rv = -1;
if (*dict != NULL)
rv = PyDict_DelItemString(*dict, name);
if (rv < 0)
PyErr_Format(PyExc_AttributeError, "trying to delete a non-existing attribute: %s", name);
return rv;
}
if (*dict == NULL) {
*dict = PyDict_New();
if (*dict == NULL)
return -1;
}
return PyDict_SetItemString(*dict, name, v);
}
PYCURL_INTERNAL PyObject *
my_getattr(PyObject *co, char *name, PyObject *dict1, PyObject *dict2, PyMethodDef *m)
{
PyObject *v = NULL;
if (v == NULL && dict1 != NULL)
v = PyDict_GetItemString(dict1, name);
if (v == NULL && dict2 != NULL)
v = PyDict_GetItemString(dict2, name);
if (v != NULL) {
Py_INCREF(v);
return v;
}
return Py_FindMethod(m, co, name);
}
#endif /* PY_MAJOR_VERSION >= 3 */
PYCURL_INTERNAL int
PyListOrTuple_Check(PyObject *v)
{
int result;
if (PyList_Check(v)) {
result = PYLISTORTUPLE_LIST;
} else if (PyTuple_Check(v)) {
result = PYLISTORTUPLE_TUPLE;
} else {
result = PYLISTORTUPLE_OTHER;
}
return result;
}
PYCURL_INTERNAL Py_ssize_t
PyListOrTuple_Size(PyObject *v, int which)
{
switch (which) {
case PYLISTORTUPLE_LIST:
return PyList_Size(v);
case PYLISTORTUPLE_TUPLE:
return PyTuple_Size(v);
default:
assert(0);
return 0;
}
}
PYCURL_INTERNAL PyObject *
PyListOrTuple_GetItem(PyObject *v, Py_ssize_t i, int which)
{
switch (which) {
case PYLISTORTUPLE_LIST:
return PyList_GetItem(v, i);
case PYLISTORTUPLE_TUPLE:
return PyTuple_GetItem(v, i);
default:
assert(0);
return NULL;
}
}
/* vi:ts=4:et:nowrap
*/
|