Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aae4db4

Browse files
committed
Test all functions
run_tests now displays the number of tests.
1 parent 1c50968 commit aae4db4

2 files changed

Lines changed: 80 additions & 12 deletions

File tree

tests/run_tests.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,10 @@ def import_tests():
6363
return test_pythoncapi_compat
6464

6565

66-
def _run_tests(testmod, verbose):
67-
for name in dir(testmod):
68-
if not name.startswith("test"):
69-
continue
66+
def _run_tests(tests, verbose):
67+
for name, test_func in tests:
7068
if verbose:
7169
print(f"{name}()", flush=True)
72-
test_func = getattr(testmod, name)
7370
test_func()
7471

7572

@@ -88,8 +85,12 @@ def run_tests(testmod):
8885

8986
check_refleak = hasattr(sys, 'gettotalrefcount')
9087

88+
tests = [(name, getattr(testmod, name))
89+
for name in dir(testmod)
90+
if name.startswith("test")]
91+
9192
def test_func():
92-
_run_tests(testmod, VERBOSE)
93+
_run_tests(tests, VERBOSE)
9394

9495
if check_refleak:
9596
_check_refleak(test_func)
@@ -101,7 +102,8 @@ def test_func():
101102

102103
ver = sys.version_info
103104
build = 'debug' if hasattr(sys, 'gettotalrefcount') else 'release'
104-
msg = f"Python {ver.major}.{ver.minor} ({build} build): TESTS OK!"
105+
msg = f"{len(tests)} tests succeeded!"
106+
msg = f"Python {ver.major}.{ver.minor} ({build} build): {msg}"
105107
if check_refleak:
106108
msg = f"{msg} (no reference leak detected)"
107109
print(msg)

tests/test_pythoncapi_compat.c

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// FIXME: test PyObject_CallNoArgs()
2-
// FIXME: test PyModule_AddType()
3-
// FIXME: test PyObject_GC_IsTracked()
4-
// FIXME: test PyObject_GC_IsFinalized()
5-
61
// Always enable assertions
72
#undef NDEBUG
83

@@ -103,11 +98,82 @@ test_interpreter(PyObject *self, PyObject *ignored)
10398
}
10499

105100

101+
static PyObject *
102+
test_calls(PyObject *self, PyObject *ignored)
103+
{
104+
PyObject *func = (PyObject *)&PyUnicode_Type;
105+
106+
// test PyObject_CallNoArgs(): str() returns an empty string
107+
PyObject *res = PyObject_CallNoArgs(func);
108+
if (res == NULL) {
109+
return NULL;
110+
}
111+
assert(PyUnicode_Check(res));
112+
Py_DECREF(res);
113+
114+
Py_RETURN_NONE;
115+
}
116+
117+
118+
static PyObject *
119+
test_gc(PyObject *self, PyObject *ignored)
120+
{
121+
PyObject *tuple = PyTuple_New(1);
122+
Py_INCREF(Py_None);
123+
PyTuple_SET_ITEM(tuple, 0, Py_None);
124+
125+
// test PyObject_GC_IsTracked()
126+
int tracked = PyObject_GC_IsTracked(tuple);
127+
assert(tracked);
128+
129+
// test PyObject_GC_IsFinalized()
130+
int finalized = PyObject_GC_IsFinalized(tuple);
131+
assert(!finalized);
132+
133+
Py_DECREF(tuple);
134+
Py_RETURN_NONE;
135+
}
136+
137+
138+
static PyObject *
139+
test_module(PyObject *self, PyObject *ignored)
140+
{
141+
PyObject *module = self;
142+
assert(PyModule_Check(module));
143+
144+
// test PyModule_AddType()
145+
PyTypeObject *type = &PyUnicode_Type;
146+
Py_ssize_t refcnt = Py_REFCNT(type);
147+
148+
if (PyModule_AddType(module, type) < 0) {
149+
return NULL;
150+
}
151+
assert(Py_REFCNT(type) == refcnt + 1);
152+
153+
PyObject *attr = PyObject_GetAttrString(module, "str");
154+
if (attr == NULL) {
155+
return NULL;
156+
}
157+
assert(attr == (PyObject *)type);
158+
Py_DECREF(attr);
159+
160+
if (PyObject_DelAttrString(module, "str") < 0) {
161+
return NULL;
162+
}
163+
assert(Py_REFCNT(type) == refcnt);
164+
165+
Py_RETURN_NONE;
166+
}
167+
168+
106169
static struct PyMethodDef methods[] = {
107170
{"test_set_funcs", test_set_funcs, METH_NOARGS},
108171
{"test_frame", test_frame, METH_NOARGS},
109172
{"test_thread_state", test_thread_state, METH_NOARGS},
110173
{"test_interpreter", test_interpreter, METH_NOARGS},
174+
{"test_calls", test_calls, METH_NOARGS},
175+
{"test_gc", test_gc, METH_NOARGS},
176+
{"test_module", test_module, METH_NOARGS},
111177
{NULL, NULL}
112178
};
113179

0 commit comments

Comments
 (0)