Merge branch 'master' into sh_merge_master

This commit is contained in:
Ralf W. Grosse-Kunstleve 2022-02-23 15:47:03 -08:00
commit be43439670
9 changed files with 86 additions and 12 deletions

View File

@ -46,7 +46,7 @@ jobs:
args: >
-DPYBIND11_FINDPYTHON=ON
-DCMAKE_CXX_FLAGS="-D_=1"
- runs-on: windows-latest
- runs-on: windows-2019
python: '3.6'
args: >
-DPYBIND11_FINDPYTHON=ON
@ -718,7 +718,7 @@ jobs:
args: -DCMAKE_CXX_STANDARD=17
name: "🐍 ${{ matrix.python }} • MSVC 2019 • x86 ${{ matrix.args }}"
runs-on: windows-latest
runs-on: windows-2019
steps:
- uses: actions/checkout@v2

View File

@ -73,7 +73,7 @@ repos:
# Autoremoves unused imports
- repo: https://github.com/hadialqattan/pycln
rev: "v1.1.0"
rev: "v1.2.0"
hooks:
- id: pycln
@ -166,6 +166,7 @@ repos:
# Clang format the codebase automatically
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: "v13.0.0"
rev: "v13.0.1"
hooks:
- id: clang-format
types_or: [c++, c, cuda]

View File

@ -397,7 +397,7 @@ struct string_caster {
return false;
}
if (!PyUnicode_Check(load_src.ptr())) {
return load_bytes(load_src);
return load_raw(load_src);
}
// For UTF-8 we avoid the need for a temporary `bytes` object by using
@ -475,26 +475,37 @@ private:
#endif
}
// When loading into a std::string or char*, accept a bytes object as-is (i.e.
// When loading into a std::string or char*, accept a bytes/bytearray object as-is (i.e.
// without any encoding/decoding attempt). For other C++ char sizes this is a no-op.
// which supports loading a unicode from a str, doesn't take this path.
template <typename C = CharT>
bool load_bytes(enable_if_t<std::is_same<C, char>::value, handle> src) {
bool load_raw(enable_if_t<std::is_same<C, char>::value, handle> src) {
if (PYBIND11_BYTES_CHECK(src.ptr())) {
// We were passed raw bytes; accept it into a std::string or char*
// without any encoding attempt.
const char *bytes = PYBIND11_BYTES_AS_STRING(src.ptr());
if (bytes) {
value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
return true;
if (!bytes) {
pybind11_fail("Unexpected PYBIND11_BYTES_AS_STRING() failure.");
}
value = StringType(bytes, (size_t) PYBIND11_BYTES_SIZE(src.ptr()));
return true;
}
if (PyByteArray_Check(src.ptr())) {
// We were passed a bytearray; accept it into a std::string or char*
// without any encoding attempt.
const char *bytearray = PyByteArray_AsString(src.ptr());
if (!bytearray) {
pybind11_fail("Unexpected PyByteArray_AsString() failure.");
}
value = StringType(bytearray, (size_t) PyByteArray_Size(src.ptr()));
return true;
}
return false;
}
template <typename C = CharT>
bool load_bytes(enable_if_t<!std::is_same<C, char>::value, handle>) {
bool load_raw(enable_if_t<!std::is_same<C, char>::value, handle>) {
return false;
}
};

View File

@ -936,9 +936,11 @@ PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybin
PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally
[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason) {
assert(!PyErr_Occurred());
throw std::runtime_error(reason);
}
[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const std::string &reason) {
assert(!PyErr_Occurred());
throw std::runtime_error(reason);
}

View File

@ -198,6 +198,10 @@ inline void finalize_interpreter() {
if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
internals_ptr_ptr = capsule(builtins[id]);
}
// Local internals contains data managed by the current interpreter, so we must clear them to
// avoid undefined behaviors when initializing another interpreter
detail::get_local_internals().registered_types_cpp.clear();
detail::get_local_internals().registered_exception_translators.clear();
Py_Finalize();

View File

@ -1509,6 +1509,9 @@ public:
explicit weakref(handle obj, handle callback = {})
: object(PyWeakref_NewRef(obj.ptr(), callback.ptr()), stolen_t{}) {
if (!m_ptr) {
if (PyErr_Occurred()) {
throw error_already_set();
}
pybind11_fail("Could not allocate weak reference!");
}
}

View File

@ -133,6 +133,15 @@ def test_bytes_to_string():
assert m.string_length("💩".encode()) == 4
def test_bytearray_to_string():
"""Tests the ability to pass bytearray to C++ string-accepting functions"""
assert m.string_length(bytearray(b"Hi")) == 2
assert m.strlen(bytearray(b"bytearray")) == 9
assert m.string_length(bytearray()) == 0
assert m.string_length(bytearray("🦜", "utf-8", "strict")) == 4
assert m.string_length(bytearray(b"\x80")) == 1
@pytest.mark.skipif(not hasattr(m, "has_string_view"), reason="no <string_view>")
def test_string_view(capture):
"""Tests support for C++17 string_view arguments and return values"""

View File

@ -375,3 +375,21 @@ TEST_CASE("sys.argv gets initialized properly") {
}
py::initialize_interpreter();
}
TEST_CASE("make_iterator can be called before then after finalizing an interpreter") {
// Reproduction of issue #2101 (https://github.com/pybind/pybind11/issues/2101)
py::finalize_interpreter();
std::vector<int> container;
{
pybind11::scoped_interpreter g;
auto iter = pybind11::make_iterator(container.begin(), container.end());
}
REQUIRE_NOTHROW([&]() {
pybind11::scoped_interpreter g;
auto iter = pybind11::make_iterator(container.begin(), container.end());
}());
py::initialize_interpreter();
}

View File

@ -1,8 +1,9 @@
import contextlib
import sys
import pytest
import env # noqa: F401
import env
from pybind11_tests import debug_enabled
from pybind11_tests import pytypes as m
@ -583,6 +584,31 @@ def test_weakref(create_weakref, create_weakref_with_callback):
assert callback_called
@pytest.mark.parametrize(
"create_weakref, has_callback",
[
(m.weakref_from_handle, False),
(m.weakref_from_object, False),
(m.weakref_from_handle_and_function, True),
(m.weakref_from_object_and_function, True),
],
)
def test_weakref_err(create_weakref, has_callback):
class C:
__slots__ = []
def callback(_):
pass
ob = C()
# Should raise TypeError on CPython
with pytest.raises(TypeError) if not env.PYPY else contextlib.nullcontext():
if has_callback:
_ = create_weakref(ob, callback)
else:
_ = create_weakref(ob)
def test_cpp_iterators():
assert m.tuple_iterator() == 12
assert m.dict_iterator() == 305 + 711