Skip to content

Commit

Permalink
fix: patch PyPy
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed May 25, 2024
1 parent c57a840 commit f74d23a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,15 @@ class object_api : public pyobject_tag {
str_attr_accessor doc() const;

/// Return the object's current reference count
ssize_t ref_count() const { return Py_REFCNT(derived().ptr()); }
ssize_t ref_count() const {
#ifdef PYPY_VERSION
// PyPy uses the top few bits for REFCNT_FROM_PYPY & REFCNT_FROM_PYPY_LIGHT
// Following pybind11 2.12.1 and older behavior and removing this part
return static_cast<ssize_t>(static_cast<int>(Py_REFCNT(derived().ptr())));
#else
return Py_REFCNT(derived().ptr());
#endif
}

// TODO PYBIND11_DEPRECATED(
// "Call py::type::handle_of(h) or py::type::of(h) instead of h.get_type()")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_kwargs_and_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def test_args_refcount():
myval = 54321
expected = refcount(myval)
assert m.arg_refcount_h(myval) == expected
assert m.arg_refcount_o(myval) == expected + 1
assert m.arg_refcount_o(myval) in {expected + 1, 2**32 - 1}
assert m.arg_refcount_h(myval) == expected
assert refcount(myval) == expected

Expand Down

0 comments on commit f74d23a

Please sign in to comment.