Skip to content

Commit 7e2cbb5

Browse files
committed
More attrwrapper-instead-of-dict support
Specifically, when passed to PyArg_ParseTuple when a dict argument is asked for.
1 parent a20593e commit 7e2cbb5

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

from_cpython/Python/getargs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ int _PyArg_ParseSingle_SizeT(PyObject* obj, int arg_idx, const char* fname, cons
619619
return r;
620620
}
621621

622+
extern PyTypeObject* attrwrapper_cls;
623+
622624
/* Convert a non-tuple argument. Return NULL if conversion went OK,
623625
or a string with a message describing the failure. The message is
624626
formatted as "must be <desired type>, not <actual type>".
@@ -1279,7 +1281,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
12791281
type = va_arg(*p_va, PyTypeObject*);
12801282
p = va_arg(*p_va, PyObject **);
12811283
format++;
1282-
if (PyType_IsSubtype(arg->ob_type, type))
1284+
if (PyType_IsSubtype(arg->ob_type, type) || /* Pyston hack */ (arg->ob_type == attrwrapper_cls && type == &PyDict_Type))
12831285
*p = arg;
12841286
else
12851287
return converterr(type->tp_name, arg, msgbuf, bufsize);

test/test_extension/api_test.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ set_size(PyObject *self, PyObject *so)
66
return Py_BuildValue("i", PySet_Size(so));
77
}
88

9+
static PyObject*
10+
test_attrwrapper_parse(PyObject *self, PyObject* args) {
11+
PyObject* d;
12+
int r = PyArg_ParseTuple(args, "O!", &PyDict_Type, &d);
13+
if (!r)
14+
return NULL;
15+
Py_RETURN_NONE;
16+
}
17+
918
static PyMethodDef TestMethods[] = {
1019
{"set_size", set_size, METH_O, "Get set size by PySet_Size." },
20+
{"test_attrwrapper_parse", test_attrwrapper_parse, METH_VARARGS, "Test PyArg_ParseTuple for attrwrappers." },
1121
{NULL, NULL, 0, NULL} /* Sentinel */
1222
};
1323

test/tests/attrwrappers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# import ctypes
2+
#
3+
# print ctypes.pythonapi.PyArg_ParseTuple(globals()
4+
5+
import api_test
6+
7+
api_test.test_attrwrapper_parse(globals())
8+
def f():
9+
pass
10+
api_test.test_attrwrapper_parse(f.__dict__)

0 commit comments

Comments
 (0)