Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Nitpicks
- Use test helpers
- Use PyOS_snprintf
- Touch up formatting (PEP 7)
  • Loading branch information
encukou committed Mar 25, 2024
commit d8e2e0ee53b5950e683bf74ea687db727cb653ac
33 changes: 10 additions & 23 deletions Lib/test/test_capi/test_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import enum
import unittest
from test.support import import_helper
from test.support import os_helper

_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
_testcapi = import_helper.import_module('_testcapi')
Expand Down Expand Up @@ -53,9 +54,6 @@ def test_get_constant_borrowed(self):

class PrintTest(unittest.TestCase):
def testPyObjectPrintObject(self):
import os
import test.support.os_helper as os_helper
from test.support import import_helper

class PrintableObject:

Expand All @@ -67,54 +65,43 @@ def __str__(self):

obj = PrintableObject()
output_filename = os_helper.TESTFN
self.addCleanup(os_helper.unlink, output_filename)

# Test repr printing
_testcapi.call_pyobject_print(obj, output_filename, False)
with open(output_filename, 'r') as output_file:
self.assertEqual(output_file.read(), repr(obj))

# Test str printing
_testcapi.call_pyobject_print(obj, output_filename, True)
with open(output_filename, 'r') as output_file:
self.assertEqual(output_file.read(), str(obj))

os.remove(output_filename)

def testPyObjectPrintNULL(self):
import os
import test.support.os_helper as os_helper
from test.support import import_helper

output_filename = os_helper.TESTFN
self.addCleanup(os_helper.unlink, output_filename)

# Test repr printing
_testcapi.pyobject_print_null(output_filename)
with open(output_filename, 'r') as output_file:
self.assertEqual(output_file.read(), '<nil>')

os.remove(output_filename)

def testPyObjectPrintNoRefObject(self):
import os
import test.support.os_helper as os_helper
from test.support import import_helper

output_filename = os_helper.TESTFN
self.addCleanup(os_helper.unlink, output_filename)

# Test repr printing
correct_output = _testcapi.pyobject_print_noref_object(output_filename)
with open(output_filename, 'r') as output_file:
self.assertEqual(output_file.read(), correct_output)

os.remove(output_filename)

def testPyObjectPrintOSError(self):
import os
import test.support.os_helper as os_helper
from test.support import import_helper

output_filename = os_helper.TESTFN
self.addCleanup(os_helper.unlink, output_filename)

open(output_filename, "w+").close()
with self.assertRaises(OSError):
_testcapi.pyobject_print_os_error(output_filename)

os.remove(output_filename)

if __name__ == "__main__":
unittest.main()
10 changes: 6 additions & 4 deletions Modules/_testcapi/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ call_pyobject_print(PyObject *self, PyObject * args)
FILE *fp;
int flags = 0;

if (!PyArg_UnpackTuple(args, "call_pyobject_print", 3, 3, &object, &filename, &print_raw)) {
if (!PyArg_UnpackTuple(args, "call_pyobject_print", 3, 3,
&object, &filename, &print_raw)) {
return NULL;
}

Expand Down Expand Up @@ -62,9 +63,10 @@ pyobject_print_noref_object(PyObject *self, PyObject *args)

test_string = PyUnicode_FromString("Spam spam spam");

test_string -> ob_refcnt = 0;
test_string->ob_refcnt = 0;

snprintf(correct_string, 100, "<refcnt %zd at %p>", Py_REFCNT(test_string), (void *)test_string);
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;
Expand All @@ -79,7 +81,7 @@ pyobject_print_noref_object(PyObject *self, PyObject *args)

fclose(fp);

test_string -> ob_refcnt = 1;
test_string->ob_refcnt = 1;
Py_DECREF(test_string);

return PyUnicode_FromString(correct_string);
Expand Down