Skip to content

Commit 348faa4

Browse files
committed
support None for const char* arg, with minimal overhead
Since pybind11 also accepts None for const char* as an exception among built-in types, remove a paragraph about differences in porting.rst.
1 parent b7c4f1a commit 348faa4

File tree

6 files changed

+13
-11
lines changed

6 files changed

+13
-11
lines changed

docs/api_core.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -1636,9 +1636,11 @@ parameter of :cpp:func:`module_::def`, :cpp:func:`class_::def`,
16361636

16371637
Set a flag noting that the function argument accepts ``None``. Can only
16381638
be used for python wrapper types (e.g. :cpp:class:`handle`,
1639-
:cpp:class:`int_`) and types that have been bound using
1640-
:cpp:class:`class_`. You cannot use this to implement functions that
1641-
accept null pointers to builtin C++ types like ``int *i = nullptr``.
1639+
:cpp:class:`int_`), for types that have been bound using
1640+
:cpp:class:`class_`, for :cpp:class:`ndarray`, ``std::optional``,
1641+
``std::variant``, and ``const char*``.
1642+
You cannot use this to implement functions that
1643+
accept null pointers to other builtin C++ types like ``int *i = nullptr``.
16421644

16431645
.. cpp:function:: arg &noconvert(bool value = true)
16441646

docs/porting.rst

-6
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ It is also possible to set a ``None`` default value, in which case the
8686
8787
m.def("func", &func, "arg"_a = nb::none());
8888
89-
``None``-valued arguments are only supported by two of the three parameter
90-
passing styles described in the section on :ref:`information exchange
91-
<exchange>`. In particular, they are supported by :ref:`bindings <bindings>`
92-
and :ref:`wrappers <wrappers>`, *but not* by :ref:`type casters
93-
<type_casters>`.
94-
9589
Shared pointers and holders
9690
---------------------------
9791

include/nanobind/nb_cast.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@ template <> struct type_caster<char> {
268268
template <typename T_>
269269
using Cast = std::conditional_t<is_pointer_v<T_>, const char *, char>;
270270

271-
bool from_python(handle src, uint8_t, cleanup_list *) noexcept {
271+
bool from_python(handle src, uint8_t flags, cleanup_list *) noexcept {
272272
value = PyUnicode_AsUTF8AndSize(src.ptr(), &size);
273273
if (!value) {
274274
PyErr_Clear();
275-
return false;
275+
// optimize for src being a string, check for None afterwards
276+
return src.is_none() && (flags & (uint8_t) cast_flags::accepts_none);
276277
}
277278
return true;
278279
}

tests/test_functions.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ NB_MODULE(test_functions_ext, m) {
240240

241241
// Test string caster
242242
m.def("test_12", [](const char *c) { return nb::str(c); });
243+
m.def("test_12_n", [](const char *c) { return nb::str(c ? c : "n/a"); }, nb::arg().none());
243244
m.def("test_13", []() -> const char * { return "test"; });
244245
m.def("test_14", [](nb::object o) -> const char * { return nb::cast<const char *>(o); });
245246

tests/test_functions.py

+2
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ def test21_numpy_overloads():
256256

257257
def test22_string_return():
258258
assert t.test_12("hello") == "hello"
259+
assert t.test_12_n("hello") == "hello"
260+
assert t.test_12_n(None) == "n/a"
259261
assert t.test_13() == "test"
260262
assert t.test_14("abc") == "abc"
261263

tests/test_functions_ext.pyi.ref

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def test_11_ull(arg: int, /) -> int: ...
100100

101101
def test_12(arg: str, /) -> str: ...
102102

103+
def test_12_n(arg: str | None) -> str: ...
104+
103105
def test_13() -> str: ...
104106

105107
def test_14(arg: object, /) -> str: ...

0 commit comments

Comments
 (0)