Skip to content

Commit 52caaef

Browse files
authored
Revert "GH-120754: Add a strace helper and test set of syscalls for o… (#123303)
Revert "GH-120754: Add a strace helper and test set of syscalls for open().read() (#121143)" This reverts commit e38d0af.
1 parent e38d0af commit 52caaef

File tree

4 files changed

+33
-280
lines changed

4 files changed

+33
-280
lines changed

Lib/test/support/strace_helper.py

Lines changed: 0 additions & 166 deletions
This file was deleted.

Lib/test/test_fileio.py

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from test.support import (
1313
cpython_only, swap_attr, gc_collect, is_emscripten, is_wasi,
14-
infinite_recursion, strace_helper
14+
infinite_recursion,
1515
)
1616
from test.support.os_helper import (
1717
TESTFN, TESTFN_ASCII, TESTFN_UNICODE, make_bad_fd,
@@ -24,9 +24,6 @@
2424
import _pyio # Python implementation of io
2525

2626

27-
_strace_flags=["--trace=%file,%desc"]
28-
29-
3027
class AutoFileTests:
3128
# file tests for which a test file is automatically set up
3229

@@ -362,94 +359,6 @@ def testErrnoOnClosedReadinto(self, f):
362359
a = array('b', b'x'*10)
363360
f.readinto(a)
364361

365-
@strace_helper.requires_strace()
366-
def test_syscalls_read(self):
367-
"""Check that the set of system calls produced by the I/O stack is what
368-
is expected for various read cases.
369-
370-
It's expected as bits of the I/O implementation change, this will need
371-
to change. The goal is to catch changes that unintentionally add
372-
additional systemcalls (ex. additional calls have been looked at in
373-
bpo-21679 and gh-120754).
374-
"""
375-
self.f.write(b"Hello, World!")
376-
self.f.close()
377-
378-
379-
def check_readall(name, code, prelude="", cleanup=""):
380-
with self.subTest(name=name):
381-
syscalls = strace_helper.get_syscalls(code, _strace_flags,
382-
prelude=prelude,
383-
cleanup=cleanup)
384-
385-
# There are a number of related syscalls used to implement
386-
# behaviors in a libc (ex. fstat, newfstatat, open, openat).
387-
# Allow any that use the same substring.
388-
def count_similarname(name):
389-
return len([sc for sc in syscalls if name in sc])
390-
391-
# Should open and close the file exactly once
392-
self.assertEqual(count_similarname('open'), 1)
393-
self.assertEqual(count_similarname('close'), 1)
394-
395-
# Should only have one fstat (bpo-21679, gh-120754)
396-
self.assertEqual(count_similarname('fstat'), 1)
397-
398-
399-
# "open, read, close" file using different common patterns.
400-
check_readall(
401-
"open builtin with default options",
402-
f"""
403-
f = open('{TESTFN}')
404-
f.read()
405-
f.close()
406-
"""
407-
)
408-
409-
check_readall(
410-
"open in binary mode",
411-
f"""
412-
f = open('{TESTFN}', 'rb')
413-
f.read()
414-
f.close()
415-
"""
416-
)
417-
418-
check_readall(
419-
"open in text mode",
420-
f"""
421-
f = open('{TESTFN}', 'rt')
422-
f.read()
423-
f.close()
424-
"""
425-
)
426-
427-
check_readall(
428-
"pathlib read_bytes",
429-
"p.read_bytes()",
430-
prelude=f"""from pathlib import Path; p = Path("{TESTFN}")"""
431-
432-
)
433-
434-
check_readall(
435-
"pathlib read_text",
436-
"p.read_text()",
437-
prelude=f"""from pathlib import Path; p = Path("{TESTFN}")"""
438-
)
439-
440-
# Focus on just `read()`.
441-
calls = strace_helper.get_syscalls(
442-
prelude=f"f = open('{TESTFN}')",
443-
code="f.read()",
444-
cleanup="f.close()",
445-
strace_flags=_strace_flags
446-
)
447-
# One to read all the bytes
448-
# One to read the EOF and get a size 0 return.
449-
self.assertEqual(calls.count("read"), 2)
450-
451-
452-
453362
class CAutoFileTests(AutoFileTests, unittest.TestCase):
454363
FileIO = _io.FileIO
455364
modulename = '_io'

Lib/test/test_subprocess.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from test.support import check_sanitizer
55
from test.support import import_helper
66
from test.support import os_helper
7-
from test.support import strace_helper
87
from test.support import warnings_helper
98
from test.support.script_helper import assert_python_ok
109
import subprocess
@@ -3416,33 +3415,44 @@ def __del__(self):
34163415

34173416
@unittest.skipIf(not sysconfig.get_config_var("HAVE_VFORK"),
34183417
"vfork() not enabled by configure.")
3419-
@strace_helper.requires_strace()
3418+
@unittest.skipIf(sys.platform != "linux", "Linux only, requires strace.")
34203419
@mock.patch("subprocess._USE_POSIX_SPAWN", new=False)
34213420
def test_vfork_used_when_expected(self):
34223421
# This is a performance regression test to ensure we default to using
34233422
# vfork() when possible.
34243423
# Technically this test could pass when posix_spawn is used as well
34253424
# because libc tends to implement that internally using vfork. But
34263425
# that'd just be testing a libc+kernel implementation detail.
3427-
3428-
# Are intersted in the system calls:
3429-
# clone,clone2,clone3,fork,vfork,exit,exit_group
3430-
# Unfortunately using `--trace` with that list to strace fails because
3431-
# not all are supported on all platforms (ex. clone2 is ia64 only...)
3432-
# So instead use `%process` which is recommended by strace, and contains
3433-
# the above.
3426+
strace_binary = "/usr/bin/strace"
3427+
# The only system calls we are interested in.
3428+
strace_filter = "--trace=clone,clone2,clone3,fork,vfork,exit,exit_group"
34343429
true_binary = "/bin/true"
3435-
strace_args = ["--trace=%process"]
3430+
strace_command = [strace_binary, strace_filter]
3431+
3432+
try:
3433+
does_strace_work_process = subprocess.run(
3434+
strace_command + [true_binary],
3435+
stderr=subprocess.PIPE,
3436+
stdout=subprocess.DEVNULL,
3437+
)
3438+
rc = does_strace_work_process.returncode
3439+
stderr = does_strace_work_process.stderr
3440+
except OSError:
3441+
rc = -1
3442+
stderr = ""
3443+
if rc or (b"+++ exited with 0 +++" not in stderr):
3444+
self.skipTest("strace not found or not working as expected.")
34363445

34373446
with self.subTest(name="default_is_vfork"):
3438-
vfork_result = strace_helper.strace_python(
3439-
f"""\
3440-
import subprocess
3441-
subprocess.check_call([{true_binary!r}])""",
3442-
strace_args
3447+
vfork_result = assert_python_ok(
3448+
"-c",
3449+
textwrap.dedent(f"""\
3450+
import subprocess
3451+
subprocess.check_call([{true_binary!r}])"""),
3452+
__run_using_command=strace_command,
34433453
)
34443454
# Match both vfork() and clone(..., flags=...|CLONE_VFORK|...)
3445-
self.assertRegex(vfork_result.event_bytes, br"(?i)vfork")
3455+
self.assertRegex(vfork_result.err, br"(?i)vfork")
34463456
# Do NOT check that fork() or other clones did not happen.
34473457
# If the OS denys the vfork it'll fallback to plain fork().
34483458

@@ -3455,20 +3465,21 @@ def test_vfork_used_when_expected(self):
34553465
("setgroups", "", "extra_groups=[]", True),
34563466
):
34573467
with self.subTest(name=sub_name):
3458-
non_vfork_result = strace_helper.strace_python(
3459-
f"""\
3468+
non_vfork_result = assert_python_ok(
3469+
"-c",
3470+
textwrap.dedent(f"""\
34603471
import subprocess
34613472
{preamble}
34623473
try:
34633474
subprocess.check_call(
34643475
[{true_binary!r}], **dict({sp_kwarg}))
34653476
except PermissionError:
34663477
if not {expect_permission_error}:
3467-
raise""",
3468-
strace_args
3478+
raise"""),
3479+
__run_using_command=strace_command,
34693480
)
34703481
# Ensure neither vfork() or clone(..., flags=...|CLONE_VFORK|...).
3471-
self.assertNotRegex(non_vfork_result.event_bytes, br"(?i)vfork")
3482+
self.assertNotRegex(non_vfork_result.err, br"(?i)vfork")
34723483

34733484

34743485
@unittest.skipUnless(mswindows, "Windows specific tests")

Misc/ACKS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,6 @@ Grzegorz Makarewicz
11641164
David Malcolm
11651165
Greg Malcolm
11661166
William Mallard
1167-
Cody Maloney
11681167
Ken Manheimer
11691168
Vladimir Marangozov
11701169
Colin Marc

0 commit comments

Comments
 (0)