Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions Modules/_testcapi/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ call_pyobject_print(PyObject *self, PyObject * args)
}

fp = Py_fopen(filename, "w+");
if (fp == NULL) {
return NULL;
}

if (Py_IsTrue(print_raw)) {
flags = Py_PRINT_RAW;
Expand All @@ -32,17 +35,15 @@ call_pyobject_print(PyObject *self, PyObject * args)
}

static PyObject *
pyobject_print_null(PyObject *self, PyObject *args)
pyobject_print_null(PyObject *self, PyObject *filename)
{
PyObject *filename;
FILE *fp;

if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) {
fp = Py_fopen(filename, "w+");
if (fp == NULL) {
return NULL;
}

fp = Py_fopen(filename, "w+");

if (PyObject_Print(NULL, fp, 0) < 0) {
fclose(fp);
return NULL;
Expand All @@ -54,26 +55,29 @@ pyobject_print_null(PyObject *self, PyObject *args)
}

static PyObject *
pyobject_print_noref_object(PyObject *self, PyObject *args)
pyobject_print_noref_object(PyObject *self, PyObject *filename)
{
PyObject *test_string;
PyObject *filename;
FILE *fp;
char correct_string[100];

test_string = PyUnicode_FromString("Spam spam spam");
if (test_string == NULL) {
return NULL;
}

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)) {
fp = Py_fopen(filename, "w+");
if (fp == NULL) {
Py_SET_REFCNT(test_string, 1);
Py_DECREF(test_string);
return NULL;
}

fp = Py_fopen(filename, "w+");

if (PyObject_Print(test_string, fp, 0) < 0){
fclose(fp);
Py_SET_REFCNT(test_string, 1);
Expand All @@ -90,20 +94,22 @@ pyobject_print_noref_object(PyObject *self, PyObject *args)
}

static PyObject *
pyobject_print_os_error(PyObject *self, PyObject *args)
pyobject_print_os_error(PyObject *self, PyObject *filename)
{
PyObject *test_string;
PyObject *filename;
FILE *fp;

test_string = PyUnicode_FromString("Spam spam spam");

if (!PyArg_UnpackTuple(args, "call_pyobject_print", 1, 1, &filename)) {
if (test_string == NULL) {
return NULL;
}

// open file in read mode to induce OSError
fp = Py_fopen(filename, "r");
if (fp == NULL) {
Py_DECREF(test_string);
return NULL;
}

if (PyObject_Print(test_string, fp, 0) < 0) {
fclose(fp);
Expand Down Expand Up @@ -582,9 +588,9 @@ pysentinel_checkexact(PyObject *self, PyObject *obj)

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_print_null", pyobject_print_null, METH_O},
{"pyobject_print_noref_object", pyobject_print_noref_object, METH_O},
{"pyobject_print_os_error", pyobject_print_os_error, METH_O},
{"pyobject_clear_weakrefs_no_callbacks", pyobject_clear_weakrefs_no_callbacks, METH_O},
{"pyobject_enable_deferred_refcount", pyobject_enable_deferred_refcount, METH_O},
{"pyobject_is_unique_temporary", pyobject_is_unique_temporary, METH_O},
Expand Down
6 changes: 0 additions & 6 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,6 @@ pymarshal_write_long_to_file(PyObject* self, PyObject *args)

fp = Py_fopen(filename, "wb");
if (fp == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -1456,7 +1455,6 @@ pymarshal_write_object_to_file(PyObject* self, PyObject *args)

fp = Py_fopen(filename, "wb");
if (fp == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -1480,7 +1478,6 @@ pymarshal_read_short_from_file(PyObject* self, PyObject *args)

fp = Py_fopen(filename, "rb");
if (fp == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -1505,7 +1502,6 @@ pymarshal_read_long_from_file(PyObject* self, PyObject *args)

fp = Py_fopen(filename, "rb");
if (fp == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -1527,7 +1523,6 @@ pymarshal_read_last_object_from_file(PyObject* self, PyObject *args)

FILE *fp = Py_fopen(filename, "rb");
if (fp == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand All @@ -1550,7 +1545,6 @@ pymarshal_read_object_from_file(PyObject* self, PyObject *args)

FILE *fp = Py_fopen(filename, "rb");
if (fp == NULL) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}

Expand Down
Loading