Skip to content

Commit

Permalink
minor cleanups, added missing documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Oct 13, 2023
1 parent b09d26d commit d9cecac
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
4 changes: 4 additions & 0 deletions docs/api_core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2495,6 +2495,10 @@ Miscellaneous

Return the ``__builtins__`` dictionary.

.. cpp:function:: dict globals()

Return the ``globals()`` dictionary.

.. cpp:function:: template <typename Source, typename Target> void implicitly_convertible()

Indicate that the type `Source` is implicitly convertible into `Target`
Expand Down
4 changes: 2 additions & 2 deletions include/nanobind/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ object eval(const str &expr, handle global = handle(), handle local = handle())
// This used to be PyRun_String, but that function isn't in the stable ABI.
object codeobj = steal(Py_CompileString(expr.c_str(), "<string>", start));
if (!codeobj.is_valid())
detail::raise_python_error();
raise_python_error();

PyObject *result = PyEval_EvalCode(codeobj.ptr(), global.ptr(), local.ptr());
if (!result)
detail::raise_python_error();
raise_python_error();

return steal(result);
}
Expand Down
10 changes: 5 additions & 5 deletions include/nanobind/nb_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,22 +479,22 @@ detail::accessor<Impl>& detail::accessor<Impl>::operator=(T &&value) {
template <typename T> void list::append(T &&value) {
object o = nanobind::cast((detail::forward_t<T>) value);
if (PyList_Append(m_ptr, o.ptr()))
detail::raise_python_error();
raise_python_error();
}

template <typename T> bool dict::contains(T&& key) const {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PyDict_Contains(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
raise_python_error();
return rv == 1;
}

template <typename T> bool set::contains(T&& key) const {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PySet_Contains(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
raise_python_error();
return rv == 1;
}

Expand All @@ -503,15 +503,15 @@ template <typename T> void set::add(T&& key) {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PySet_Add(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
raise_python_error();
}


template <typename T> bool mapping::contains(T&& key) const {
object o = nanobind::cast((detail::forward_t<T>) key);
int rv = PyMapping_HasKey(m_ptr, o.ptr());
if (rv == -1)
detail::raise_python_error();
raise_python_error();
return rv == 1;
}

Expand Down
2 changes: 1 addition & 1 deletion include/nanobind/nb_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ inline void set_implicit_cast_warnings(bool value) noexcept {
inline dict globals() {
PyObject *p = PyEval_GetGlobals();
if (!p)
detail::raise("nanobind::globals(): no frame is currently executing!");
raise("nanobind::globals(): no frame is currently executing!");
return borrow<dict>(p);
}

Expand Down
10 changes: 5 additions & 5 deletions include/nanobind/nb_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class int_ : public object {
detail::type_caster<T>::from_cpp(value, rv_policy::copy, nullptr),
detail::steal_t{}) {
if (!m_ptr)
detail::raise_python_error();
raise_python_error();
}

template <typename T, detail::enable_if_t<std::is_arithmetic_v<T>> = 0>
Expand All @@ -373,7 +373,7 @@ class float_ : public object {
explicit float_(double value)
: object(PyFloat_FromDouble(value), detail::steal_t{}) {
if (!m_ptr)
detail::raise_python_error();
raise_python_error();
}

#if !defined(Py_LIMITED_API)
Expand Down Expand Up @@ -476,7 +476,7 @@ class set : public object {
template <typename T> void add(T &&value);
void clear() {
if (PySet_Clear(m_ptr))
detail::raise_python_error();
raise_python_error();
}
};

Expand Down Expand Up @@ -586,7 +586,7 @@ class slice : public object {
slice(handle start, handle stop, handle step) {
m_ptr = PySlice_New(start.ptr(), stop.ptr(), step.ptr());
if (!m_ptr)
detail::raise_python_error();
raise_python_error();
}

template <typename T, detail::enable_if_t<std::is_arithmetic_v<T>> = 0>
Expand Down Expand Up @@ -633,7 +633,7 @@ class weakref : public object {
explicit weakref(handle obj, handle callback = {})
: object(PyWeakref_NewRef(obj.ptr(), callback.ptr()), detail::steal_t{}) {
if (!m_ptr)
detail::raise_python_error();
raise_python_error();
}
};

Expand Down

0 comments on commit d9cecac

Please sign in to comment.