@@ -16,6 +16,9 @@ call_pyobject_print(PyObject *self, PyObject * args)
1616 }
1717
1818 fp = Py_fopen (filename , "w+" );
19+ if (fp == NULL ) {
20+ return NULL ;
21+ }
1922
2023 if (Py_IsTrue (print_raw )) {
2124 flags = Py_PRINT_RAW ;
@@ -32,17 +35,15 @@ call_pyobject_print(PyObject *self, PyObject * args)
3235}
3336
3437static PyObject *
35- pyobject_print_null (PyObject * self , PyObject * args )
38+ pyobject_print_null (PyObject * self , PyObject * filename )
3639{
37- PyObject * filename ;
3840 FILE * fp ;
3941
40- if (!PyArg_UnpackTuple (args , "call_pyobject_print" , 1 , 1 , & filename )) {
42+ fp = Py_fopen (filename , "w+" );
43+ if (fp == NULL ) {
4144 return NULL ;
4245 }
4346
44- fp = Py_fopen (filename , "w+" );
45-
4647 if (PyObject_Print (NULL , fp , 0 ) < 0 ) {
4748 fclose (fp );
4849 return NULL ;
@@ -54,26 +55,29 @@ pyobject_print_null(PyObject *self, PyObject *args)
5455}
5556
5657static PyObject *
57- pyobject_print_noref_object (PyObject * self , PyObject * args )
58+ pyobject_print_noref_object (PyObject * self , PyObject * filename )
5859{
5960 PyObject * test_string ;
60- PyObject * filename ;
6161 FILE * fp ;
6262 char correct_string [100 ];
6363
6464 test_string = PyUnicode_FromString ("Spam spam spam" );
65+ if (test_string == NULL ) {
66+ return NULL ;
67+ }
6568
6669 Py_SET_REFCNT (test_string , 0 );
6770
6871 PyOS_snprintf (correct_string , 100 , "<refcnt %zd at %p>" ,
6972 Py_REFCNT (test_string ), (void * )test_string );
7073
71- if (!PyArg_UnpackTuple (args , "call_pyobject_print" , 1 , 1 , & filename )) {
74+ fp = Py_fopen (filename , "w+" );
75+ if (fp == NULL ) {
76+ Py_SET_REFCNT (test_string , 1 );
77+ Py_DECREF (test_string );
7278 return NULL ;
7379 }
7480
75- fp = Py_fopen (filename , "w+" );
76-
7781 if (PyObject_Print (test_string , fp , 0 ) < 0 ){
7882 fclose (fp );
7983 Py_SET_REFCNT (test_string , 1 );
@@ -90,20 +94,22 @@ pyobject_print_noref_object(PyObject *self, PyObject *args)
9094}
9195
9296static PyObject *
93- pyobject_print_os_error (PyObject * self , PyObject * args )
97+ pyobject_print_os_error (PyObject * self , PyObject * filename )
9498{
9599 PyObject * test_string ;
96- PyObject * filename ;
97100 FILE * fp ;
98101
99102 test_string = PyUnicode_FromString ("Spam spam spam" );
100-
101- if (!PyArg_UnpackTuple (args , "call_pyobject_print" , 1 , 1 , & filename )) {
103+ if (test_string == NULL ) {
102104 return NULL ;
103105 }
104106
105107 // open file in read mode to induce OSError
106108 fp = Py_fopen (filename , "r" );
109+ if (fp == NULL ) {
110+ Py_DECREF (test_string );
111+ return NULL ;
112+ }
107113
108114 if (PyObject_Print (test_string , fp , 0 ) < 0 ) {
109115 fclose (fp );
@@ -582,9 +588,9 @@ pysentinel_checkexact(PyObject *self, PyObject *obj)
582588
583589static PyMethodDef test_methods [] = {
584590 {"call_pyobject_print" , call_pyobject_print , METH_VARARGS },
585- {"pyobject_print_null" , pyobject_print_null , METH_VARARGS },
586- {"pyobject_print_noref_object" , pyobject_print_noref_object , METH_VARARGS },
587- {"pyobject_print_os_error" , pyobject_print_os_error , METH_VARARGS },
591+ {"pyobject_print_null" , pyobject_print_null , METH_O },
592+ {"pyobject_print_noref_object" , pyobject_print_noref_object , METH_O },
593+ {"pyobject_print_os_error" , pyobject_print_os_error , METH_O },
588594 {"pyobject_clear_weakrefs_no_callbacks" , pyobject_clear_weakrefs_no_callbacks , METH_O },
589595 {"pyobject_enable_deferred_refcount" , pyobject_enable_deferred_refcount , METH_O },
590596 {"pyobject_is_unique_temporary" , pyobject_is_unique_temporary , METH_O },
0 commit comments