Skip to content

pytypes: Add Gotchas section about default-constructed wrapper types and py::none() #2362

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

Merged
Merged
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
27 changes: 27 additions & 0 deletions docs/advanced/pycpp/object.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ Available types include :class:`handle`, :class:`object`, :class:`bool_`,
:class:`iterable`, :class:`iterator`, :class:`function`, :class:`buffer`,
:class:`array`, and :class:`array_t`.

.. warning::

Be sure to review the :ref:`pytypes_gotchas` before using this heavily in
your C++ API.

Casting back and forth
======================

Expand Down Expand Up @@ -178,3 +183,25 @@ Python exceptions from wrapper classes will be thrown as a ``py::error_already_s
See :ref:`Handling exceptions from Python in C++
<handling_python_exceptions_cpp>` for more information on handling exceptions
raised when calling C++ wrapper classes.

.. _pytypes_gotchas:

Gotchas
=======

Default-Constructed Wrappers
----------------------------

When a wrapper type is default-constructed, it is **not** a valid Python object (i.e. it is not ``py::none()``). It is simply the same as
``PyObject*`` null pointer. To check for this, use
``static_cast<bool>(my_wrapper)``.

Assigning py::none() to wrappers
--------------------------------

You may be tempted to use types like ``py::str`` and ``py::dict`` in C++
signatures (either pure C++, or in bound signatures), and assign them default
values of ``py::none()``. However, in a best case scenario, it will fail fast
because ``None`` is not convertible to that type (e.g. ``py::dict``), or in a
worse case scenario, it will silently work but corrupt the types you want to
work with (e.g. ``py::str(py::none())`` will yield ``"None"`` in Python).
10 changes: 10 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,16 @@ TEST_SUBMODULE(pytypes, m) {
return a[py::slice(0, -1, 2)];
});

// See #2361
m.def("issue2361_str_implicit_copy_none", []() {
py::str is_this_none = py::none();
return is_this_none;
});
m.def("issue2361_dict_implicit_copy_none", []() {
py::dict is_this_none = py::none();
return is_this_none;
});

m.def("test_memoryview_object", [](py::buffer b) {
return py::memoryview(b);
});
Expand Down
8 changes: 8 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ def test_list_slicing():
assert li[::2] == m.test_list_slicing(li)


def test_issue2361():
# See issue #2361
assert m.issue2361_str_implicit_copy_none() == "None"
with pytest.raises(TypeError) as excinfo:
assert m.issue2361_dict_implicit_copy_none()
assert "'NoneType' object is not iterable" in str(excinfo.value)


@pytest.mark.parametrize('method, args, fmt, expected_view', [
(m.test_memoryview_object, (b'red',), 'B', b'red'),
(m.test_memoryview_buffer_info, (b'green',), 'B', b'green'),
Expand Down