Skip to content

Commit 6f40e2f

Browse files
[3.10] gh-94808: Cover %p in PyUnicode_FromFormat (GH-96677) (#98032)
Co-authored-by: Nikita Sobolev <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]> (cherry picked from commit 72c166a)
1 parent 8c81d33 commit 6f40e2f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Lib/test/test_unicode.py

+19
Original file line numberDiff line numberDiff line change
@@ -2759,6 +2759,25 @@ def check_format(expected, format, *args):
27592759
check_format('repr=abc',
27602760
b'repr=%V', 'abc', b'xyz')
27612761

2762+
# test %p
2763+
# We cannot test the exact result,
2764+
# because it returns a hex representation of a C pointer,
2765+
# which is going to be different each time. But, we can test the format.
2766+
p_format_regex = r'^0x[a-zA-Z0-9]{3,}$'
2767+
p_format1 = PyUnicode_FromFormat(b'%p', 'abc')
2768+
self.assertIsInstance(p_format1, str)
2769+
self.assertRegex(p_format1, p_format_regex)
2770+
2771+
p_format2 = PyUnicode_FromFormat(b'%p %p', '123456', b'xyz')
2772+
self.assertIsInstance(p_format2, str)
2773+
self.assertRegex(p_format2,
2774+
r'0x[a-zA-Z0-9]{3,} 0x[a-zA-Z0-9]{3,}')
2775+
2776+
# Extra args are ignored:
2777+
p_format3 = PyUnicode_FromFormat(b'%p', '123456', None, b'xyz')
2778+
self.assertIsInstance(p_format3, str)
2779+
self.assertRegex(p_format3, p_format_regex)
2780+
27622781
# Test string decode from parameter of %s using utf-8.
27632782
# b'\xe4\xba\xba\xe6\xb0\x91' is utf-8 encoded byte sequence of
27642783
# '\u4eba\u6c11'

0 commit comments

Comments
 (0)