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