Skip to content

[WIP] Test cprofile compatibility #5601

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ set(PYBIND11_TEST_FILES
test_constants_and_functions
test_copy_move
test_cpp_conduit
test_cprofile_compatibility.py
test_custom_type_casters
test_custom_type_setup
test_docstring_options
Expand Down Expand Up @@ -241,6 +242,7 @@ tests_extra_targets("test_exceptions.py" "cross_module_interleaved_error_already
tests_extra_targets("test_gil_scoped.py" "cross_module_gil_utils")
tests_extra_targets("test_cpp_conduit.py"
"exo_planet_pybind11;exo_planet_c_api;home_planet_very_lonely_traveler")
tests_extra_targets("test_cprofile_compatibility.py" "cprofile_compatibility_ext")

set(PYBIND11_EIGEN_REPO
"https://gitlab.com/libeigen/eigen.git"
Expand Down
19 changes: 19 additions & 0 deletions tests/cprofile_compatibility_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <pybind11/pybind11.h>

namespace pybind11_tests {
namespace cprofile_compatibility {
class CppClass {};
} // namespace cprofile_compatibility
} // namespace pybind11_tests

namespace py = pybind11;

PYBIND11_MODULE(cprofile_compatibility_ext, m) {
using namespace pybind11_tests::cprofile_compatibility;

m.def("free_func_return_secret", []() { return 102; });

py::class_<CppClass>(m, "CppClass")
.def(py::init<>())
.def("member_func_return_secret", [](const CppClass &) { return 203; });
}
43 changes: 43 additions & 0 deletions tests/test_cprofile_compatibility.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from __future__ import annotations

import cProfile as profile
import io
import pstats

import cprofile_compatibility_ext


def _get_pstats(profiler):
output = io.StringIO()
stats = pstats.Stats(profiler, stream=output)
stats.sort_stats("cumtime")
stats.print_stats()
return output.getvalue()


class PyClass:
def py_member_func(self) -> None:
return cprofile_compatibility_ext.CppClass().member_func_return_secret()


def test_free_func_return_secret():
profiler = profile.Profile()
profiler.enable()
assert cprofile_compatibility_ext.free_func_return_secret() == 102
profiler.disable()
stats_output = _get_pstats(profiler)

Check failure on line 28 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 graalpy-24.2 • ubuntu-20.04 • x64

test_free_func_return_secret TypeError: Cannot create or construct a <class 'pstats.Stats'> object from <cProfile.Profile object at 0x3112c7e0>

Check failure on line 28 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 graalpy-24.1 • ubuntu-latest • x64

test_free_func_return_secret TypeError: Cannot create or construct a <class 'pstats.Stats'> object from <cProfile.Profile object at 0x55e910c2>
raise AssertionError(

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.13 • ubuntu-20.04 • x64

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 1 0.000 0.000 0.000 0.000 {built-in method cprofile_compatibility_ext.free_func_return_secret} pstats.Stats output free_func END

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.9 • ubuntu-20.04 • x64

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {built-in method cprofile_compatibility_ext.free_func_return_secret} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output free_func END

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.8 • ubuntu-20.04 • x64 -DPYBIND11_FINDPYTHON=OFF -DCMAKE_CXX_FLAGS="-D_=1" -DPYBIND11_NUMPY_1_ONLY=ON

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {built-in method cprofile_compatibility_ext.free_func_return_secret} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output free_func END

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.12 • ubuntu-latest • x64 -DCMAKE_CXX_FLAGS="-DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE"

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 1 0.000 0.000 0.000 0.000 {built-in method cprofile_compatibility_ext.free_func_return_secret} pstats.Stats output free_func END

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.12 • ubuntu-20.04 • x64

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 1 0.000 0.000 0.000 0.000 {built-in method cprofile_compatibility_ext.free_func_return_secret} pstats.Stats output free_func END

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.11 • ubuntu-latest • x64

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {built-in method cprofile_compatibility_ext.free_func_return_secret} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output free_func END

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.10 • ubuntu-20.04 • x64

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 1 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output free_func END

Check failure on line 29 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.11 • ubuntu-20.04 • x64

test_free_func_return_secret AssertionError: pstats.Stats output free_func BEGIN: 1 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output free_func END
f"\npstats.Stats output free_func BEGIN:\n{stats_output}\npstats.Stats output free_func END\n"
)


def test_member_func_return_secret():
obj = PyClass()
profiler = profile.Profile()
profiler.enable()
assert obj.py_member_func() == 203
profiler.disable()
stats_output = _get_pstats(profiler)
raise AssertionError(

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.13 • ubuntu-20.04 • x64

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 1 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.9 • ubuntu-20.04 • x64

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 /home/runner/work/pybind11/pybind11/tests/test_cprofile_compatibility.py:19(py_member_func) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.8 • ubuntu-20.04 • x64 -DPYBIND11_FINDPYTHON=OFF -DCMAKE_CXX_FLAGS="-D_=1" -DPYBIND11_NUMPY_1_ONLY=ON

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 /home/runner/work/pybind11/pybind11/tests/test_cprofile_compatibility.py:19(py_member_func) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.12 • ubuntu-latest • x64 -DCMAKE_CXX_FLAGS="-DPYBIND11_RUN_TESTING_WITH_SMART_HOLDER_AS_DEFAULT_BUT_NEVER_USE_IN_PRODUCTION_PLEASE"

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 1 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.12 • ubuntu-20.04 • x64

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 1 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 3.11 • ubuntu-latest • x64

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 3 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 /home/runner/work/pybind11/pybind11/tests/test_cprofile_compatibility.py:19(py_member_func) 1 0.000 0.000 0.000 0.000 {built-in method cprofile_compatibility_ext.member_func_return_secret} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.10 • ubuntu-20.04 • x64

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 /home/runner/work/pybind11/pybind11/tests/test_cprofile_compatibility.py:19(py_member_func) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END

Check failure on line 41 in tests/test_cprofile_compatibility.py

View workflow job for this annotation

GitHub Actions / 🐍 pypy-3.11 • ubuntu-20.04 • x64

test_member_func_return_secret AssertionError: pstats.Stats output member_func BEGIN: 2 function calls in 0.000 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 /home/runner/work/pybind11/pybind11/tests/test_cprofile_compatibility.py:19(py_member_func) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} pstats.Stats output member_func END
f"\npstats.Stats output member_func BEGIN:\n{stats_output}\npstats.Stats output member_func END\n"
)
Loading