mirror of
https://github.com/pybind/pybind11.git
synced 2025-03-12 07:49:28 +00:00
Merge branch 'master' into sh_merge_master
This commit is contained in:
commit
d5125eaf1e
10
.github/workflows/ci.yml
vendored
10
.github/workflows/ci.yml
vendored
@ -383,17 +383,17 @@ jobs:
|
||||
# Testing on CentOS 7 + PGI compilers, which seems to require more workarounds
|
||||
centos-nvhpc7:
|
||||
runs-on: ubuntu-latest
|
||||
name: "🐍 3 • CentOS7 / PGI 20.9 • x64"
|
||||
name: "🐍 3 • CentOS7 / PGI 22.3 • x64"
|
||||
container: centos:7
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Add Python 3 and a few requirements
|
||||
run: yum update -y && yum install -y epel-release && yum install -y git python3-devel make environment-modules cmake3
|
||||
run: yum update -y && yum install -y epel-release && yum install -y git python3-devel make environment-modules cmake3 yum-utils
|
||||
|
||||
- name: Install NVidia HPC SDK
|
||||
run: yum -y install https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-20-9-20.9-1.x86_64.rpm https://developer.download.nvidia.com/hpc-sdk/20.9/nvhpc-2020-20.9-1.x86_64.rpm
|
||||
run: yum-config-manager --add-repo https://developer.download.nvidia.com/hpc-sdk/rhel/nvhpc.repo && yum -y install nvhpc-22.3
|
||||
|
||||
# On CentOS 7, we have to filter a few tests (compiler internal error)
|
||||
# and allow deeper template recursion (not needed on CentOS 8 with a newer
|
||||
@ -403,7 +403,7 @@ jobs:
|
||||
shell: bash
|
||||
run: |
|
||||
source /etc/profile.d/modules.sh
|
||||
module load /opt/nvidia/hpc_sdk/modulefiles/nvhpc/20.9
|
||||
module load /opt/nvidia/hpc_sdk/modulefiles/nvhpc/22.3
|
||||
cmake3 -S . -B build -DDOWNLOAD_CATCH=ON \
|
||||
-DCMAKE_CXX_STANDARD=11 \
|
||||
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)") \
|
||||
@ -636,7 +636,7 @@ jobs:
|
||||
container: i386/debian:buster
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v1 # Required to run inside docker
|
||||
|
||||
- name: Install requirements
|
||||
run: |
|
||||
|
@ -73,7 +73,7 @@ repos:
|
||||
|
||||
# Autoremoves unused imports
|
||||
- repo: https://github.com/hadialqattan/pycln
|
||||
rev: "v1.3.1"
|
||||
rev: "v1.3.2"
|
||||
hooks:
|
||||
- id: pycln
|
||||
stages: [manual]
|
||||
@ -110,7 +110,7 @@ repos:
|
||||
|
||||
# PyLint has native support - not always usable, but works for us
|
||||
- repo: https://github.com/PyCQA/pylint
|
||||
rev: "v2.13.5"
|
||||
rev: "v2.13.8"
|
||||
hooks:
|
||||
- id: pylint
|
||||
files: ^pybind11
|
||||
@ -126,7 +126,7 @@ repos:
|
||||
|
||||
# Check static types with mypy
|
||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||
rev: "v0.942"
|
||||
rev: "v0.950"
|
||||
hooks:
|
||||
- id: mypy
|
||||
args: []
|
||||
|
@ -933,6 +933,14 @@ struct handle_type_name<kwargs> {
|
||||
|
||||
template <typename type>
|
||||
struct pyobject_caster {
|
||||
template <typename T = type, enable_if_t<std::is_same<T, handle>::value, int> = 0>
|
||||
pyobject_caster() : value() {}
|
||||
|
||||
// `type` may not be default constructible (e.g. frozenset, anyset). Initializing `value`
|
||||
// to a nil handle is safe since it will only be accessed if `load` succeeds.
|
||||
template <typename T = type, enable_if_t<std::is_base_of<object, T>::value, int> = 0>
|
||||
pyobject_caster() : value(reinterpret_steal<type>(handle())) {}
|
||||
|
||||
template <typename T = type, enable_if_t<std::is_same<T, handle>::value, int> = 0>
|
||||
bool load(handle src, bool /* convert */) {
|
||||
value = src;
|
||||
|
@ -1784,25 +1784,35 @@ class kwargs : public dict {
|
||||
PYBIND11_OBJECT_DEFAULT(kwargs, dict, PyDict_Check)
|
||||
};
|
||||
|
||||
class set : public object {
|
||||
class anyset : public object {
|
||||
public:
|
||||
PYBIND11_OBJECT_CVT(set, object, PySet_Check, PySet_New)
|
||||
set() : object(PySet_New(nullptr), stolen_t{}) {
|
||||
PYBIND11_OBJECT(anyset, object, PyAnySet_Check)
|
||||
size_t size() const { return static_cast<size_t>(PySet_Size(m_ptr)); }
|
||||
bool empty() const { return size() == 0; }
|
||||
template <typename T>
|
||||
bool contains(T &&val) const {
|
||||
return PySet_Contains(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 1;
|
||||
}
|
||||
};
|
||||
|
||||
class set : public anyset {
|
||||
public:
|
||||
PYBIND11_OBJECT_CVT(set, anyset, PySet_Check, PySet_New)
|
||||
set() : anyset(PySet_New(nullptr), stolen_t{}) {
|
||||
if (!m_ptr) {
|
||||
pybind11_fail("Could not allocate set object!");
|
||||
}
|
||||
}
|
||||
size_t size() const { return (size_t) PySet_Size(m_ptr); }
|
||||
bool empty() const { return size() == 0; }
|
||||
template <typename T>
|
||||
bool add(T &&val) /* py-non-const */ {
|
||||
return PySet_Add(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 0;
|
||||
}
|
||||
void clear() /* py-non-const */ { PySet_Clear(m_ptr); }
|
||||
template <typename T>
|
||||
bool contains(T &&val) const {
|
||||
return PySet_Contains(m_ptr, detail::object_or_cast(std::forward<T>(val)).ptr()) == 1;
|
||||
}
|
||||
};
|
||||
|
||||
class frozenset : public anyset {
|
||||
public:
|
||||
PYBIND11_OBJECT_CVT(frozenset, anyset, PyFrozenSet_Check, PyFrozenSet_New)
|
||||
};
|
||||
|
||||
class function : public object {
|
||||
|
@ -55,10 +55,10 @@ struct set_caster {
|
||||
using key_conv = make_caster<Key>;
|
||||
|
||||
bool load(handle src, bool convert) {
|
||||
if (!isinstance<pybind11::set>(src)) {
|
||||
if (!isinstance<anyset>(src)) {
|
||||
return false;
|
||||
}
|
||||
auto s = reinterpret_borrow<pybind11::set>(src);
|
||||
auto s = reinterpret_borrow<anyset>(src);
|
||||
value.clear();
|
||||
for (auto entry : s) {
|
||||
key_conv conv;
|
||||
|
@ -75,7 +75,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
m.def("get_none", [] { return py::none(); });
|
||||
m.def("print_none", [](const py::none &none) { py::print("none: {}"_s.format(none)); });
|
||||
|
||||
// test_set
|
||||
// test_set, test_frozenset
|
||||
m.def("get_set", []() {
|
||||
py::set set;
|
||||
set.add(py::str("key1"));
|
||||
@ -83,14 +83,26 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
set.add(std::string("key3"));
|
||||
return set;
|
||||
});
|
||||
m.def("print_set", [](const py::set &set) {
|
||||
m.def("get_frozenset", []() {
|
||||
py::set set;
|
||||
set.add(py::str("key1"));
|
||||
set.add("key2");
|
||||
set.add(std::string("key3"));
|
||||
return py::frozenset(set);
|
||||
});
|
||||
m.def("print_anyset", [](const py::anyset &set) {
|
||||
for (auto item : set) {
|
||||
py::print("key:", item);
|
||||
}
|
||||
});
|
||||
m.def("set_contains",
|
||||
[](const py::set &set, const py::object &key) { return set.contains(key); });
|
||||
m.def("set_contains", [](const py::set &set, const char *key) { return set.contains(key); });
|
||||
m.def("anyset_size", [](const py::anyset &set) { return set.size(); });
|
||||
m.def("anyset_empty", [](const py::anyset &set) { return set.empty(); });
|
||||
m.def("anyset_contains",
|
||||
[](const py::anyset &set, const py::object &key) { return set.contains(key); });
|
||||
m.def("anyset_contains",
|
||||
[](const py::anyset &set, const char *key) { return set.contains(key); });
|
||||
m.def("set_add", [](py::set &set, const py::object &key) { set.add(key); });
|
||||
m.def("set_clear", [](py::set &set) { set.clear(); });
|
||||
|
||||
// test_dict
|
||||
m.def("get_dict", []() { return py::dict("key"_a = "value"); });
|
||||
@ -310,6 +322,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
"list"_a = py::list(d["list"]),
|
||||
"dict"_a = py::dict(d["dict"]),
|
||||
"set"_a = py::set(d["set"]),
|
||||
"frozenset"_a = py::frozenset(d["frozenset"]),
|
||||
"memoryview"_a = py::memoryview(d["memoryview"]));
|
||||
});
|
||||
|
||||
@ -325,6 +338,7 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
"list"_a = d["list"].cast<py::list>(),
|
||||
"dict"_a = d["dict"].cast<py::dict>(),
|
||||
"set"_a = d["set"].cast<py::set>(),
|
||||
"frozenset"_a = d["frozenset"].cast<py::frozenset>(),
|
||||
"memoryview"_a = d["memoryview"].cast<py::memoryview>());
|
||||
});
|
||||
|
||||
|
@ -66,11 +66,12 @@ def test_none(capture, doc):
|
||||
|
||||
def test_set(capture, doc):
|
||||
s = m.get_set()
|
||||
assert isinstance(s, set)
|
||||
assert s == {"key1", "key2", "key3"}
|
||||
|
||||
s.add("key4")
|
||||
with capture:
|
||||
s.add("key4")
|
||||
m.print_set(s)
|
||||
m.print_anyset(s)
|
||||
assert (
|
||||
capture.unordered
|
||||
== """
|
||||
@ -81,12 +82,43 @@ def test_set(capture, doc):
|
||||
"""
|
||||
)
|
||||
|
||||
assert not m.set_contains(set(), 42)
|
||||
assert m.set_contains({42}, 42)
|
||||
assert m.set_contains({"foo"}, "foo")
|
||||
m.set_add(s, "key5")
|
||||
assert m.anyset_size(s) == 5
|
||||
|
||||
assert doc(m.get_list) == "get_list() -> list"
|
||||
assert doc(m.print_list) == "print_list(arg0: list) -> None"
|
||||
m.set_clear(s)
|
||||
assert m.anyset_empty(s)
|
||||
|
||||
assert not m.anyset_contains(set(), 42)
|
||||
assert m.anyset_contains({42}, 42)
|
||||
assert m.anyset_contains({"foo"}, "foo")
|
||||
|
||||
assert doc(m.get_set) == "get_set() -> set"
|
||||
assert doc(m.print_anyset) == "print_anyset(arg0: anyset) -> None"
|
||||
|
||||
|
||||
def test_frozenset(capture, doc):
|
||||
s = m.get_frozenset()
|
||||
assert isinstance(s, frozenset)
|
||||
assert s == frozenset({"key1", "key2", "key3"})
|
||||
|
||||
with capture:
|
||||
m.print_anyset(s)
|
||||
assert (
|
||||
capture.unordered
|
||||
== """
|
||||
key: key1
|
||||
key: key2
|
||||
key: key3
|
||||
"""
|
||||
)
|
||||
assert m.anyset_size(s) == 3
|
||||
assert not m.anyset_empty(s)
|
||||
|
||||
assert not m.anyset_contains(frozenset(), 42)
|
||||
assert m.anyset_contains(frozenset({42}), 42)
|
||||
assert m.anyset_contains(frozenset({"foo"}), "foo")
|
||||
|
||||
assert doc(m.get_frozenset) == "get_frozenset() -> frozenset"
|
||||
|
||||
|
||||
def test_dict(capture, doc):
|
||||
@ -302,6 +334,7 @@ def test_constructors():
|
||||
list: range(3),
|
||||
dict: [("two", 2), ("one", 1), ("three", 3)],
|
||||
set: [4, 4, 5, 6, 6, 6],
|
||||
frozenset: [4, 4, 5, 6, 6, 6],
|
||||
memoryview: b"abc",
|
||||
}
|
||||
inputs = {k.__name__: v for k, v in data.items()}
|
||||
|
@ -73,6 +73,7 @@ def test_set(doc):
|
||||
assert s == {"key1", "key2"}
|
||||
s.add("key3")
|
||||
assert m.load_set(s)
|
||||
assert m.load_set(frozenset(s))
|
||||
|
||||
assert doc(m.cast_set) == "cast_set() -> Set[str]"
|
||||
assert doc(m.load_set) == "load_set(arg0: Set[str]) -> bool"
|
||||
|
@ -127,10 +127,11 @@ if USE_SYSCONFIG:
|
||||
scheme = 'posix_prefix'
|
||||
print(s.get_path('platinclude', scheme))
|
||||
print(s.get_path('platlib'))
|
||||
print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))
|
||||
else:
|
||||
print(ds.get_python_inc(plat_specific=True));
|
||||
print(ds.get_python_lib(plat_specific=True));
|
||||
print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'));
|
||||
print(ds.get_config_var('EXT_SUFFIX') or ds.get_config_var('SO'));
|
||||
print(hasattr(sys, 'gettotalrefcount')+0);
|
||||
print(struct.calcsize('@P'));
|
||||
print(s.get_config_var('LDVERSION') or s.get_config_var('VERSION'));
|
||||
|
Loading…
Reference in New Issue
Block a user