Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/profiling/tracing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ def main():
# in the module's namespace.
globs = module.__dict__
globs.update({
'__spec__': spec,
# Set __spec__ to None so the profiled program behaves like a
# script run directly (gh-140729).
'__spec__': None,
'__file__': spec.origin,
'__name__': spec.name,
'__package__': None,
Expand Down
40 changes: 37 additions & 3 deletions Lib/test/test_profiling/test_tracing_profiler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Test suite for the cProfile module."""

import sys
import unittest

Expand Down Expand Up @@ -118,8 +117,9 @@ def test_second_profiler(self):
pr = self.profilerclass()
pr2 = self.profilerclass()
pr.enable()
self.assertRaises(ValueError, pr2.enable)
pr.disable()
self.addCleanup(pr.disable)
msg = f"tool {sys.monitoring.PROFILER_ID} is already in use"
self.assertRaisesRegex(ValueError, msg, pr2.enable)

def test_throw(self):
"""
Expand Down Expand Up @@ -192,6 +192,40 @@ class Foo:
f.close()
assert_python_ok('-m', "cProfile", f.name)

def _test_process_run_pickle(self, start_method):
val = 10
with tempfile.NamedTemporaryFile("w+", delete_on_close=False) as f:
f.write(textwrap.dedent(
f'''\
import multiprocessing

def worker(x):
print(__name__)
exit(x ** 2)

if __name__ == "__main__":
multiprocessing.set_start_method('{start_method}')
p = multiprocessing.Process(target=worker, args=({val},))
p.start()
p.join()
print("p.exitcode =", p.exitcode)
'''))
f.close()
_, out, err = assert_python_ok('-m', "cProfile", f.name)
self.assertIn(b"__mp_main__", out)
self.assertIn(bytes(f"exitcode = {val**2}", encoding='utf8'), out)
self.assertNotIn(b"Can't pickle", err)

def test_process_spawn_pickle(self):
# gh-140729: test use Process in cProfile.
self._test_process_run_pickle('spawn')

@unittest.skipIf(sys.platform == 'win32',
"No 'forkserver' start method on Windows")
def test_process_forkserver_pickle(self):
# gh-140729: test use Process in cProfile.
self._test_process_run_pickle('forkserver')


def main():
if '-r' not in sys.argv:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a pickling error in the ``cProfile`` module when profiling a script that
uses :class:`multiprocessing.Process` with the ``spawn`` and ``forkserver``
start methods.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`!cProfile.Profile.enable` to no longer overwrite errors from
:mod:`sys.monitoring`.
1 change: 0 additions & 1 deletion Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,6 @@ _lsprof_Profiler_enable_impl(ProfilerObject *self, int subcalls,
"use_tool_id", "is",
self->tool_id, "cProfile");
if (check == NULL) {
PyErr_Format(PyExc_ValueError, "Another profiling tool is already active");
goto error;
}
Py_DECREF(check);
Expand Down
2 changes: 1 addition & 1 deletion Platforms/emscripten/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Any data that can vary between Python versions is to be kept in this file.
# This allows for blanket copying of the Emscripten build code between supported
# Python versions.
emscripten-version = "6.0.0"
emscripten-version = "6.0.2"
node-version = "24"
test-args = [
"-m", "test",
Expand Down
20 changes: 0 additions & 20 deletions Python/emscripten_syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,26 +293,6 @@ int __syscall_poll(intptr_t fds, int nfds, int timeout) {
return __block_for_int(p);
}


// Workaround for an Emscripten bug: getentropy(buffer, 1) returns the single
// byte of entropy as the return code. Fixed upstream by
// emscripten-core/emscripten#27122
int __real_getentropy(void*, size_t);

int __wrap_getentropy(void *buffer, size_t len) {
if (len != 1) {
return __real_getentropy(buffer, len);
}
// Length is 1. Workaround is to get two bytes of entropy and write the
// first one into the original target buffer.
uint8_t tmp[2];
int ret = __real_getentropy(tmp, 2);
if (ret == 0) {
*(uint8_t *)buffer = tmp[0];
}
return ret;
}

#include <sys/ioctl.h>

int syscall_ioctl_orig(int fd, int request, void* varargs)
Expand Down
2 changes: 0 additions & 2 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2411,10 +2411,6 @@ AS_CASE([$ac_sys_system],
dnl Avoid bugs in JS fallback string decoding path
AS_VAR_APPEND([LINKFORSHARED], [" -sTEXTDECODER=2"])

dnl Workaround for a bug in Emscipten libc's getentropy. See
dnl __wrap_getentropy in Python/emscripten_syscalls.c.
AS_VAR_APPEND([LDFLAGS_NODIST], [" -Wl,--wrap=getentropy"])

AS_VAR_IF([enable_wasm_dynamic_linking], [yes], [
AS_VAR_APPEND([LINKFORSHARED], [" -sMAIN_MODULE"])
])
Expand Down
Loading