Skip to content

Commit

Permalink
Add support for bool (#382)
Browse files Browse the repository at this point in the history
* Add bool_ support

* Directly compare against Py_True
  • Loading branch information
matthewdcong authored and wjakob committed Dec 12, 2023
1 parent eb308de commit 90dfbaf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/api_core.rst
Expand Up @@ -892,6 +892,24 @@ Wrapper classes
Return the pointer wrapped by the capsule.


.. cpp:class:: bool_: public object

This wrapper class represents Python ``bool`` instances.

.. cpp:function:: int_(handle h)

Performs a boolean cast within Python. This is equivalent to the Python
expression ``bool(h)``.

.. cpp:function:: explicit bool_(bool value)

Convert an C++ boolean instance into a Python ``bool``.

.. cpp:function:: explicit operator bool() const

Extract the boolean value underlying this object.


.. cpp:class:: int_: public object

This wrapper class represents Python ``int`` instances. It can handle large
Expand Down
2 changes: 1 addition & 1 deletion docs/exchanging.rst
Expand Up @@ -384,7 +384,7 @@ directives:
:cpp:class:`bytes`, :cpp:class:`callable`, :cpp:class:`capsule`,
:cpp:class:`dict`, :cpp:class:`ellipsis`, :cpp:class:`handle`,
:cpp:class:`handle_t\<T\> <handle_t>`,
:cpp:class:`int_`, :cpp:class:`float_`,
:cpp:class:`bool_`, :cpp:class:`int_`, :cpp:class:`float_`,
:cpp:class:`iterable`,
:cpp:class:`iterator`, :cpp:class:`list`, :cpp:class:`mapping`,
:cpp:class:`module_`, :cpp:class:`object`, :cpp:class:`set`, :cpp:class:`sequence`,
Expand Down
3 changes: 3 additions & 0 deletions include/nanobind/nb_lib.h
Expand Up @@ -133,6 +133,9 @@ NB_CORE PyObject *bytes_from_cstr_and_size(const char *c, size_t n);

// ========================================================================

/// Convert a Python object into a Python boolean object
NB_CORE PyObject *bool_from_obj(PyObject *o);

/// Convert a Python object into a Python integer object
NB_CORE PyObject *int_from_obj(PyObject *o);

Expand Down
14 changes: 14 additions & 0 deletions include/nanobind/nb_types.h
Expand Up @@ -340,6 +340,20 @@ class capsule : public object {
void *data() const { return PyCapsule_GetPointer(m_ptr, name()); }
};

class bool_ : public object {
NB_OBJECT_DEFAULT(bool_, object, "bool", PyBool_Check)

explicit bool_(handle h)
: object(detail::bool_from_obj(h.ptr()), detail::borrow_t{}) { }

explicit bool_(bool value)
: object(value ? Py_True : Py_False, detail::borrow_t{}) { }

explicit operator bool() const {
return m_ptr == Py_True;
}
};

class int_ : public object {
NB_OBJECT_DEFAULT(int_, object, "int", PyLong_Check)

Expand Down
7 changes: 7 additions & 0 deletions src/common.cpp
Expand Up @@ -566,6 +566,13 @@ PyObject *bytes_from_cstr_and_size(const char *str, size_t size) {

// ========================================================================

PyObject *bool_from_obj(PyObject *o) {
int rv = PyObject_IsTrue(o);
if (rv == -1)
raise_python_error();
return rv == 1 ? Py_True : Py_False;
}

PyObject *int_from_obj(PyObject *o) {
PyObject *result = PyNumber_Long(o);
if (!result)
Expand Down

0 comments on commit 90dfbaf

Please sign in to comment.