mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-23 05:35:13 +00:00
Merge branch 'master' into sh_merge_master
This commit is contained in:
commit
4db19272cb
5
.github/workflows/ci.yml
vendored
5
.github/workflows/ci.yml
vendored
@ -33,6 +33,7 @@ jobs:
|
|||||||
- '3.10'
|
- '3.10'
|
||||||
- 'pypy-3.7'
|
- 'pypy-3.7'
|
||||||
- 'pypy-3.8'
|
- 'pypy-3.8'
|
||||||
|
- 'pypy-3.9'
|
||||||
|
|
||||||
# Items in here will either be added to the build matrix (if not
|
# Items in here will either be added to the build matrix (if not
|
||||||
# present), or add new keys to an existing matrix element if all the
|
# present), or add new keys to an existing matrix element if all the
|
||||||
@ -46,6 +47,10 @@ jobs:
|
|||||||
args: >
|
args: >
|
||||||
-DPYBIND11_FINDPYTHON=ON
|
-DPYBIND11_FINDPYTHON=ON
|
||||||
-DCMAKE_CXX_FLAGS="-D_=1"
|
-DCMAKE_CXX_FLAGS="-D_=1"
|
||||||
|
- runs-on: ubuntu-latest
|
||||||
|
python: 'pypy-3.8'
|
||||||
|
args: >
|
||||||
|
-DPYBIND11_FINDPYTHON=ON
|
||||||
- runs-on: windows-2019
|
- runs-on: windows-2019
|
||||||
python: '3.6'
|
python: '3.6'
|
||||||
args: >
|
args: >
|
||||||
|
@ -46,7 +46,7 @@ repos:
|
|||||||
|
|
||||||
# Black, the code formatter, natively supports pre-commit
|
# Black, the code formatter, natively supports pre-commit
|
||||||
- repo: https://github.com/psf/black
|
- repo: https://github.com/psf/black
|
||||||
rev: "22.1.0" # Keep in sync with blacken-docs
|
rev: "22.3.0" # Keep in sync with blacken-docs
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ repos:
|
|||||||
hooks:
|
hooks:
|
||||||
- id: blacken-docs
|
- id: blacken-docs
|
||||||
additional_dependencies:
|
additional_dependencies:
|
||||||
- black==22.1.0 # keep in sync with black hook
|
- black==22.3.0 # keep in sync with black hook
|
||||||
|
|
||||||
# Changes tabs to spaces
|
# Changes tabs to spaces
|
||||||
- repo: https://github.com/Lucas-C/pre-commit-hooks
|
- repo: https://github.com/Lucas-C/pre-commit-hooks
|
||||||
@ -76,6 +76,8 @@ repos:
|
|||||||
rev: "v1.2.5"
|
rev: "v1.2.5"
|
||||||
hooks:
|
hooks:
|
||||||
- id: pycln
|
- id: pycln
|
||||||
|
additional_dependencies: [click<8.1] # Unpin when typer updates
|
||||||
|
stages: [manual]
|
||||||
|
|
||||||
# Checking for common mistakes
|
# Checking for common mistakes
|
||||||
- repo: https://github.com/pre-commit/pygrep-hooks
|
- repo: https://github.com/pre-commit/pygrep-hooks
|
||||||
@ -109,7 +111,7 @@ repos:
|
|||||||
|
|
||||||
# PyLint has native support - not always usable, but works for us
|
# PyLint has native support - not always usable, but works for us
|
||||||
- repo: https://github.com/PyCQA/pylint
|
- repo: https://github.com/PyCQA/pylint
|
||||||
rev: "v2.12.2"
|
rev: "v2.13.2"
|
||||||
hooks:
|
hooks:
|
||||||
- id: pylint
|
- id: pylint
|
||||||
files: ^pybind11
|
files: ^pybind11
|
||||||
@ -125,7 +127,7 @@ repos:
|
|||||||
|
|
||||||
# Check static types with mypy
|
# Check static types with mypy
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: "v0.941"
|
rev: "v0.942"
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
args: [--show-error-codes]
|
args: [--show-error-codes]
|
||||||
|
@ -225,8 +225,8 @@ PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
|
|||||||
if (throw_if_missing) {
|
if (throw_if_missing) {
|
||||||
std::string tname = tp.name();
|
std::string tname = tp.name();
|
||||||
detail::clean_type_id(tname);
|
detail::clean_type_id(tname);
|
||||||
pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \"" + tname
|
pybind11_fail("pybind11::detail::get_type_info: unable to find type info for \""
|
||||||
+ "\"");
|
+ std::move(tname) + '"');
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@ -512,9 +512,13 @@ PYBIND11_NOINLINE std::string error_string() {
|
|||||||
Py_INCREF(f_code);
|
Py_INCREF(f_code);
|
||||||
# endif
|
# endif
|
||||||
int lineno = PyFrame_GetLineNumber(frame);
|
int lineno = PyFrame_GetLineNumber(frame);
|
||||||
errorString += " " + handle(f_code->co_filename).cast<std::string>() + "("
|
errorString += " ";
|
||||||
+ std::to_string(lineno)
|
errorString += handle(f_code->co_filename).cast<std::string>();
|
||||||
+ "): " + handle(f_code->co_name).cast<std::string>() + "\n";
|
errorString += '(';
|
||||||
|
errorString += std::to_string(lineno);
|
||||||
|
errorString += "): ";
|
||||||
|
errorString += handle(f_code->co_name).cast<std::string>();
|
||||||
|
errorString += '\n';
|
||||||
Py_DECREF(f_code);
|
Py_DECREF(f_code);
|
||||||
# if PY_VERSION_HEX >= 0x030900B1
|
# if PY_VERSION_HEX >= 0x030900B1
|
||||||
auto *b_frame = PyFrame_GetBack(frame);
|
auto *b_frame = PyFrame_GetBack(frame);
|
||||||
|
@ -668,7 +668,7 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
|
|||||||
Type::Flags &(Eigen::RowMajor | Eigen::ColMajor),
|
Type::Flags &(Eigen::RowMajor | Eigen::ColMajor),
|
||||||
StorageIndex>(shape[0].cast<Index>(),
|
StorageIndex>(shape[0].cast<Index>(),
|
||||||
shape[1].cast<Index>(),
|
shape[1].cast<Index>(),
|
||||||
nnz,
|
std::move(nnz),
|
||||||
outerIndices.mutable_data(),
|
outerIndices.mutable_data(),
|
||||||
innerIndices.mutable_data(),
|
innerIndices.mutable_data(),
|
||||||
values.mutable_data());
|
values.mutable_data());
|
||||||
@ -686,7 +686,8 @@ struct type_caster<Type, enable_if_t<is_eigen_sparse<Type>::value>> {
|
|||||||
array outerIndices((rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr());
|
array outerIndices((rowMajor ? src.rows() : src.cols()) + 1, src.outerIndexPtr());
|
||||||
array innerIndices(src.nonZeros(), src.innerIndexPtr());
|
array innerIndices(src.nonZeros(), src.innerIndexPtr());
|
||||||
|
|
||||||
return matrix_type(std::make_tuple(data, innerIndices, outerIndices),
|
return matrix_type(std::make_tuple(
|
||||||
|
std::move(data), std::move(innerIndices), std::move(outerIndices)),
|
||||||
std::make_pair(src.rows(), src.cols()))
|
std::make_pair(src.rows(), src.cols()))
|
||||||
.release();
|
.release();
|
||||||
}
|
}
|
||||||
|
@ -940,7 +940,7 @@ protected:
|
|||||||
|
|
||||||
void fail_dim_check(ssize_t dim, const std::string &msg) const {
|
void fail_dim_check(ssize_t dim, const std::string &msg) const {
|
||||||
throw index_error(msg + ": " + std::to_string(dim) + " (ndim = " + std::to_string(ndim())
|
throw index_error(msg + ": " + std::to_string(dim) + " (ndim = " + std::to_string(ndim())
|
||||||
+ ")");
|
+ ')');
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Ix>
|
template <typename... Ix>
|
||||||
@ -1144,11 +1144,11 @@ struct format_descriptor<T, detail::enable_if_t<detail::is_pod_struct<T>::value>
|
|||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
struct format_descriptor<char[N]> {
|
struct format_descriptor<char[N]> {
|
||||||
static std::string format() { return std::to_string(N) + "s"; }
|
static std::string format() { return std::to_string(N) + 's'; }
|
||||||
};
|
};
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
struct format_descriptor<std::array<char, N>> {
|
struct format_descriptor<std::array<char, N>> {
|
||||||
static std::string format() { return std::to_string(N) + "s"; }
|
static std::string format() { return std::to_string(N) + 's'; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -1288,7 +1288,8 @@ public:
|
|||||||
static pybind11::dtype dtype() {
|
static pybind11::dtype dtype() {
|
||||||
list shape;
|
list shape;
|
||||||
array_info<T>::append_extents(shape);
|
array_info<T>::append_extents(shape);
|
||||||
return pybind11::dtype::from_args(pybind11::make_tuple(base_descr::dtype(), shape));
|
return pybind11::dtype::from_args(
|
||||||
|
pybind11::make_tuple(base_descr::dtype(), std::move(shape)));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -562,14 +562,14 @@ protected:
|
|||||||
for (auto *it = chain_start; it != nullptr; it = it->next) {
|
for (auto *it = chain_start; it != nullptr; it = it->next) {
|
||||||
if (options::show_function_signatures()) {
|
if (options::show_function_signatures()) {
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
signatures += "\n";
|
signatures += '\n';
|
||||||
}
|
}
|
||||||
if (chain) {
|
if (chain) {
|
||||||
signatures += std::to_string(++index) + ". ";
|
signatures += std::to_string(++index) + ". ";
|
||||||
}
|
}
|
||||||
signatures += rec->name;
|
signatures += rec->name;
|
||||||
signatures += it->signature;
|
signatures += it->signature;
|
||||||
signatures += "\n";
|
signatures += '\n';
|
||||||
}
|
}
|
||||||
if (it->doc && it->doc[0] != '\0' && options::show_user_defined_docstrings()) {
|
if (it->doc && it->doc[0] != '\0' && options::show_user_defined_docstrings()) {
|
||||||
// If we're appending another docstring, and aren't printing function signatures,
|
// If we're appending another docstring, and aren't printing function signatures,
|
||||||
@ -578,15 +578,15 @@ protected:
|
|||||||
if (first_user_def) {
|
if (first_user_def) {
|
||||||
first_user_def = false;
|
first_user_def = false;
|
||||||
} else {
|
} else {
|
||||||
signatures += "\n";
|
signatures += '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (options::show_function_signatures()) {
|
if (options::show_function_signatures()) {
|
||||||
signatures += "\n";
|
signatures += '\n';
|
||||||
}
|
}
|
||||||
signatures += it->doc;
|
signatures += it->doc;
|
||||||
if (options::show_function_signatures()) {
|
if (options::show_function_signatures()) {
|
||||||
signatures += "\n";
|
signatures += '\n';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1056,7 +1056,7 @@ protected:
|
|||||||
msg += it2->signature;
|
msg += it2->signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
msg += "\n";
|
msg += '\n';
|
||||||
}
|
}
|
||||||
msg += "\nInvoked with: ";
|
msg += "\nInvoked with: ";
|
||||||
auto args_ = reinterpret_borrow<tuple>(args_in);
|
auto args_ = reinterpret_borrow<tuple>(args_in);
|
||||||
|
@ -1243,8 +1243,8 @@ public:
|
|||||||
}
|
}
|
||||||
char *buffer = nullptr;
|
char *buffer = nullptr;
|
||||||
ssize_t length = 0;
|
ssize_t length = 0;
|
||||||
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length)) {
|
if (PyBytes_AsStringAndSize(temp.ptr(), &buffer, &length) != 0) {
|
||||||
pybind11_fail("Unable to extract string contents! (invalid type)");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
return std::string(buffer, (size_t) length);
|
return std::string(buffer, (size_t) length);
|
||||||
}
|
}
|
||||||
@ -1299,14 +1299,7 @@ public:
|
|||||||
explicit bytes(const pybind11::str &s);
|
explicit bytes(const pybind11::str &s);
|
||||||
|
|
||||||
// NOLINTNEXTLINE(google-explicit-constructor)
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
||||||
operator std::string() const {
|
operator std::string() const { return string_op<std::string>(); }
|
||||||
char *buffer = nullptr;
|
|
||||||
ssize_t length = 0;
|
|
||||||
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length)) {
|
|
||||||
pybind11_fail("Unable to extract bytes contents!");
|
|
||||||
}
|
|
||||||
return std::string(buffer, (size_t) length);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef PYBIND11_HAS_STRING_VIEW
|
#ifdef PYBIND11_HAS_STRING_VIEW
|
||||||
// enable_if is needed to avoid "ambiguous conversion" errors (see PR #3521).
|
// enable_if is needed to avoid "ambiguous conversion" errors (see PR #3521).
|
||||||
@ -1318,15 +1311,18 @@ public:
|
|||||||
// valid so long as the `bytes` instance remains alive and so generally should not outlive the
|
// valid so long as the `bytes` instance remains alive and so generally should not outlive the
|
||||||
// lifetime of the `bytes` instance.
|
// lifetime of the `bytes` instance.
|
||||||
// NOLINTNEXTLINE(google-explicit-constructor)
|
// NOLINTNEXTLINE(google-explicit-constructor)
|
||||||
operator std::string_view() const {
|
operator std::string_view() const { return string_op<std::string_view>(); }
|
||||||
|
#endif
|
||||||
|
private:
|
||||||
|
template <typename T>
|
||||||
|
T string_op() const {
|
||||||
char *buffer = nullptr;
|
char *buffer = nullptr;
|
||||||
ssize_t length = 0;
|
ssize_t length = 0;
|
||||||
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(m_ptr, &buffer, &length)) {
|
if (PyBytes_AsStringAndSize(m_ptr, &buffer, &length) != 0) {
|
||||||
pybind11_fail("Unable to extract bytes contents!");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
return {buffer, static_cast<size_t>(length)};
|
return {buffer, static_cast<size_t>(length)};
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
// Note: breathe >= 4.17.0 will fail to build docs if the below two constructors
|
// Note: breathe >= 4.17.0 will fail to build docs if the below two constructors
|
||||||
// are included in the doxygen group; close here and reopen after as a workaround
|
// are included in the doxygen group; close here and reopen after as a workaround
|
||||||
@ -1337,13 +1333,13 @@ inline bytes::bytes(const pybind11::str &s) {
|
|||||||
if (PyUnicode_Check(s.ptr())) {
|
if (PyUnicode_Check(s.ptr())) {
|
||||||
temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(s.ptr()));
|
temp = reinterpret_steal<object>(PyUnicode_AsUTF8String(s.ptr()));
|
||||||
if (!temp) {
|
if (!temp) {
|
||||||
pybind11_fail("Unable to extract string contents! (encoding issue)");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char *buffer = nullptr;
|
char *buffer = nullptr;
|
||||||
ssize_t length = 0;
|
ssize_t length = 0;
|
||||||
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(temp.ptr(), &buffer, &length)) {
|
if (PyBytes_AsStringAndSize(temp.ptr(), &buffer, &length) != 0) {
|
||||||
pybind11_fail("Unable to extract string contents! (invalid type)");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
auto obj = reinterpret_steal<object>(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length));
|
auto obj = reinterpret_steal<object>(PYBIND11_BYTES_FROM_STRING_AND_SIZE(buffer, length));
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
@ -1355,8 +1351,8 @@ inline bytes::bytes(const pybind11::str &s) {
|
|||||||
inline str::str(const bytes &b) {
|
inline str::str(const bytes &b) {
|
||||||
char *buffer = nullptr;
|
char *buffer = nullptr;
|
||||||
ssize_t length = 0;
|
ssize_t length = 0;
|
||||||
if (PYBIND11_BYTES_AS_STRING_AND_SIZE(b.ptr(), &buffer, &length)) {
|
if (PyBytes_AsStringAndSize(b.ptr(), &buffer, &length) != 0) {
|
||||||
pybind11_fail("Unable to extract bytes contents!");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(buffer, length));
|
auto obj = reinterpret_steal<object>(PyUnicode_FromStringAndSize(buffer, length));
|
||||||
if (!obj) {
|
if (!obj) {
|
||||||
@ -1574,7 +1570,7 @@ public:
|
|||||||
void (*destructor)(PyObject *) = nullptr)
|
void (*destructor)(PyObject *) = nullptr)
|
||||||
: object(PyCapsule_New(const_cast<void *>(value), name, destructor), stolen_t{}) {
|
: object(PyCapsule_New(const_cast<void *>(value), name, destructor), stolen_t{}) {
|
||||||
if (!m_ptr) {
|
if (!m_ptr) {
|
||||||
pybind11_fail("Could not allocate capsule object!");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1582,34 +1578,42 @@ public:
|
|||||||
capsule(const void *value, void (*destruct)(PyObject *))
|
capsule(const void *value, void (*destruct)(PyObject *))
|
||||||
: object(PyCapsule_New(const_cast<void *>(value), nullptr, destruct), stolen_t{}) {
|
: object(PyCapsule_New(const_cast<void *>(value), nullptr, destruct), stolen_t{}) {
|
||||||
if (!m_ptr) {
|
if (!m_ptr) {
|
||||||
pybind11_fail("Could not allocate capsule object!");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
capsule(const void *value, void (*destructor)(void *)) {
|
capsule(const void *value, void (*destructor)(void *)) {
|
||||||
m_ptr = PyCapsule_New(const_cast<void *>(value), nullptr, [](PyObject *o) {
|
m_ptr = PyCapsule_New(const_cast<void *>(value), nullptr, [](PyObject *o) {
|
||||||
auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o));
|
auto destructor = reinterpret_cast<void (*)(void *)>(PyCapsule_GetContext(o));
|
||||||
|
if (destructor == nullptr) {
|
||||||
|
if (PyErr_Occurred()) {
|
||||||
|
throw error_already_set();
|
||||||
|
}
|
||||||
|
pybind11_fail("Unable to get capsule context");
|
||||||
|
}
|
||||||
void *ptr = PyCapsule_GetPointer(o, nullptr);
|
void *ptr = PyCapsule_GetPointer(o, nullptr);
|
||||||
|
if (ptr == nullptr) {
|
||||||
|
throw error_already_set();
|
||||||
|
}
|
||||||
destructor(ptr);
|
destructor(ptr);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!m_ptr) {
|
if (!m_ptr || PyCapsule_SetContext(m_ptr, (void *) destructor) != 0) {
|
||||||
pybind11_fail("Could not allocate capsule object!");
|
throw error_already_set();
|
||||||
}
|
|
||||||
|
|
||||||
if (PyCapsule_SetContext(m_ptr, (void *) destructor) != 0) {
|
|
||||||
pybind11_fail("Could not set capsule context!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit capsule(void (*destructor)()) {
|
explicit capsule(void (*destructor)()) {
|
||||||
m_ptr = PyCapsule_New(reinterpret_cast<void *>(destructor), nullptr, [](PyObject *o) {
|
m_ptr = PyCapsule_New(reinterpret_cast<void *>(destructor), nullptr, [](PyObject *o) {
|
||||||
auto destructor = reinterpret_cast<void (*)()>(PyCapsule_GetPointer(o, nullptr));
|
auto destructor = reinterpret_cast<void (*)()>(PyCapsule_GetPointer(o, nullptr));
|
||||||
|
if (destructor == nullptr) {
|
||||||
|
throw error_already_set();
|
||||||
|
}
|
||||||
destructor();
|
destructor();
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!m_ptr) {
|
if (!m_ptr) {
|
||||||
pybind11_fail("Could not allocate capsule object!");
|
throw error_already_set();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1624,8 +1628,7 @@ public:
|
|||||||
const auto *name = this->name();
|
const auto *name = this->name();
|
||||||
T *result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name));
|
T *result = static_cast<T *>(PyCapsule_GetPointer(m_ptr, name));
|
||||||
if (!result) {
|
if (!result) {
|
||||||
PyErr_Clear();
|
throw error_already_set();
|
||||||
pybind11_fail("Unable to extract capsule contents!");
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -1633,8 +1636,7 @@ public:
|
|||||||
/// Replaces a capsule's pointer *without* calling the destructor on the existing one.
|
/// Replaces a capsule's pointer *without* calling the destructor on the existing one.
|
||||||
void set_pointer(const void *value) {
|
void set_pointer(const void *value) {
|
||||||
if (PyCapsule_SetPointer(m_ptr, const_cast<void *>(value)) != 0) {
|
if (PyCapsule_SetPointer(m_ptr, const_cast<void *>(value)) != 0) {
|
||||||
PyErr_Clear();
|
throw error_already_set();
|
||||||
pybind11_fail("Could not set capsule pointer");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user