Skip to content

Commit e8c1df0

Browse files
sobolevnaisk
authored andcommitted
pythongh-110572: Remove test_* from _testcapi/getargs.c (pythonGH-111214)
1 parent 08c747b commit e8c1df0

File tree

4 files changed

+25
-214
lines changed

4 files changed

+25
-214
lines changed

Lib/test/test_capi/test_getargs.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,15 @@ def test_w_star(self):
871871
self.assertRaises(TypeError, getargs_w_star, NONCONTIG_WRITABLE)
872872
self.assertRaises(TypeError, getargs_w_star, NONCONTIG_READONLY)
873873

874+
def test_getargs_empty(self):
875+
from _testcapi import getargs_empty
876+
self.assertTrue(getargs_empty())
877+
self.assertRaises(TypeError, getargs_empty, 1)
878+
self.assertRaises(TypeError, getargs_empty, 1, 2, 3)
879+
self.assertRaises(TypeError, getargs_empty, a=1)
880+
self.assertRaises(TypeError, getargs_empty, a=1, b=2)
881+
self.assertRaises(TypeError, getargs_empty, 'x', 'y', 'z', a=1, b=2)
882+
874883

875884
class String_TestCase(unittest.TestCase):
876885
def test_C(self):
@@ -1306,11 +1315,5 @@ def test_nonascii_keywords(self):
13061315
parse((), {name2: 1, name3: 2}, '|OO', [name, name3])
13071316

13081317

1309-
class Test_testcapi(unittest.TestCase):
1310-
locals().update((name, getattr(_testcapi, name))
1311-
for name in dir(_testcapi)
1312-
if name.startswith('test_') and name.endswith('_code'))
1313-
1314-
13151318
if __name__ == "__main__":
13161319
unittest.main()

Lib/test/test_capi/test_misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ def test_gilstate_matches_current(self):
21592159
class Test_testcapi(unittest.TestCase):
21602160
locals().update((name, getattr(_testcapi, name))
21612161
for name in dir(_testcapi)
2162-
if name.startswith('test_') and not name.endswith('_code'))
2162+
if name.startswith('test_'))
21632163

21642164
# Suppress warning from PyUnicode_FromUnicode().
21652165
@warnings_helper.ignore_warnings(category=DeprecationWarning)

Modules/_testcapi/getargs.c

Lines changed: 14 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,25 @@ getargs_w_star(PyObject *self, PyObject *args)
138138
}
139139

140140
static PyObject *
141-
test_empty_argparse(PyObject *self, PyObject *Py_UNUSED(ignored))
141+
getargs_empty(PyObject *self, PyObject *args, PyObject *kwargs)
142142
{
143143
/* Test that formats can begin with '|'. See issue #4720. */
144-
PyObject *dict = NULL;
145-
static char *kwlist[] = {NULL};
146-
PyObject *tuple = PyTuple_New(0);
147-
if (!tuple) {
148-
return NULL;
149-
}
144+
assert(PyTuple_CheckExact(args));
145+
assert(kwargs == NULL || PyDict_CheckExact(kwargs));
146+
150147
int result;
151-
if (!(result = PyArg_ParseTuple(tuple, "|:test_empty_argparse"))) {
152-
goto done;
153-
}
154-
dict = PyDict_New();
155-
if (!dict) {
156-
goto done;
157-
}
158-
result = PyArg_ParseTupleAndKeywords(tuple, dict, "|:test_empty_argparse",
159-
kwlist);
160-
done:
161-
Py_DECREF(tuple);
162-
Py_XDECREF(dict);
148+
if (kwargs != NULL && PyDict_GET_SIZE(kwargs) > 0) {
149+
static char *kwlist[] = {NULL};
150+
result = PyArg_ParseTupleAndKeywords(args, kwargs, "|:getargs_empty",
151+
kwlist);
152+
}
153+
else {
154+
result = PyArg_ParseTuple(args, "|:getargs_empty");
155+
}
163156
if (!result) {
164157
return NULL;
165158
}
166-
Py_RETURN_NONE;
159+
return PyLong_FromLong(result);
167160
}
168161

169162
/* Test tuple argument processing */
@@ -354,90 +347,6 @@ getargs_K(PyObject *self, PyObject *args)
354347
return PyLong_FromUnsignedLongLong(value);
355348
}
356349

357-
/* This function not only tests the 'k' getargs code, but also the
358-
PyLong_AsUnsignedLongMask() function. */
359-
static PyObject *
360-
test_k_code(PyObject *self, PyObject *Py_UNUSED(ignored))
361-
{
362-
PyObject *tuple = PyTuple_New(1);
363-
if (tuple == NULL) {
364-
return NULL;
365-
}
366-
367-
/* a number larger than ULONG_MAX even on 64-bit platforms */
368-
PyObject *num = PyLong_FromString("FFFFFFFFFFFFFFFFFFFFFFFF", NULL, 16);
369-
if (num == NULL) {
370-
goto error;
371-
}
372-
373-
unsigned long value = PyLong_AsUnsignedLongMask(num);
374-
if (value == (unsigned long)-1 && PyErr_Occurred()) {
375-
Py_DECREF(num);
376-
goto error;
377-
}
378-
else if (value != ULONG_MAX) {
379-
Py_DECREF(num);
380-
PyErr_SetString(PyExc_AssertionError,
381-
"test_k_code: "
382-
"PyLong_AsUnsignedLongMask() returned wrong value for long 0xFFF...FFF");
383-
goto error;
384-
}
385-
386-
PyTuple_SET_ITEM(tuple, 0, num);
387-
388-
value = 0;
389-
if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
390-
goto error;
391-
}
392-
if (value != ULONG_MAX) {
393-
PyErr_SetString(PyExc_AssertionError,
394-
"test_k_code: k code returned wrong value for long 0xFFF...FFF");
395-
goto error;
396-
}
397-
398-
Py_DECREF(tuple); // also clears `num`
399-
tuple = PyTuple_New(1);
400-
if (tuple == NULL) {
401-
return NULL;
402-
}
403-
num = PyLong_FromString("-FFFFFFFF000000000000000042", NULL, 16);
404-
if (num == NULL) {
405-
goto error;
406-
}
407-
408-
value = PyLong_AsUnsignedLongMask(num);
409-
if (value == (unsigned long)-1 && PyErr_Occurred()) {
410-
Py_DECREF(num);
411-
goto error;
412-
}
413-
else if (value != (unsigned long)-0x42) {
414-
Py_DECREF(num);
415-
PyErr_SetString(PyExc_AssertionError,
416-
"test_k_code: "
417-
"PyLong_AsUnsignedLongMask() returned wrong value for long -0xFFF..000042");
418-
goto error;
419-
}
420-
421-
PyTuple_SET_ITEM(tuple, 0, num);
422-
423-
value = 0;
424-
if (!PyArg_ParseTuple(tuple, "k:test_k_code", &value)) {
425-
goto error;
426-
}
427-
if (value != (unsigned long)-0x42) {
428-
PyErr_SetString(PyExc_AssertionError,
429-
"test_k_code: k code returned wrong value for long -0xFFF..000042");
430-
goto error;
431-
}
432-
433-
Py_DECREF(tuple);
434-
Py_RETURN_NONE;
435-
436-
error:
437-
Py_DECREF(tuple);
438-
return NULL;
439-
}
440-
441350
static PyObject *
442351
getargs_f(PyObject *self, PyObject *args)
443352
{
@@ -718,104 +627,6 @@ getargs_et_hash(PyObject *self, PyObject *args)
718627
return result;
719628
}
720629

721-
/* Test the L code for PyArg_ParseTuple. This should deliver a long long
722-
for both long and int arguments. The test may leak a little memory if
723-
it fails.
724-
*/
725-
static PyObject *
726-
test_L_code(PyObject *self, PyObject *Py_UNUSED(ignored))
727-
{
728-
PyObject *tuple = PyTuple_New(1);
729-
if (tuple == NULL) {
730-
return NULL;
731-
}
732-
733-
PyObject *num = PyLong_FromLong(42);
734-
if (num == NULL) {
735-
goto error;
736-
}
737-
738-
PyTuple_SET_ITEM(tuple, 0, num);
739-
740-
long long value = -1;
741-
if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
742-
goto error;
743-
}
744-
if (value != 42) {
745-
PyErr_SetString(PyExc_AssertionError,
746-
"test_L_code: L code returned wrong value for long 42");
747-
goto error;
748-
}
749-
750-
Py_DECREF(tuple); // also clears `num`
751-
tuple = PyTuple_New(1);
752-
if (tuple == NULL) {
753-
return NULL;
754-
}
755-
num = PyLong_FromLong(42);
756-
if (num == NULL) {
757-
goto error;
758-
}
759-
760-
PyTuple_SET_ITEM(tuple, 0, num);
761-
762-
value = -1;
763-
if (!PyArg_ParseTuple(tuple, "L:test_L_code", &value)) {
764-
goto error;
765-
}
766-
if (value != 42) {
767-
PyErr_SetString(PyExc_AssertionError,
768-
"test_L_code: L code returned wrong value for int 42");
769-
goto error;
770-
}
771-
772-
Py_DECREF(tuple);
773-
Py_RETURN_NONE;
774-
775-
error:
776-
Py_DECREF(tuple);
777-
return NULL;
778-
}
779-
780-
/* Test the s and z codes for PyArg_ParseTuple.
781-
*/
782-
static PyObject *
783-
test_s_code(PyObject *self, PyObject *Py_UNUSED(ignored))
784-
{
785-
/* Unicode strings should be accepted */
786-
PyObject *tuple = PyTuple_New(1);
787-
if (tuple == NULL) {
788-
return NULL;
789-
}
790-
791-
PyObject *obj = PyUnicode_Decode("t\xeate", strlen("t\xeate"),
792-
"latin-1", NULL);
793-
if (obj == NULL) {
794-
goto error;
795-
}
796-
797-
PyTuple_SET_ITEM(tuple, 0, obj);
798-
799-
/* These two blocks used to raise a TypeError:
800-
* "argument must be string without null bytes, not str"
801-
*/
802-
char *value;
803-
if (!PyArg_ParseTuple(tuple, "s:test_s_code1", &value)) {
804-
goto error;
805-
}
806-
807-
if (!PyArg_ParseTuple(tuple, "z:test_s_code2", &value)) {
808-
goto error;
809-
}
810-
811-
Py_DECREF(tuple);
812-
Py_RETURN_NONE;
813-
814-
error:
815-
Py_DECREF(tuple);
816-
return NULL;
817-
}
818-
819630
static PyObject *
820631
gh_99240_clear_args(PyObject *self, PyObject *args)
821632
{
@@ -869,17 +680,14 @@ static PyMethodDef test_methods[] = {
869680
{"getargs_s_star", getargs_s_star, METH_VARARGS},
870681
{"getargs_tuple", getargs_tuple, METH_VARARGS},
871682
{"getargs_w_star", getargs_w_star, METH_VARARGS},
683+
{"getargs_empty", _PyCFunction_CAST(getargs_empty), METH_VARARGS|METH_KEYWORDS},
872684
{"getargs_y", getargs_y, METH_VARARGS},
873685
{"getargs_y_hash", getargs_y_hash, METH_VARARGS},
874686
{"getargs_y_star", getargs_y_star, METH_VARARGS},
875687
{"getargs_z", getargs_z, METH_VARARGS},
876688
{"getargs_z_hash", getargs_z_hash, METH_VARARGS},
877689
{"getargs_z_star", getargs_z_star, METH_VARARGS},
878690
{"parse_tuple_and_keywords", parse_tuple_and_keywords, METH_VARARGS},
879-
{"test_L_code", test_L_code, METH_NOARGS},
880-
{"test_empty_argparse", test_empty_argparse, METH_NOARGS},
881-
{"test_k_code", test_k_code, METH_NOARGS},
882-
{"test_s_code", test_s_code, METH_NOARGS},
883691
{"gh_99240_clear_args", gh_99240_clear_args, METH_VARARGS},
884692
{NULL},
885693
};

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ Modules/_testcapimodule.c make_exception_with_doc kwlist -
530530
Modules/_testcapimodule.c raise_SIGINT_then_send_None PyId_send -
531531
Modules/_testcapimodule.c slot_tp_del PyId___tp_del__ -
532532
Modules/_testcapimodule.c test_capsule buffer -
533-
Modules/_testcapimodule.c test_empty_argparse kwlist -
533+
Modules/_testcapimodule.c getargs_empty kwlist -
534534
Modules/_testcapimodule.c test_structmembers_new keywords -
535535
Modules/_testcapimodule.c getargs_s_hash_int keywords -
536536
Modules/_testcapimodule.c - g_dict_watch_events -

0 commit comments

Comments
 (0)