From 733f8de24feed964f96b639a0a44247f46bed868 Mon Sep 17 00:00:00 2001 From: jonathan-conder-sm <63538679+jonathan-conder-sm@users.noreply.github.com> Date: Thu, 1 Jul 2021 17:19:14 +1200 Subject: [PATCH 1/2] Avoid string copy if possible when passing a Python object to std::ostream (#3042) --- include/pybind11/stl.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 61d3fba61..ca20b7483 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -380,7 +380,11 @@ struct type_caster> : variant_caster> { PYBIND11_NAMESPACE_END(detail) inline std::ostream &operator<<(std::ostream &os, const handle &obj) { +#ifdef PYBIND11_HAS_STRING_VIEW + os << str(obj).cast(); +#else os << (std::string) str(obj); +#endif return os; } From f067deb563d1073a7193b87d058fce37838ea5aa Mon Sep 17 00:00:00 2001 From: cyy Date: Thu, 1 Jul 2021 14:35:25 +0800 Subject: [PATCH 2/2] avoid unnecessary strlen (#3058) --- include/pybind11/attr.h | 2 +- include/pybind11/pybind11.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 50efdc7ce..97147904c 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -377,7 +377,7 @@ template <> struct process_attribute : process_attribu }; inline void process_kw_only_arg(const arg &a, function_record *r) { - if (!a.name || strlen(a.name) == 0) + if (!a.name || a.name[0] == '\0') pybind11_fail("arg(): cannot specify an unnamed argument after an kw_only() annotation"); ++r->nargs_kw_only; } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 1fb0d1fa1..9938b0b71 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -470,7 +470,7 @@ protected: signatures += it->signature; signatures += "\n"; } - if (it->doc && strlen(it->doc) > 0 && options::show_user_defined_docstrings()) { + if (it->doc && it->doc[0] != '\0' && options::show_user_defined_docstrings()) { // If we're appending another docstring, and aren't printing function signatures, we // need to append a newline first: if (!options::show_function_signatures()) {