| 1 |
from __future__ import print_function |
| 2 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 3 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 4 |
|
| 5 |
class AppTestGetargs(AppTestCpythonExtensionBase): |
| 6 |
def w_import_parser(self, implementation, argstyle='METH_VARARGS', |
| 7 |
PY_SSIZE_T_CLEAN=False): |
| 8 |
mod = self.import_extension( |
| 9 |
'modname', [('funcname', argstyle, implementation)], |
| 10 |
PY_SSIZE_T_CLEAN=PY_SSIZE_T_CLEAN) |
| 11 |
return mod.funcname |
| 12 |
|
| 13 |
def test_pyarg_parse_int(self): |
| 14 |
""" |
| 15 |
The `i` format specifier can be used to parse an integer. |
| 16 |
""" |
| 17 |
oneargint = self.import_parser( |
| 18 |
''' |
| 19 |
int l; |
| 20 |
if (!PyArg_ParseTuple(args, "i", &l)) { |
| 21 |
return NULL; |
| 22 |
} |
| 23 |
return PyLong_FromLong(l); |
| 24 |
''') |
| 25 |
assert oneargint(1) == 1 |
| 26 |
raises(TypeError, oneargint, None) |
| 27 |
raises(TypeError, oneargint) |
| 28 |
|
| 29 |
|
| 30 |
def test_pyarg_parse_fromname(self): |
| 31 |
""" |
| 32 |
The name of the function parsing the arguments can be given after a `:` |
| 33 |
in the argument format string. |
| 34 |
""" |
| 35 |
oneargandform = self.import_parser( |
| 36 |
''' |
| 37 |
int l; |
| 38 |
if (!PyArg_ParseTuple(args, "i:oneargandstuff", &l)) { |
| 39 |
return NULL; |
| 40 |
} |
| 41 |
return PyLong_FromLong(l); |
| 42 |
''') |
| 43 |
assert oneargandform(1) == 1 |
| 44 |
|
| 45 |
|
| 46 |
def test_pyarg_parse_object(self): |
| 47 |
""" |
| 48 |
The `O` format specifier can be used to parse an arbitrary object. |
| 49 |
""" |
| 50 |
oneargobject = self.import_parser( |
| 51 |
''' |
| 52 |
PyObject *obj; |
| 53 |
if (!PyArg_ParseTuple(args, "O", &obj)) { |
| 54 |
return NULL; |
| 55 |
} |
| 56 |
Py_INCREF(obj); |
| 57 |
return obj; |
| 58 |
''') |
| 59 |
sentinel = object() |
| 60 |
res = oneargobject(sentinel) |
| 61 |
assert res is sentinel |
| 62 |
|
| 63 |
def test_pyarg_parse_restricted_object_type(self): |
| 64 |
""" |
| 65 |
The `O!` format specifier can be used to parse an object of a particular |
| 66 |
type. |
| 67 |
""" |
| 68 |
oneargobjectandlisttype = self.import_parser( |
| 69 |
''' |
| 70 |
PyObject *obj; |
| 71 |
if (!PyArg_ParseTuple(args, "O!", &PyList_Type, &obj)) { |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
Py_INCREF(obj); |
| 75 |
return obj; |
| 76 |
''') |
| 77 |
sentinel = object() |
| 78 |
raises(TypeError, "oneargobjectandlisttype(sentinel)") |
| 79 |
sentinel = [] |
| 80 |
res = oneargobjectandlisttype(sentinel) |
| 81 |
assert res is sentinel |
| 82 |
|
| 83 |
|
| 84 |
def test_pyarg_parse_one_optional(self): |
| 85 |
""" |
| 86 |
An object corresponding to a format specifier after a `|` in the |
| 87 |
argument format string is optional and may be passed or not. |
| 88 |
""" |
| 89 |
twoopt = self.import_parser( |
| 90 |
''' |
| 91 |
PyObject *a; |
| 92 |
PyObject *b = NULL; |
| 93 |
if (!PyArg_ParseTuple(args, "O|O", &a, &b)) { |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
if (b) |
| 97 |
Py_INCREF(b); |
| 98 |
else |
| 99 |
b = PyLong_FromLong(42); |
| 100 |
/* return an owned reference */ |
| 101 |
return b; |
| 102 |
''') |
| 103 |
assert twoopt(1) == 42 |
| 104 |
assert twoopt(1, 2) == 2 |
| 105 |
raises(TypeError, twoopt, 1, 2, 3) |
| 106 |
|
| 107 |
|
| 108 |
def test_pyarg_parse_string_py_buffer(self): |
| 109 |
""" |
| 110 |
The `s*` format specifier can be used to parse a str into a Py_buffer |
| 111 |
structure containing a pointer to the string data and the length of the |
| 112 |
string data. |
| 113 |
""" |
| 114 |
pybuffer = self.import_parser( |
| 115 |
''' |
| 116 |
Py_buffer buf; |
| 117 |
PyObject *result; |
| 118 |
if (!PyArg_ParseTuple(args, "s*", &buf)) { |
| 119 |
return NULL; |
| 120 |
} |
| 121 |
result = PyBytes_FromStringAndSize(buf.buf, buf.len); |
| 122 |
PyBuffer_Release(&buf); |
| 123 |
return result; |
| 124 |
''') |
| 125 |
assert b'foo\0bar\0baz' == pybuffer(b'foo\0bar\0baz') |
| 126 |
|
| 127 |
assert b'foo\0bar\0baz' == pybuffer(bytearray(b'foo\0bar\0baz')) |
| 128 |
|
| 129 |
|
| 130 |
def test_pyarg_parse_string_fails(self): |
| 131 |
""" |
| 132 |
Test the failing case of PyArg_ParseTuple(): it must not keep |
| 133 |
a reference on the PyObject passed in. |
| 134 |
""" |
| 135 |
pybuffer = self.import_parser( |
| 136 |
''' |
| 137 |
Py_buffer buf1, buf2, buf3; |
| 138 |
if (!PyArg_ParseTuple(args, "s*s*s*", &buf1, &buf2, &buf3)) { |
| 139 |
return NULL; |
| 140 |
} |
| 141 |
Py_FatalError("should not get there"); |
| 142 |
return NULL; |
| 143 |
''') |
| 144 |
freed = [] |
| 145 |
class freestring(bytes): |
| 146 |
def __del__(self): |
| 147 |
freed.append('x') |
| 148 |
raises(TypeError, pybuffer, |
| 149 |
freestring(b"string"), freestring(b"other string"), 42) |
| 150 |
self.debug_collect() |
| 151 |
|
| 152 |
|
| 153 |
assert freed == ['x', 'x'] |
| 154 |
|
| 155 |
|
| 156 |
def test_pyarg_parse_charbuf_and_length(self): |
| 157 |
""" |
| 158 |
The `s#` format specifier can be used to parse a read-only 8-bit |
| 159 |
character buffer into a char* and int giving its length in bytes. |
| 160 |
""" |
| 161 |
charbuf = self.import_parser( |
| 162 |
''' |
| 163 |
char *buf; |
| 164 |
int len; |
| 165 |
if (!PyArg_ParseTuple(args, "s#", &buf, &len)) { |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
return PyBytes_FromStringAndSize(buf, len); |
| 169 |
''', PY_SSIZE_T_CLEAN=True) |
| 170 |
raises(TypeError, "charbuf(10)") |
| 171 |
|
| 172 |
|
| 173 |
def test_pyarg_parse_with_py_ssize_t(self): |
| 174 |
charbuf = self.import_parser( |
| 175 |
''' |
| 176 |
char *buf; |
| 177 |
Py_ssize_t y = -1; |
| 178 |
if (!PyArg_ParseTuple(args, "s#", &buf, &y)) { |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
return PyLong_FromSsize_t(y); |
| 182 |
''', PY_SSIZE_T_CLEAN=True) |
| 183 |
assert charbuf(b'12345') == 5 |
| 184 |
|
| 185 |
def test_pyarg_parse_with_py_ssize_t_bytes(self): |
| 186 |
charbuf = self.import_parser( |
| 187 |
''' |
| 188 |
char *buf; |
| 189 |
Py_ssize_t len = -1; |
| 190 |
if (!PyArg_ParseTuple(args, "y#", &buf, &len)) { |
| 191 |
return NULL; |
| 192 |
} |
| 193 |
return PyBytes_FromStringAndSize(buf, len); |
| 194 |
''', PY_SSIZE_T_CLEAN=True) |
| 195 |
assert type(charbuf(b'12345')) is bytes |
| 196 |
assert charbuf(b'12345') == b'12345' |
| 197 |
|
| 198 |
def test_getargs_keywords(self): |
| 199 |
|
| 200 |
module = self.import_extension('foo', [ |
| 201 |
("getargs_keywords", "METH_KEYWORDS | METH_VARARGS", |
| 202 |
''' |
| 203 |
static char *keywords[] = {"arg1","arg2","arg3","arg4","arg5", NULL}; |
| 204 |
static char *fmt="(ii)i|(i(ii))(iii)i"; |
| 205 |
int int_args[10]={-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 206 |
|
| 207 |
if (!PyArg_ParseTupleAndKeywords(args, kwargs, fmt, keywords, |
| 208 |
&int_args[0], &int_args[1], &int_args[2], &int_args[3], |
| 209 |
&int_args[4], &int_args[5], &int_args[6], &int_args[7], |
| 210 |
&int_args[8], &int_args[9])) |
| 211 |
return NULL; |
| 212 |
return Py_BuildValue("iiiiiiiiii", |
| 213 |
int_args[0], int_args[1], int_args[2], int_args[3], int_args[4], |
| 214 |
int_args[5], int_args[6], int_args[7], int_args[8], int_args[9] |
| 215 |
); |
| 216 |
''')]) |
| 217 |
raises(TypeError, module.getargs_keywords, (1,2), 3, (4,(5,6)), (7,8,9), **{'\uDC80': 10}) |
| 218 |
|
| 219 |
def test_y(self): |
| 220 |
module = self.import_extension('foo', [ |
| 221 |
("getargs_y_star", "METH_VARARGS", |
| 222 |
""" |
| 223 |
Py_buffer buffer; |
| 224 |
PyObject *bytes; |
| 225 |
if (!PyArg_ParseTuple(args, "y*", &buffer)) |
| 226 |
return NULL; |
| 227 |
bytes = PyBytes_FromStringAndSize(buffer.buf, buffer.len); |
| 228 |
PyBuffer_Release(&buffer); |
| 229 |
return bytes; |
| 230 |
"""), |
| 231 |
("getargs_y", "METH_VARARGS", |
| 232 |
""" |
| 233 |
char *str; |
| 234 |
if (!PyArg_ParseTuple(args, "y", &str)) |
| 235 |
return NULL; |
| 236 |
return PyBytes_FromString(str); |
| 237 |
"""), |
| 238 |
]) |
| 239 |
NONCONTIG_WRITABLE = memoryview(bytearray(b'noncontig'))[::-2] |
| 240 |
NONCONTIG_READONLY = memoryview(b'noncontig')[::-2] |
| 241 |
with raises(BufferError): |
| 242 |
module.getargs_y_star(NONCONTIG_WRITABLE) |
| 243 |
with raises(BufferError): |
| 244 |
module.getargs_y_star(NONCONTIG_READONLY) |
| 245 |
assert module.getargs_y(b'bytes') == b'bytes' |
| 246 |
with raises(TypeError): |
| 247 |
module.getargs_y(memoryview(b'memoryview')) |
| 248 |
with raises(TypeError): |
| 249 |
module.getargs_y(bytearray(b'memoryview')) |
| 250 |
|
| 251 |
def test_d(self): |
| 252 |
module = self.import_extension('foo', [ |
| 253 |
("getargs_d", "METH_VARARGS", |
| 254 |
""" |
| 255 |
double d; |
| 256 |
if (!PyArg_ParseTuple(args, "d", &d)) |
| 257 |
return NULL; |
| 258 |
return PyFloat_FromDouble(d); |
| 259 |
"""), |
| 260 |
]) |
| 261 |
class FloatSubclass2(float): |
| 262 |
def __float__(self): |
| 263 |
return 4.25 |
| 264 |
|
| 265 |
assert module.getargs_d(FloatSubclass2(7.5)) == 7.5 |
| 266 |
|
| 267 |
def test_d_index(self): |
| 268 |
|
| 269 |
|
| 270 |
module = self.import_extension('foo', [ |
| 271 |
("getargs_d", "METH_VARARGS", |
| 272 |
""" |
| 273 |
double d; |
| 274 |
if (!PyArg_ParseTuple(args, "d", &d)) |
| 275 |
return NULL; |
| 276 |
return PyFloat_FromDouble(d); |
| 277 |
"""), |
| 278 |
]) |
| 279 |
class Index: |
| 280 |
def __index__(self): |
| 281 |
return 99 |
| 282 |
assert module.getargs_d(Index()) == 99.0 |
| 283 |
|
| 284 |
def test_f_index(self): |
| 285 |
|
| 286 |
module = self.import_extension('foo', [ |
| 287 |
("getargs_f", "METH_VARARGS", |
| 288 |
""" |
| 289 |
float f; |
| 290 |
if (!PyArg_ParseTuple(args, "f", &f)) |
| 291 |
return NULL; |
| 292 |
return PyFloat_FromDouble((double)f); |
| 293 |
"""), |
| 294 |
]) |
| 295 |
class Index: |
| 296 |
def __index__(self): |
| 297 |
return 99 |
| 298 |
assert module.getargs_f(Index()) == 99.0 |
| 299 |
|
| 300 |
def test_nested_tuple_es(self): |
| 301 |
|
| 302 |
|
| 303 |
module = self.import_extension('foo', [ |
| 304 |
("getargs_nested_es", "METH_VARARGS", |
| 305 |
""" |
| 306 |
const char *encoding = ""; |
| 307 |
char *buf = NULL; |
| 308 |
if (!PyArg_ParseTuple(args, "(es)", encoding, &buf)) |
| 309 |
return NULL; |
| 310 |
PyObject *result = PyUnicode_FromString(buf); |
| 311 |
PyMem_Free(buf); |
| 312 |
return result; |
| 313 |
"""), |
| 314 |
]) |
| 315 |
|
| 316 |
|
| 317 |
raises(LookupError, module.getargs_nested_es, ('a',)) |
| 318 |
|
| 319 |
exc = raises(TypeError, module.getargs_nested_es, ()) |
| 320 |
assert "length 1" in str(exc.value) |
| 321 |
assert "not 0" in str(exc.value) |
| 322 |
|