[3.14] gh-139748: fix leaks in AC error paths when using unicode FS-b… (#139789)

* [3.14] gh-139748: fix leaks in AC error paths when using unicode FS-based converters (GH-139765)
(cherry picked from commit b04a57deef)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Kumar Aditya 2025-10-08 22:16:21 +05:30 committed by GitHub
parent 077652b44f
commit 90cd009209
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 88 additions and 46 deletions

View file

@ -651,6 +651,21 @@ def test_compile_filename(self):
compile('pass', filename, 'exec')
self.assertRaises(TypeError, compile, 'pass', list(b'file.py'), 'exec')
def test_compile_filename_refleak(self):
# Regression tests for reference leak in PyUnicode_FSDecoder.
# See https://github.com/python/cpython/issues/139748.
mortal_str = 'this is a mortal string'
# check error path when 'mode' AC conversion failed
self.assertRaises(TypeError, compile, b'', mortal_str, mode=1234)
# check error path when 'optimize' AC conversion failed
self.assertRaises(OverflowError, compile, b'', mortal_str,
'exec', optimize=1 << 1000)
# check error path when 'dont_inherit' AC conversion failed
class EvilBool:
def __bool__(self): raise ValueError
self.assertRaises(ValueError, compile, b'', mortal_str,
'exec', dont_inherit=EvilBool())
@support.cpython_only
def test_same_filename_used(self):
s = """def f(): pass\ndef g(): pass"""