Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-94808:Improve coverage of PyObject_Print #98749

Merged
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 @@
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 @@

test_string = PyUnicode_FromString("Spam spam spam");

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

Check failure on line 66 in Modules/_testcapi/object.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'ob_refcnt': is not a member of '_object' [D:\a\cpython\cpython\PCbuild\_testcapi.vcxproj]

Check failure on line 66 in Modules/_testcapi/object.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'ob_refcnt': is not a member of '_object' [D:\a\cpython\cpython\PCbuild\_testcapi.vcxproj]

Check failure on line 66 in Modules/_testcapi/object.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test

‘PyObject’ {aka ‘struct _object’} has no member named ‘ob_refcnt’

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 @@

fclose(fp);

test_string -> ob_refcnt = 1;
test_string->ob_refcnt = 1;

Check failure on line 84 in Modules/_testcapi/object.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'ob_refcnt': is not a member of '_object' [D:\a\cpython\cpython\PCbuild\_testcapi.vcxproj]

Check failure on line 84 in Modules/_testcapi/object.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'ob_refcnt': is not a member of '_object' [D:\a\cpython\cpython\PCbuild\_testcapi.vcxproj]

Check failure on line 84 in Modules/_testcapi/object.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test

‘PyObject’ {aka ‘struct _object’} has no member named ‘ob_refcnt’
Py_DECREF(test_string);

return PyUnicode_FromString(correct_string);
Expand Down
Loading