-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: align py::print with Python semantics #6120
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
Changes from all commits
b210aac
ba52003
0ce2e59
a440b60
7aa9532
a7ac2b6
d20ede5
3fd7ad2
de1ef9a
e0a770c
8dd4776
1dcb760
b60e256
7a08eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -643,6 +643,9 @@ TEST_SUBMODULE(pytypes, m) { | |
| "{a} + {b} = {c}"_s.format("a"_a = "py::print", "b"_a = "str.format", "c"_a = "this")); | ||
| }); | ||
|
|
||
| m.def("print_args", | ||
| [](const py::args &args, const py::kwargs &kwargs) { py::print(*args, **kwargs); }); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hold on can this it just be bound as py::print? Or does the overloading system not like that?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I pointed codex to this question. I then added this to the PR #6121 Description: The lambda is intentional: |
||
|
|
||
| m.def("print_failure", []() { py::print(42, UnregisteredType()); }); | ||
|
|
||
| m.def("hash_function", [](py::object obj) { return py::hash(std::move(obj)); }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Shared harness for the py::print-during-shutdown regression tests in | ||
| // test_interpreter.cpp and test_subinterpreter.cpp. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <pybind11/pybind11.h> | ||
|
|
||
| struct print_shutdown_state { | ||
| bool callback_ran = false; | ||
| bool stdout_was_none = false; | ||
| bool print_threw = false; | ||
| }; | ||
|
|
||
| // Attach a capsule to sys whose destructor calls py::print during interpreter | ||
| // shutdown, recording what happened in `state`. | ||
| inline void install_print_shutdown_probe(print_shutdown_state &state) { | ||
| namespace py = pybind11; | ||
| py::module_::import("sys").attr("pybind11_print_on_shutdown") | ||
| = py::capsule(&state, [](void *payload) noexcept { | ||
| auto *state = static_cast<print_shutdown_state *>(payload); | ||
| state->callback_ran = true; | ||
| state->stdout_was_none = PySys_GetObject("stdout") == Py_None; | ||
| try { | ||
| py::print("print during interpreter shutdown"); | ||
| } catch (...) { | ||
| state->print_threw = true; | ||
| } | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use PyObject instead of handles or py::object here?