| 3292 | |
| 3293 | PYBIND11_NAMESPACE_BEGIN(detail) |
| 3294 | PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) { |
| 3295 | auto strings = tuple(args.size()); |
| 3296 | for (size_t i = 0; i < args.size(); ++i) { |
| 3297 | strings[i] = str(args[i]); |
| 3298 | } |
| 3299 | auto sep = kwargs.contains("sep") ? kwargs["sep"] : str(" "); |
| 3300 | auto line = sep.attr("join")(std::move(strings)); |
| 3301 | |
| 3302 | object file; |
| 3303 | if (kwargs.contains("file")) { |
| 3304 | file = kwargs["file"].cast<object>(); |
| 3305 | } else { |
| 3306 | try { |
| 3307 | file = module_::import("sys").attr("stdout"); |
| 3308 | } catch (const error_already_set &) { |
| 3309 | /* If print() is called from code that is executed as |
| 3310 | part of garbage collection during interpreter shutdown, |
| 3311 | importing 'sys' can fail. Give up rather than crashing the |
| 3312 | interpreter in this case. */ |
| 3313 | return; |
| 3314 | } |
| 3315 | } |
| 3316 | |
| 3317 | auto write = file.attr("write"); |
| 3318 | write(std::move(line)); |
| 3319 | write(kwargs.contains("end") ? kwargs["end"] : str("\n")); |
| 3320 | |
| 3321 | if (kwargs.contains("flush") && kwargs["flush"].cast<bool>()) { |
| 3322 | file.attr("flush")(); |
| 3323 | } |
| 3324 | } |
| 3325 | PYBIND11_NAMESPACE_END(detail) |
| 3326 | |
| 3327 | template <return_value_policy policy = return_value_policy::automatic_reference, typename... Args> |
no test coverage detected