Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[smart_holder] git merge master #3924

Merged
merged 7 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)") \
Expand Down Expand Up @@ -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: |
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/ci_sh_def.yml
Original file line number Diff line number Diff line change
Expand Up @@ -401,17 +401,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
Expand All @@ -421,7 +421,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)") \
Expand Down Expand Up @@ -658,7 +658,7 @@ jobs:
container: i386/debian:buster

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v1 # Required to run inside docker

- name: Install requirements
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/ci_sh_def.yml.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- ci.yml 2022-05-02 12:41:48.573584316 -0700
+++ ci_sh_def.yml 2022-05-02 12:42:27.140839307 -0700
--- ci.yml 2022-05-05 16:12:58.585658042 -0700
+++ ci_sh_def.yml 2022-05-05 16:13:29.421627917 -0700
@@ -1,4 +1,16 @@
-name: CI
+# PLEASE KEEP THIS GROUP OF FILES IN SYNC AT ALL TIMES:
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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: []
Expand Down
8 changes: 8 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions include/pybind11/detail/smart_holder_type_casters.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,10 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
if (copy_constructor) {
valueptr = copy_constructor(src);
} else {
#if defined(NDEBUG)
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
throw cast_error("return_value_policy = copy, but type is "
"non-copyable! (compile in debug mode for details)");
"non-copyable! (#define PYBIND11_DETAILED_ERROR_MESSAGES or "
"compile in debug mode for details)");
#else
std::string type_name(tinfo->cpptype->name());
detail::clean_type_id(type_name);
Expand All @@ -741,10 +742,11 @@ struct smart_holder_type_caster : smart_holder_type_caster_load<T>,
} else if (copy_constructor) {
valueptr = copy_constructor(src);
} else {
#if defined(NDEBUG)
#if !defined(PYBIND11_DETAILED_ERROR_MESSAGES)
throw cast_error("return_value_policy = move, but type is neither "
"movable nor copyable! "
"(compile in debug mode for details)");
"(#define PYBIND11_DETAILED_ERROR_MESSAGES or compile in "
"debug mode for details)");
#else
std::string type_name(tinfo->cpptype->name());
detail::clean_type_id(type_name);
Expand Down
28 changes: 19 additions & 9 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 19 additions & 5 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,34 @@ 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"));
set.add("key2");
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"); });
Expand Down Expand Up @@ -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"]));
});

Expand All @@ -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>());
});

Expand Down
47 changes: 40 additions & 7 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
== """
Expand All @@ -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):
Expand Down Expand Up @@ -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()}
Expand Down
1 change: 1 addition & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion tools/FindPythonLibsNew.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down