Skip to content

Commit 46aa5d2

Browse files
[3.11] gh-94808: Cover %p in PyUnicode_FromFormat (GH-96677) (#98033)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> (cherry picked from commit 72c166a)
1 parent c2f21af commit 46aa5d2

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
@@ -2809,6 +2809,25 @@ def check_format(expected, format, *args):
28092809
check_format('repr=abc',
28102810
b'repr=%V', 'abc', b'xyz')
28112811

2812+
# test %p
2813+
# We cannot test the exact result,
2814+
# because it returns a hex representation of a C pointer,
2815+
# which is going to be different each time. But, we can test the format.
2816+
p_format_regex = r'^0x[a-zA-Z0-9]{3,}$'
2817+
p_format1 = PyUnicode_FromFormat(b'%p', 'abc')
2818+
self.assertIsInstance(p_format1, str)
2819+
self.assertRegex(p_format1, p_format_regex)
2820+
2821+
p_format2 = PyUnicode_FromFormat(b'%p %p', '123456', b'xyz')
2822+
self.assertIsInstance(p_format2, str)
2823+
self.assertRegex(p_format2,
2824+
r'0x[a-zA-Z0-9]{3,} 0x[a-zA-Z0-9]{3,}')
2825+
2826+
# Extra args are ignored:
2827+
p_format3 = PyUnicode_FromFormat(b'%p', '123456', None, b'xyz')
2828+
self.assertIsInstance(p_format3, str)
2829+
self.assertRegex(p_format3, p_format_regex)
2830+
28122831
# Test string decode from parameter of %s using utf-8.
28132832
# b'\xe4\xba\xba\xe6\xb0\x91' is utf-8 encoded byte sequence of
28142833
# '\u4eba\u6c11'

0 commit comments

Comments
 (0)