Skip to content

Commit f841aa6

Browse files
committed
Add a simple test of the METH_CLASS and METH_STATIC flags for type methods.
1 parent 4157ffb commit f841aa6

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Lib/test/test_descr.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,20 @@ def f(cls, arg): return (cls, arg)
12141214
vereq(ff.__get__(0, int)(42), (int, 42))
12151215
vereq(ff.__get__(0)(42), (int, 42))
12161216

1217+
def classmethods_in_c():
1218+
if verbose: print "Testing C-based class methods..."
1219+
import xxsubtype as spam
1220+
a = (1, 2, 3)
1221+
d = {'abc': 123}
1222+
x, a1, d1 = spam.spamlist.classmeth(*a, **d)
1223+
veris(x, None)
1224+
vereq((spam.spamlist,) + a, a1)
1225+
vereq(d, d1)
1226+
x, a1, d1 = spam.spamlist().classmeth(*a, **d)
1227+
veris(x, None)
1228+
vereq((spam.spamlist,) + a, a1)
1229+
vereq(d, d1)
1230+
12171231
def staticmethods():
12181232
if verbose: print "Testing static methods..."
12191233
class C(object):
@@ -1231,6 +1245,20 @@ class D(C):
12311245
vereq(d.foo(1), (d, 1))
12321246
vereq(D.foo(d, 1), (d, 1))
12331247

1248+
def staticmethods_in_c():
1249+
if verbose: print "Testing C-based static methods..."
1250+
import xxsubtype as spam
1251+
a = (1, 2, 3)
1252+
d = {"abc": 123}
1253+
x, a1, d1 = spam.spamlist.staticmeth(*a, **d)
1254+
veris(x, None)
1255+
vereq(a, a1)
1256+
vereq(d, d1)
1257+
x, a1, d2 = spam.spamlist().staticmeth(*a, **d)
1258+
veris(x, None)
1259+
vereq(a, a1)
1260+
vereq(d, d1)
1261+
12341262
def classic():
12351263
if verbose: print "Testing classic classes..."
12361264
class C:
@@ -2884,7 +2912,9 @@ def test_main():
28842912
dynamics()
28852913
errors()
28862914
classmethods()
2915+
classmethods_in_c()
28872916
staticmethods()
2917+
staticmethods_in_c()
28882918
classic()
28892919
compattr()
28902920
newslot()

Modules/xxsubtype.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,39 @@ spamlist_setstate(spamlistobject *self, PyObject *args)
4343
return Py_None;
4444
}
4545

46+
static PyObject *
47+
spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
48+
{
49+
PyObject *result = PyTuple_New(3);
50+
51+
if (result != NULL) {
52+
if (self == NULL)
53+
self = Py_None;
54+
if (kw == NULL)
55+
kw = Py_None;
56+
Py_INCREF(self);
57+
PyTuple_SET_ITEM(result, 0, self);
58+
Py_INCREF(args);
59+
PyTuple_SET_ITEM(result, 1, args);
60+
Py_INCREF(kw);
61+
PyTuple_SET_ITEM(result, 2, kw);
62+
}
63+
return result;
64+
}
65+
4666
static PyMethodDef spamlist_methods[] = {
4767
{"getstate", (PyCFunction)spamlist_getstate, METH_VARARGS,
4868
"getstate() -> state"},
4969
{"setstate", (PyCFunction)spamlist_setstate, METH_VARARGS,
5070
"setstate(state)"},
71+
/* These entries differ only in the flags; they are used by the tests
72+
in test.test_descr. */
73+
{"classmeth", (PyCFunction)spamlist_specialmeth,
74+
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
75+
"classmeth(*args, **kw)"},
76+
{"staticmeth", (PyCFunction)spamlist_specialmeth,
77+
METH_VARARGS | METH_KEYWORDS | METH_STATIC,
78+
"staticmeth(*args, **kw)"},
5179
{NULL, NULL},
5280
};
5381

0 commit comments

Comments
 (0)