Skip to content

Commit bb3938b

Browse files
committed
Initial commit
1 parent 936d60d commit bb3938b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

Lib/profiling/tracing/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ def main():
197197
# in the module's namespace.
198198
globs = module.__dict__
199199
globs.update({
200-
'__spec__': spec,
200+
# See gh-140729, set None to __spec__ according
201+
# to the documentation,
202+
# https://docs.python.org/3/reference/import.html#module-specs
203+
'__spec__': None,
201204
'__file__': spec.origin,
202205
'__name__': spec.name,
203206
'__package__': None,

Lib/test/test_profiling/test_sampling_profiler/test_advanced.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,50 @@ def worker(x):
234234

235235
self.assertIn("Results: [2, 4, 6]", stdout)
236236
self.assertNotIn("Can't pickle", stderr)
237+
238+
239+
@requires_remote_subprocess_debugging()
240+
class TestProcessRunSupport(unittest.TestCase):
241+
"""
242+
Test that Process works correctly with cProfile.
243+
"""
244+
245+
def test_process_run_pickle(self):
246+
# gh-140729: test use Process in cProfile.
247+
val = 10
248+
test_script = f'''
249+
import multiprocessing
250+
251+
def worker(x):
252+
print(__name__)
253+
exit(x ** 2)
254+
255+
if __name__ == "__main__":
256+
multiprocessing.set_start_method("spawn")
257+
p = multiprocessing.Process(target=worker, args=({val},))
258+
p.start()
259+
p.join()
260+
print("p.exitcode =", p.exitcode)
261+
'''
262+
263+
with os_helper.temp_dir() as temp_dir:
264+
script = script_helper.make_script(
265+
temp_dir, 'test_process_run_pickle', test_script
266+
)
267+
with SuppressCrashReport():
268+
with script_helper.spawn_python(
269+
"-m", "cProfile",
270+
script,
271+
stdout=subprocess.PIPE,
272+
stderr=subprocess.PIPE,
273+
text=True
274+
) as proc:
275+
try:
276+
stdout, stderr = proc.communicate(timeout=SHORT_TIMEOUT)
277+
except subprocess.TimeoutExpired:
278+
proc.kill()
279+
stdout, stderr = proc.communicate()
280+
281+
self.assertIn("__mp_main__", stdout)
282+
self.assertIn(f"exitcode = {val**2}", stdout)
283+
self.assertNotIn("Can't pickle", stderr)

0 commit comments

Comments
 (0)