8000 bpo-34284: Proper tests for tuples of `sys` · python/cpython@d018573 · GitHub
[go: up one dir, main page]

Skip to content

Commit d018573

Browse files
committed
bpo-34284: Proper tests for tuples of sys
1 parent a64abba commit d018573

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

Lib/test/test_sys.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import sysconfig
1212
import locale
1313
import threading
14+
import re
1415

1516
# count the number of test runs, used to create unique
1617
# strings to intern in test_intern()
@@ -541,10 +542,26 @@ def assert_raise_on_new_sys_type(self, sys_attr):
541542
# Users are intentionally prevented from creating new instances of
542543
# sys.flags, sys.version_info, and sys.getwindowsversion.
543544
attr_type = type(sys_attr)
544-
with self.assertRaises(TypeError):
545+
attr_type_name = f'sys.{attr_type.__name__}'
546+
547+
cant_create_message = re.escape(f"cannot create '{attr_type_name}' instances")
548+
object_message = re.escape(
549+
f'object.__new__({attr_type_name}) is not safe, '
550+
f'use {attr_type_name}.__new__()'
551+
)
552+
tuple_message = re.escape(
553+
f'tuple.__new__({attr_type_name}) is not safe, '
554+
f'use {attr_type_name}.__new__()'
555+
)
556+
557+
with self.assertRaisesRegex(TypeError, cant_create_message):
545558
attr_type()
546-
with self.assertRaises(TypeError):
559+
with self.assertRaisesRegex(TypeError, cant_create_message):
547560
attr_type.__new__(attr_type)
561+
with self.assertRaisesRegex(TypeError, object_message):
562+
object.__new__(attr_type)
563+
with self.assertRaisesRegex(TypeError, tuple_message):
564+
tuple.__new__(attr_type)
548565

549566
def test_sys_flags_no_instantiation(self):
550567
self.assert_raise_on_new_sys_type(sys.flags)

Lib/test/test_types.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,28 +1688,6 @@ def test_create_uncreatable(self):
16881688
with self.assertRaisesRegex(TypeError, not_safe_message):
16891689
object.__new__(C)
16901690

1691-
def test_create_sys_tuples(self):
1692-
C = type(sys.flags)
1693-
1694-
cant_create_message = re.escape("cannot create 'sys.flags' instances")
1695-
object_message = re.escape(
1696-
'object.__new__(sys.flags) is not safe, '
1697-
'use sys.flags.__new__()'
1698-
)
1699-
tuple_message = re.escape(
1700-
'tuple.__new__(sys.flags) is not safe, '
1701-
'use sys.flags.__new__()'
1702-
)
1703-
1704-
with self.assertRaisesRegex(TypeError, cant_create_message):
1705-
C()
1706-
with self.assertRaisesRegex(TypeError, cant_create_message):
1707-
C.__new__(C)
1708-
with self.assertRaisesRegex(TypeError, object_message):
1709-
object.__new__(C)
1710-
with self.assertRaisesRegex(TypeError, tuple_message):
1711-
tuple.__new__(C)
1712-
17131691

17141692
if __name__ == '__main__':
17151693
unittest.main()

0 commit comments

Comments
 (0)
0