bpo-43916: PyStdPrinter_Type uses Py_TPFLAGS_DISALLOW_INSTANTIATION (… · python/cpython@4908fae (original) (raw)

`@@ -1473,11 +1473,67 @@ def test_audit_run_stdin(self):

`

1473

1473

`timeout=support.SHORT_TIMEOUT,

`

1474

1474

`returncode=1)

`

1475

1475

``

``

1476

+

1476

1477

`class MiscTests(EmbeddingTestsMixin, unittest.TestCase):

`

1477

1478

`def test_unicode_id_init(self):

`

1478

1479

`# bpo-42882: Test that _PyUnicode_FromId() works

`

1479

1480

`# when Python is initialized multiples times.

`

1480

1481

`self.run_embedded_interpreter("test_unicode_id_init")

`

1481

1482

``

``

1483

+

``

1484

`+

class StdPrinterTests(EmbeddingTestsMixin, unittest.TestCase):

`

``

1485

`+

Test PyStdPrinter_Type which is used by _PySys_SetPreliminaryStderr():

`

``

1486

`+

"Set up a preliminary stderr printer until we have enough

`

``

1487

`+

infrastructure for the io module in place."

`

``

1488

+

``

1489

`+

def get_stdout_fd(self):

`

``

1490

`+

return sys.stdout.fileno()

`

``

1491

+

``

1492

`+

def create_printer(self, fd):

`

``

1493

`+

ctypes = import_helper.import_module('ctypes')

`

``

1494

`+

PyFile_NewStdPrinter = ctypes.pythonapi.PyFile_NewStdPrinter

`

``

1495

`+

PyFile_NewStdPrinter.argtypes = (ctypes.c_int,)

`

``

1496

`+

PyFile_NewStdPrinter.restype = ctypes.py_object

`

``

1497

`+

return PyFile_NewStdPrinter(fd)

`

``

1498

+

``

1499

`+

def test_write(self):

`

``

1500

`+

message = "unicode:\xe9-\u20ac-\udc80!\n"

`

``

1501

+

``

1502

`+

stdout_fd = self.get_stdout_fd()

`

``

1503

`+

stdout_fd_copy = os.dup(stdout_fd)

`

``

1504

`+

self.addCleanup(os.close, stdout_fd_copy)

`

``

1505

+

``

1506

`+

rfd, wfd = os.pipe()

`

``

1507

`+

self.addCleanup(os.close, rfd)

`

``

1508

`+

self.addCleanup(os.close, wfd)

`

``

1509

`+

try:

`

``

1510

`+

PyFile_NewStdPrinter() only accepts fileno(stdout)

`

``

1511

`+

or fileno(stderr) file descriptor.

`

``

1512

`+

os.dup2(wfd, stdout_fd)

`

``

1513

+

``

1514

`+

printer = self.create_printer(stdout_fd)

`

``

1515

`+

printer.write(message)

`

``

1516

`+

finally:

`

``

1517

`+

os.dup2(stdout_fd_copy, stdout_fd)

`

``

1518

+

``

1519

`+

data = os.read(rfd, 100)

`

``

1520

`+

self.assertEqual(data, message.encode('utf8', 'backslashreplace'))

`

``

1521

+

``

1522

`+

def test_methods(self):

`

``

1523

`+

fd = self.get_stdout_fd()

`

``

1524

`+

printer = self.create_printer(fd)

`

``

1525

`+

self.assertEqual(printer.fileno(), fd)

`

``

1526

`+

self.assertEqual(printer.isatty(), os.isatty(fd))

`

``

1527

`+

printer.flush() # noop

`

``

1528

`+

printer.close() # noop

`

``

1529

+

``

1530

`+

def test_disallow_instantiation(self):

`

``

1531

`+

fd = self.get_stdout_fd()

`

``

1532

`+

printer = self.create_printer(fd)

`

``

1533

`+

PyStdPrinter_Type = type(printer)

`

``

1534

`+

with self.assertRaises(TypeError):

`

``

1535

`+

PyStdPrinter_Type(fd)

`

``

1536

+

``

1537

+

1482

1538

`if name == "main":

`

1483

1539

`unittest.main()

`