8000 gh-130163: Fix usage of borrow references from _PySys_GetAttr by sergey-miryanov · Pull Request #130282 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-130163: Fix usage of borrow references from _PySys_GetAttr #130282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Prev Previous commit
Next Next commit
Simplify tests for print and add _check to unify tests
  • Loading branch information
sergey-miryanov committed Feb 23, 2025
commit 413940935f48019fb08c6aac51d43f31262be9f1
194 changes: 75 additions & 119 deletions Lib/test/test_sys_getattr.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@
class PySysGetAttrTest(unittest.TestCase):

common_faulthandler_code = textwrap.dedent('''
from contextlib import redirect_stderr
import sys
from faulthandler import dump_traceback, enable, dump_traceback_later
from threading import Thread
import time

class FakeFile:
def __init__(self):
self.f = open("{0}", "w")
def write(self, s):
self.f.write(s)
class FakeIO:
def __init__(self, what):
self.what = what
def write(self, str):
pass
def flush(self):
time.sleep(0.2)
pass
def fileno(self):
time.sleep(0.2)
return self.f.fileno()
self.restore_std('stderr')
return 0

@staticmethod
def restore_std(what):
stdfile = getattr(sys, what)
setattr(sys, what, getattr(sys, "__" + what + "__"))
del stdfile

def thread1():
text = FakeFile()
with redirect_stderr(text):
time.sleep(0.2)
@staticmethod
def set_std(what):
setattr(sys, what, FakeIO(what))

def main():
enable(None, True)
t1 = Thread(target=thread1, args=())
t1.start()
time.sleep(0.1)
{1}
FakeIO.set_std('stderr')
{0}

if __name__ == "__main__":
main()
exit(0)
''')

common_warnings_code = textwrap.dedent('''
Expand Down Expand Up @@ -210,150 +210,106 @@ def main():
main()
''')

print_code = textwrap.dedent('''
from io import StringIO
import sys

def test_print_deleted_stdout(self):
# print should use strong reference to the stdout.
code = textwrap.dedent('''
from io import StringIO
import sys

class Bar:
def __init__(self):
self.x = sys.stdout
setattr(sys, "stdout", StringIO())

def __repr__(self):
x = sys.stdout
setattr(sys, "stdout", self.x)
del x
return "Bar"

def main():
print(Bar())

if __name__ == "__main__":
main()
exit(0)
''')
class Bar:
def __init__(self):
self.x = sys.stdout
setattr(sys, "stdout", StringIO())

def __repr__(self):
x = sys.stdout
setattr(sys, "stdout", self.x)
del x
return "Bar"

def main():
print(Bar())

if __name__ == "__main__":
main()
exit(0)
''')

def _check(self, code):
rc, out, err = assert_python_ok('-c', code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)

def test_print_deleted_stdout(self):
# print should use strong reference to the stdout.
self._check(self.print_code)

def test_faulthandler_enable_deleted_stderr(self):
# faulthandler should use strong reference to the stderr
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir, "test_faulthandler_enable")
test_code = self.common_faulthandler_code.format(
path.as_posix(),
"enable(None, True)"
)
rc, out, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
code = self.common_faulthandler_code.format(
"enable(None, True)"
)
self._check(code)

def test_faulthandler_dump_traceback_deleted_stderr(self):
# faulthandler should use strong reference to the stderr
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir, "test_faulthandler_dump_traceback")
test_code = self.common_faulthandler_code.format(
path.as_posix(),
"dump_traceback(None, False)"
)
rc, out, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
code = self.common_faulthandler_code.format(
"dump_traceback(None, False)"
)
self._check(code)

def test_faulthandler_dump_traceback_later_deleted_stderr(self):
# faulthandler should use strong reference to the stderr
with tempfile.TemporaryDirectory() as tmpdir:
path = Path(tmpdir, "test_faulthandler_dump_traceback_later")
test_code = self.common_faulthandler_code.format(
path.as_posix(),
"dump_traceback_later(0.1, True, None, False)"
)
rc, out, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
code = self.common_faulthandler_code.format(
"dump_traceback_later(0.1, True, None, False)"
)
self._check(code)

def test_warnings_warn(self):
test_code = self.common_warnings_code.format(
code = self.common_warnings_code.format(
"warnings.warn(Foo())"
)
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
self._check(code)

def test_warnings_warn_explicit(self):
test_code = self.common_warnings_code.format(
code = self.common_warnings_code.format(
"warnings.warn_explicit(Foo(), UserWarning, 'filename', 0)"
)
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
self._check(code)

def test_input_stdin(self):
test_code = self.common_input_code.format(
code = self.common_input_code.format(
"",
"CrashStdin()"
)
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
self._check(code)

def test_input_stdout(self):
test_code = self.common_input_code.format(
code = self.common_input_code.format(
"",
"CrashStdout()"
)
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
self._check(code)

def test_input_stderr(self):
test_code = self.common_input_code.format(
code = self.common_input_code.format(
"sys.addaudithook(audit)",
"CrashStderr()"
)
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
self._check(code)

def test_errors_unraisablehook(self):
test_code = self.unraisable_hook_code
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
self._check(self.unraisable_hook_code)

def test_py_finalize_flush_std_files_stdout(self):
test_code = self.flush_std_files_common_code.format("stdout")
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
code = self.flush_std_files_common_code.format("stdout")
self._check(code)

def test_py_finalize_flush_std_files_stderr(self):
test_code = self.flush_std_files_common_code.format("stderr")
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
code = self.flush_std_files_common_code.format("stderr")
self._check(code)

def test_pyerr_printex_excepthook(self):
test_code = self.pyerr_printex_code
rc, _, err = assert_python_ok('-c', test_code)
self.assertEqual(rc, 0)
self.assertNotIn(b"Segmentation fault", err)
self.assertNotIn(b"access violation", err)
self._check(self.pyerr_printex_code)

if __name__ == "__main__":
unittest.main()
Loading
0