|
| 1 | +#include "parts.h" |
| 2 | +#include "util.h" |
| 3 | + |
| 4 | +static PyObject * |
| 5 | +call_pyobject_print(PyObject *self, PyObject * args) |
| 6 | +{ |
| 7 | + PyObject *object; |
| 8 | + PyObject *filename; |
| 9 | + PyObject *print_raw; |
| 10 | + FILE *fp; |
| 11 | + int flags = 0; |
| 12 | + |
| 13 | + if (!PyArg_UnpackTuple(args, "call_pyobject_print", 3, 3, |
| 14 | + &object, &filename, &print_raw)) { |
| 15 | + return NULL; |
| 16 | + } |
| 17 | + |
| 18 | + fp = _Py_fopen_obj(filename, "w+"); |
| 19 | + |
| 20 | + if (Py_IsTrue(print_raw)) { |
| 21 | + flags = Py_PRINT_RAW; |
| 22 | + } |
| 23 | + |
| 24 | + if (PyObject_Print(object, fp, flags) < 0) { |
| 25 | + fclose(fp); |
| 26 | + return NULL; |
| 27 | + } |
| 28 | + |
| 29 | + fclose(fp); |
| 30 | + |
| 31 | + Py_RETURN_NONE; |
| 32 | +} |
| 33 | + |
| 34 | +static PyObject * |
| 35 | +pyobject_print_null(PyObject *self, PyObject *args) |
| 36 | +{ |
| 37 | + PyObject *filename; |
| 38 | + FILE *fp; |
| 39 | + |
| 40 | + if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) { |
| 41 | + return NULL; |
| 42 | + } |
| 43 | + |
| 44 | + fp = _Py_fopen_obj(filename, "w+"); |
| 45 | + |
| 46 | + if (PyObject_Print(NULL, fp, 0) < 0) { |
| 47 | + fclose(fp); |
| 48 | + return NULL; |
| 49 | + } |
| 50 | + |
| 51 | + fclose(fp); |
| 52 | + |
| 53 | + Py_RETURN_NONE; |
| 54 | +} |
| 55 | + |
| 56 | +static PyObject * |
| 57 | +pyobject_print_noref_object(PyObject *self, PyObject *args) |
| 58 | +{ |
| 59 | + PyObject *test_string; |
| 60 | + PyObject *filename; |
| 61 | + FILE *fp; |
| 62 | + char correct_string[100]; |
| 63 | + |
| 64 | + test_string = PyUnicode_FromString("Spam spam spam"); |
| 65 | + |
| 66 | + Py_SET_REFCNT(test_string, 0); |
| 67 | + |
| 68 | + PyOS_snprintf(correct_string, 100, "<refcnt %zd at %p>", |
| 69 | + Py_REFCNT(test_string), (void *)test_string); |
| 70 | + |
| 71 | + if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) { |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + |
| 75 | + fp = _Py_fopen_obj(filename, "w+"); |
| 76 | + |
| 77 | + if (PyObject_Print(test_string, fp, 0) < 0){ |
| 78 | + fclose(fp); |
| 79 | + return NULL; |
| 80 | + } |
| 81 | + |
| 82 | + fclose(fp); |
| 83 | + |
| 84 | + Py_SET_REFCNT(test_string, 1); |
| 85 | + Py_DECREF(test_string); |
| 86 | + |
| 87 | + return PyUnicode_FromString(correct_string); |
| 88 | +} |
| 89 | + |
| 90 | +static PyObject * |
| 91 | +pyobject_print_os_error(PyObject *self, PyObject *args) |
| 92 | +{ |
| 93 | + PyObject *test_string; |
| 94 | + PyObject *filename; |
| 95 | + FILE *fp; |
| 96 | + |
| 97 | + test_string = PyUnicode_FromString("Spam spam spam"); |
| 98 | + |
| 99 | + if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) { |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + |
| 103 | + // open file in read mode to induce OSError |
| 104 | + fp = _Py_fopen_obj(filename, "r"); |
| 105 | + |
| 106 | + if (PyObject_Print(test_string, fp, 0) < 0) { |
| 107 | + fclose(fp); |
| 108 | + return NULL; |
| 109 | + } |
| 110 | + |
| 111 | + fclose(fp); |
| 112 | + |
| 113 | + Py_RETURN_NONE; |
| 114 | +} |
| 115 | + |
| 116 | +static PyMethodDef test_methods[] = { |
| 117 | + {"call_pyobject_print", call_pyobject_print, METH_VARARGS}, |
| 118 | + {"pyobject_print_null", pyobject_print_null, METH_VARARGS}, |
| 119 | + {"pyobject_print_noref_object", pyobject_print_noref_object, METH_VARARGS}, |
| 120 | + {"pyobject_print_os_error", pyobject_print_os_error, METH_VARARGS}, |
| 121 | + |
| 122 | + {NULL}, |
| 123 | +}; |
| 124 | + |
| 125 | +int |
| 126 | +_PyTestCapi_Init_Object(PyObject *m) |
| 127 | +{ |
| 128 | + if (PyModule_AddFunctions(m, test_methods) < 0) { |
| 129 | + return -1; |
| 130 | + } |
| 131 | + |
| 132 | + return 0; |
| 133 | +} |
0 commit comments