@@ -138,32 +138,25 @@ getargs_w_star(PyObject *self, PyObject *args)
138
138
}
139
139
140
140
static PyObject *
141
- test_empty_argparse (PyObject * self , PyObject * Py_UNUSED ( ignored ) )
141
+ getargs_empty (PyObject * self , PyObject * args , PyObject * kwargs )
142
142
{
143
143
/* 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
+
150
147
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
+ }
163
156
if (!result ) {
164
157
return NULL ;
165
158
}
166
- Py_RETURN_NONE ;
159
+ return PyLong_FromLong ( result ) ;
167
160
}
168
161
169
162
/* Test tuple argument processing */
@@ -354,90 +347,6 @@ getargs_K(PyObject *self, PyObject *args)
354
347
return PyLong_FromUnsignedLongLong (value );
355
348
}
356
349
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
-
441
350
static PyObject *
442
351
getargs_f (PyObject * self , PyObject * args )
443
352
{
@@ -718,104 +627,6 @@ getargs_et_hash(PyObject *self, PyObject *args)
718
627
return result ;
719
628
}
720
629
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
-
819
630
static PyObject *
820
631
gh_99240_clear_args (PyObject * self , PyObject * args )
821
632
{
@@ -869,17 +680,14 @@ static PyMethodDef test_methods[] = {
869
680
{"getargs_s_star" , getargs_s_star , METH_VARARGS },
870
681
{"getargs_tuple" , getargs_tuple , METH_VARARGS },
871
682
{"getargs_w_star" , getargs_w_star , METH_VARARGS },
683
+ {"getargs_empty" , _PyCFunction_CAST (getargs_empty ), METH_VARARGS |METH_KEYWORDS },
872
684
{"getargs_y" , getargs_y , METH_VARARGS },
873
685
{"getargs_y_hash" , getargs_y_hash , METH_VARARGS },
874
686
{"getargs_y_star" , getargs_y_star , METH_VARARGS },
875
687
{"getargs_z" , getargs_z , METH_VARARGS },
876
688
{"getargs_z_hash" , getargs_z_hash , METH_VARARGS },
877
689
{"getargs_z_star" , getargs_z_star , METH_VARARGS },
878
690
{"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 },
883
691
{"gh_99240_clear_args" , gh_99240_clear_args , METH_VARARGS },
884
692
{NULL },
885
693
};
0 commit comments