Skip to content

Commit 72c166a

Browse files
sobolevnJelleZijlstra
andauthoredOct 7, 2022
gh-94808: Cover %p in PyUnicode_FromFormat (#96677)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent be4099e commit 72c166a

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

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

0 commit comments

Comments
 (0)