@@ -2001,6 +2001,109 @@ pyobject_bytes_from_null(PyObject *self, PyObject *Py_UNUSED(ignored))
2001
2001
return PyObject_Bytes (NULL );
2002
2002
}
2003
2003
2004
+ static PyObject *
2005
+ call_pyobject_print (PyObject * self , PyObject * args )
2006
+ {
2007
+ PyObject * object ;
2008
+ PyObject * filename ;
2009
+ PyObject * print_raw ;
2010
+ FILE * fp ;
2011
+ int flags = 0 ;
2012
+
2013
+ if (!PyArg_UnpackTuple (args , "call_pyobject_print" , 3 , 3 , & object , & filename , & print_raw )) {
2014
+ return NULL ;
2015
+ }
2016
+
2017
+ fp = _Py_fopen_obj (filename , "w+" );
2018
+
2019
+ if (Py_IsTrue (print_raw )) {
2020
+ flags = Py_PRINT_RAW ;
2021
+ }
2022
+
2023
+ if (PyObject_Print (object , fp , flags ) < 0 ) {
2024
+ return NULL ;
2025
+ }
2026
+
2027
+ fclose (fp );
2028
+
2029
+ Py_RETURN_NONE ;
2030
+ }
2031
+
2032
+ static PyObject *
2033
+ pyobject_print_null (PyObject * self , PyObject * args )
2034
+ {
2035
+ PyObject * filename ;
2036
+ FILE * fp ;
2037
+
2038
+ if (!PyArg_UnpackTuple (args , "call_pyobject_print" , 1 , 1 , & filename )) {
2039
+ return NULL ;
2040
+ }
2041
+
2042
+ fp = _Py_fopen_obj (filename , "w+" );
2043
+
2044
+ if (PyObject_Print (NULL , fp , 0 ) < 0 ) {
2045
+ return NULL ;
2046
+ }
2047
+
2048
+ fclose (fp );
2049
+
2050
+ Py_RETURN_NONE ;
2051
+ }
2052
+
2053
+ static PyObject *
2054
+ pyobject_print_noref_object (PyObject * self , PyObject * args )
2055
+ {
2056
+ PyObject * test_string ;
2057
+ PyObject * filename ;
2058
+ FILE * fp ;
2059
+ char correct_string [100 ];
2060
+
2061
+ test_string = PyUnicode_FromString ("Spam spam spam" );
2062
+
2063
+ Py_DECREF (test_string );
2064
+
2065
+ snprintf (correct_string , 100 , "<refcnt %zd at %p>" , Py_REFCNT (test_string ), (void * )test_string );
2066
+
2067
+ if (!PyArg_UnpackTuple (args , "call_pyobject_print" , 1 , 1 , & filename )) {
2068
+ return NULL ;
2069
+ }
2070
+
2071
+ fp = _Py_fopen_obj (filename , "w+" );
2072
+
2073
+ if (PyObject_Print (test_string , fp , 0 ) < 0 ){
2074
+ return NULL ;
2075
+ }
2076
+
2077
+ fclose (fp );
2078
+
2079
+ return PyUnicode_FromString (correct_string );
2080
+ }
2081
+
2082
+ static PyObject *
2083
+ pyobject_print_os_error (PyObject * self , PyObject * args )
2084
+ {
2085
+ PyObject * test_string ;
2086
+ PyObject * filename ;
2087
+ FILE * fp ;
2088
+
2089
+ test_string = PyUnicode_FromString ("Spam spam spam" );
2090
+
2091
+ if (!PyArg_UnpackTuple (args , "call_pyobject_print" , 1 , 1 , & filename )) {
2092
+ return NULL ;
2093
+ }
2094
+
2095
+ // open file in read mode to induce OSError
2096
+ fp = _Py_fopen_obj (filename , "r" );
2097
+
2098
+ if (PyObject_Print (test_string , fp , 0 ) < 0 ) {
2099
+ return NULL ;
2100
+ }
2101
+
2102
+ fclose (fp );
2103
+
2104
+ Py_RETURN_NONE ;
2105
+ }
2106
+
2004
2107
static PyObject *
2005
2108
raise_exception (PyObject * self , PyObject * args )
2006
2109
{
@@ -5969,6 +6072,10 @@ static PyMethodDef TestMethods[] = {
5969
6072
{"pyobject_repr_from_null" , pyobject_repr_from_null , METH_NOARGS },
5970
6073
{"pyobject_str_from_null" , pyobject_str_from_null , METH_NOARGS },
5971
6074
{"pyobject_bytes_from_null" , pyobject_bytes_from_null , METH_NOARGS },
6075
+ {"call_pyobject_print" , call_pyobject_print , METH_VARARGS },
6076
+ {"pyobject_print_null" , pyobject_print_null , METH_VARARGS },
6077
+ {"pyobject_print_noref_object" , pyobject_print_noref_object , METH_VARARGS },
6078
+ {"pyobject_print_os_error" , pyobject_print_os_error , METH_VARARGS },
5972
6079
{"test_with_docstring" , test_with_docstring , METH_NOARGS ,
5973
6080
PyDoc_STR ("This is a pretty normal docstring." )},
5974
6081
{"test_string_to_double" , test_string_to_double , METH_NOARGS },
0 commit comments