amulet-pybind11-extensions 1.0.0a0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- amulet_pybind11_extensions-1.0.0a0/LICENSE +21 -0
- amulet_pybind11_extensions-1.0.0a0/MANIFEST.in +3 -0
- amulet_pybind11_extensions-1.0.0a0/PKG-INFO +28 -0
- amulet_pybind11_extensions-1.0.0a0/README.md +7 -0
- amulet_pybind11_extensions-1.0.0a0/pyproject.toml +54 -0
- amulet_pybind11_extensions-1.0.0a0/setup.cfg +4 -0
- amulet_pybind11_extensions-1.0.0a0/setup.py +8 -0
- amulet_pybind11_extensions-1.0.0a0/src/amulet_pybind11_extensions.egg-info/PKG-INFO +28 -0
- amulet_pybind11_extensions-1.0.0a0/src/amulet_pybind11_extensions.egg-info/SOURCES.txt +21 -0
- amulet_pybind11_extensions-1.0.0a0/src/amulet_pybind11_extensions.egg-info/dependency_links.txt +1 -0
- amulet_pybind11_extensions-1.0.0a0/src/amulet_pybind11_extensions.egg-info/requires.txt +7 -0
- amulet_pybind11_extensions-1.0.0a0/src/amulet_pybind11_extensions.egg-info/top_level.txt +1 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/__init__.py +3 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/_version.py +21 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/builtins.hpp +72 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/collections.hpp +209 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/compatibility.hpp +53 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/contextlib.hpp +69 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/numpy.hpp +29 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/py_module.hpp +40 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/pybind11.hpp +34 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/types.hpp +13 -0
- amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/pybind11_extensionsConfig.cmake +8 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 gentlegiantJGC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: amulet-pybind11-extensions
|
|
3
|
+
Version: 1.0.0a0
|
|
4
|
+
Summary: An extension library for pybind11.
|
|
5
|
+
Author: James Clare
|
|
6
|
+
Project-URL: Homepage, https://www.amuletmc.com
|
|
7
|
+
Project-URL: Repository, https://github.com/Amulet-Team/Amulet-pybind11-extensions
|
|
8
|
+
Project-URL: Issues, https://github.com/Amulet-Team/Amulet-pybind11-extensions
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.11
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: setuptools>=42; extra == "dev"
|
|
16
|
+
Requires-Dist: versioneer; extra == "dev"
|
|
17
|
+
Requires-Dist: wheel; extra == "dev"
|
|
18
|
+
Requires-Dist: pybind11[global]==2.13.6; extra == "dev"
|
|
19
|
+
Requires-Dist: black>=22.3; extra == "dev"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# Amulet-pybind11-extensions
|
|
23
|
+
|
|
24
|
+
This library is an extension to pybind11 to fix some simple issues and add some useful extension classes.
|
|
25
|
+
|
|
26
|
+
This code is subject to change.
|
|
27
|
+
|
|
28
|
+
If you want to use it, target a specific commit so that changes don't break your code.
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = [
|
|
3
|
+
"setuptools >= 42",
|
|
4
|
+
"wheel",
|
|
5
|
+
"versioneer",
|
|
6
|
+
]
|
|
7
|
+
build-backend = "setuptools.build_meta"
|
|
8
|
+
|
|
9
|
+
[project]
|
|
10
|
+
name = "amulet-pybind11-extensions"
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "James Clare"},
|
|
13
|
+
]
|
|
14
|
+
description = "An extension library for pybind11."
|
|
15
|
+
dynamic = ["version", "readme"]
|
|
16
|
+
requires-python = ">=3.11"
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.optional-dependencies]
|
|
23
|
+
dev = [
|
|
24
|
+
"setuptools>=42",
|
|
25
|
+
"versioneer",
|
|
26
|
+
"wheel",
|
|
27
|
+
"pybind11[global]==2.13.6",
|
|
28
|
+
"black>=22.3",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Homepage = "https://www.amuletmc.com"
|
|
33
|
+
Repository = "https://github.com/Amulet-Team/Amulet-pybind11-extensions"
|
|
34
|
+
Issues = "https://github.com/Amulet-Team/Amulet-pybind11-extensions"
|
|
35
|
+
|
|
36
|
+
[tool.setuptools]
|
|
37
|
+
include-package-data = false
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.package-data]
|
|
40
|
+
"*" = [
|
|
41
|
+
"*Config.cmake",
|
|
42
|
+
"**/*.hpp",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.setuptools.dynamic]
|
|
46
|
+
readme = {file = ["README.md"], content-type = "text/markdown"}
|
|
47
|
+
|
|
48
|
+
[tool.versioneer]
|
|
49
|
+
VCS = "git"
|
|
50
|
+
style = "pep440"
|
|
51
|
+
versionfile_source = "src/pybind11_extensions/_version.py"
|
|
52
|
+
versionfile_build = "pybind11_extensions/_version.py"
|
|
53
|
+
tag_prefix = ""
|
|
54
|
+
parentdir_prefix = "pybind11_extensions-"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: amulet-pybind11-extensions
|
|
3
|
+
Version: 1.0.0a0
|
|
4
|
+
Summary: An extension library for pybind11.
|
|
5
|
+
Author: James Clare
|
|
6
|
+
Project-URL: Homepage, https://www.amuletmc.com
|
|
7
|
+
Project-URL: Repository, https://github.com/Amulet-Team/Amulet-pybind11-extensions
|
|
8
|
+
Project-URL: Issues, https://github.com/Amulet-Team/Amulet-pybind11-extensions
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Requires-Python: >=3.11
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: setuptools>=42; extra == "dev"
|
|
16
|
+
Requires-Dist: versioneer; extra == "dev"
|
|
17
|
+
Requires-Dist: wheel; extra == "dev"
|
|
18
|
+
Requires-Dist: pybind11[global]==2.13.6; extra == "dev"
|
|
19
|
+
Requires-Dist: black>=22.3; extra == "dev"
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# Amulet-pybind11-extensions
|
|
23
|
+
|
|
24
|
+
This library is an extension to pybind11 to fix some simple issues and add some useful extension classes.
|
|
25
|
+
|
|
26
|
+
This code is subject to change.
|
|
27
|
+
|
|
28
|
+
If you want to use it, target a specific commit so that changes don't break your code.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.py
|
|
6
|
+
src/amulet_pybind11_extensions.egg-info/PKG-INFO
|
|
7
|
+
src/amulet_pybind11_extensions.egg-info/SOURCES.txt
|
|
8
|
+
src/amulet_pybind11_extensions.egg-info/dependency_links.txt
|
|
9
|
+
src/amulet_pybind11_extensions.egg-info/requires.txt
|
|
10
|
+
src/amulet_pybind11_extensions.egg-info/top_level.txt
|
|
11
|
+
src/pybind11_extensions/__init__.py
|
|
12
|
+
src/pybind11_extensions/_version.py
|
|
13
|
+
src/pybind11_extensions/pybind11_extensionsConfig.cmake
|
|
14
|
+
src/pybind11_extensions/include/pybind11_extensions/builtins.hpp
|
|
15
|
+
src/pybind11_extensions/include/pybind11_extensions/collections.hpp
|
|
16
|
+
src/pybind11_extensions/include/pybind11_extensions/compatibility.hpp
|
|
17
|
+
src/pybind11_extensions/include/pybind11_extensions/contextlib.hpp
|
|
18
|
+
src/pybind11_extensions/include/pybind11_extensions/numpy.hpp
|
|
19
|
+
src/pybind11_extensions/include/pybind11_extensions/py_module.hpp
|
|
20
|
+
src/pybind11_extensions/include/pybind11_extensions/pybind11.hpp
|
|
21
|
+
src/pybind11_extensions/include/pybind11_extensions/types.hpp
|
amulet_pybind11_extensions-1.0.0a0/src/amulet_pybind11_extensions.egg-info/dependency_links.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pybind11_extensions
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
# This file was generated by 'versioneer.py' (0.29) from
|
|
3
|
+
# revision-control system data, or from the parent directory name of an
|
|
4
|
+
# unpacked source archive. Distribution tarballs contain a pre-generated copy
|
|
5
|
+
# of this file.
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
|
|
9
|
+
version_json = '''
|
|
10
|
+
{
|
|
11
|
+
"date": "2025-04-14T09:19:50+0100",
|
|
12
|
+
"dirty": false,
|
|
13
|
+
"error": null,
|
|
14
|
+
"full-revisionid": "67aea294a9f9ed7de7b9ee82bcc7dc78fb885d50",
|
|
15
|
+
"version": "1.0.0a0"
|
|
16
|
+
}
|
|
17
|
+
''' # END VERSION_JSON
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_versions():
|
|
21
|
+
return json.loads(version_json)
|
amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/builtins.hpp
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <pybind11/pybind11.h>
|
|
3
|
+
#include <pybind11/detail/descr.h>
|
|
4
|
+
|
|
5
|
+
// Type hints for builtin types.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
namespace pybind11_extensions {
|
|
9
|
+
// Type hint for a native python object.
|
|
10
|
+
namespace detail {
|
|
11
|
+
template<size_t N>
|
|
12
|
+
struct FixedString {
|
|
13
|
+
char buf[N + 1]{};
|
|
14
|
+
constexpr FixedString(char const* s) {
|
|
15
|
+
for (unsigned i = 0; i != N; ++i) buf[i] = s[i];
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
template<unsigned N>
|
|
19
|
+
FixedString(char const (&)[N])->FixedString<N - 1>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// A python object with a user defined string type hint for an arbitrary python object.
|
|
23
|
+
// This is useful if a C++ function needs to interact with a native python object.
|
|
24
|
+
template <detail::FixedString T>
|
|
25
|
+
class PyObjectStr : public pybind11::object {
|
|
26
|
+
PYBIND11_OBJECT_DEFAULT(PyObjectStr, object, PyObject_Type)
|
|
27
|
+
using object::object;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// A python object with a type hint from a C++ object.
|
|
31
|
+
// This is useful if you don't want to cast the python object to the C++ object and want to keep the type hint.
|
|
32
|
+
template <typename cppT>
|
|
33
|
+
class PyObjectCpp : public pybind11::object {
|
|
34
|
+
PYBIND11_OBJECT_DEFAULT(PyObjectCpp, object, PyObject_Type)
|
|
35
|
+
using object::object;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
template <typename T>
|
|
39
|
+
class Args : public pybind11::args {
|
|
40
|
+
using args::args;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
template <typename T>
|
|
44
|
+
class KWArgs : public pybind11::kwargs {
|
|
45
|
+
using kwargs::kwargs;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
namespace pybind11 {
|
|
51
|
+
namespace detail {
|
|
52
|
+
template <pybind11_extensions::detail::FixedString T>
|
|
53
|
+
struct handle_type_name<pybind11_extensions::PyObjectStr<T>> {
|
|
54
|
+
static constexpr auto name = pybind11::detail::const_name(T.buf);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
template <typename cppT>
|
|
58
|
+
struct handle_type_name<pybind11_extensions::PyObjectCpp<cppT>> {
|
|
59
|
+
static constexpr auto name = make_caster<cppT>::name;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
template <typename T>
|
|
63
|
+
struct handle_type_name<pybind11_extensions::Args<T>> {
|
|
64
|
+
static constexpr auto name = const_name("*args: ") + make_caster<T>::name;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
template <typename T>
|
|
68
|
+
struct handle_type_name<pybind11_extensions::KWArgs<T>> {
|
|
69
|
+
static constexpr auto name = const_name("**kwargs: ") + make_caster<T>::name;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <iterator>
|
|
3
|
+
#include <ranges>
|
|
4
|
+
|
|
5
|
+
#include <pybind11/pybind11.h>
|
|
6
|
+
|
|
7
|
+
// I have found cases where I want to accept or return a python object matching a collections.abc class.
|
|
8
|
+
// This extension adds subclasses of py::object with type hints for the collections.abc classes.
|
|
9
|
+
// This allows C++ functions to accept or return python objects that match the collection.abc classes.
|
|
10
|
+
// Note that these are handled in the same way as py::object thus there is no type validation.
|
|
11
|
+
|
|
12
|
+
namespace pybind11_extensions {
|
|
13
|
+
|
|
14
|
+
template <typename T>
|
|
15
|
+
class Iterator : public pybind11::object {
|
|
16
|
+
public:
|
|
17
|
+
using iterator_category = std::input_iterator_tag;
|
|
18
|
+
using difference_type = pybind11::ssize_t;
|
|
19
|
+
using value_type = T;
|
|
20
|
+
|
|
21
|
+
PYBIND11_OBJECT_DEFAULT(Iterator, object, PyIter_Check)
|
|
22
|
+
|
|
23
|
+
Iterator& operator++()
|
|
24
|
+
{
|
|
25
|
+
advance();
|
|
26
|
+
return *this;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
Iterator operator++(int)
|
|
30
|
+
{
|
|
31
|
+
auto rv = *this;
|
|
32
|
+
advance();
|
|
33
|
+
return rv;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
T operator*() const
|
|
37
|
+
{
|
|
38
|
+
return obj().template cast<T>();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
std::unique_ptr<T> operator->() const
|
|
42
|
+
{
|
|
43
|
+
return std::make_unique<T>(obj().template cast<T>());
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static Iterator sentinel() { return {}; }
|
|
47
|
+
|
|
48
|
+
friend bool operator==(const Iterator& a, const Iterator& b) { return a.obj().ptr() == b.obj().ptr(); }
|
|
49
|
+
friend bool operator!=(const Iterator& a, const Iterator& b) { return a.obj().ptr() != b.obj().ptr(); }
|
|
50
|
+
|
|
51
|
+
private:
|
|
52
|
+
void advance()
|
|
53
|
+
{
|
|
54
|
+
value = pybind11::reinterpret_steal<pybind11::object>(PyIter_Next(m_ptr));
|
|
55
|
+
if (value.ptr() == nullptr && PyErr_Occurred()) {
|
|
56
|
+
throw pybind11::error_already_set();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const pybind11::object& obj() const
|
|
61
|
+
{
|
|
62
|
+
if (m_ptr && !value.ptr()) {
|
|
63
|
+
auto& self = const_cast<Iterator&>(*this);
|
|
64
|
+
self.advance();
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private:
|
|
70
|
+
pybind11::object value = {};
|
|
71
|
+
};
|
|
72
|
+
static_assert(std::input_iterator<Iterator<int>>);
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
template <typename T>
|
|
76
|
+
class Iterable : public pybind11::object {
|
|
77
|
+
PYBIND11_OBJECT_DEFAULT(Iterable, object, pybind11::detail::PyIterable_Check)
|
|
78
|
+
|
|
79
|
+
Iterator<T> begin() const
|
|
80
|
+
{
|
|
81
|
+
return Iterator<T>(pybind11::object::begin());
|
|
82
|
+
}
|
|
83
|
+
Iterator<T> end() const
|
|
84
|
+
{
|
|
85
|
+
return Iterator<T>(pybind11::object::end());
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
};
|
|
89
|
+
static_assert(std::ranges::input_range<Iterable<int>>);
|
|
90
|
+
static_assert(std::convertible_to<std::ranges::range_value_t<Iterable<int>>, const int&>);
|
|
91
|
+
|
|
92
|
+
namespace collections {
|
|
93
|
+
namespace abc {
|
|
94
|
+
template <typename T>
|
|
95
|
+
class Iterator : public pybind11::object {
|
|
96
|
+
PYBIND11_OBJECT_DEFAULT(Iterator, object, PyObject_Type)
|
|
97
|
+
using object::object;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
template <typename T>
|
|
101
|
+
class Iterable : public pybind11::object {
|
|
102
|
+
PYBIND11_OBJECT_DEFAULT(Iterable, object, PyObject_Type)
|
|
103
|
+
using object::object;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
template <typename T>
|
|
107
|
+
class Sequence : public pybind11::object {
|
|
108
|
+
PYBIND11_OBJECT_DEFAULT(Sequence, object, PyObject_Type)
|
|
109
|
+
using object::object;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
template <typename K, typename V>
|
|
113
|
+
class Mapping : public pybind11::object {
|
|
114
|
+
PYBIND11_OBJECT_DEFAULT(Mapping, object, PyObject_Type)
|
|
115
|
+
using object::object;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
template <typename K, typename V>
|
|
119
|
+
class MutableMapping : public pybind11::object {
|
|
120
|
+
PYBIND11_OBJECT_DEFAULT(MutableMapping, object, PyObject_Type)
|
|
121
|
+
using object::object;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
template <typename K>
|
|
125
|
+
class KeysView : public pybind11::object {
|
|
126
|
+
PYBIND11_OBJECT_DEFAULT(KeysView, object, PyObject_Type)
|
|
127
|
+
using object::object;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
template <typename V>
|
|
131
|
+
class ValuesView : public pybind11::object {
|
|
132
|
+
PYBIND11_OBJECT_DEFAULT(ValuesView, object, PyObject_Type)
|
|
133
|
+
using object::object;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
template <typename K, typename V>
|
|
137
|
+
class ItemsView : public pybind11::object {
|
|
138
|
+
PYBIND11_OBJECT_DEFAULT(ItemsView, object, PyObject_Type)
|
|
139
|
+
using object::object;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
namespace pybind11 {
|
|
146
|
+
namespace detail {
|
|
147
|
+
template <typename T>
|
|
148
|
+
struct handle_type_name<pybind11_extensions::collections::abc::Iterator<T>> {
|
|
149
|
+
static constexpr auto name = const_name("collections.abc.Iterator[") + make_caster<T>::name + const_name("]");
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
template <>
|
|
153
|
+
struct handle_type_name<pybind11_extensions::Iterator<pybind11::object>> {
|
|
154
|
+
static constexpr auto name = const_name("collections.abc.Iterator");
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
template <typename T>
|
|
158
|
+
struct handle_type_name<pybind11_extensions::Iterator<T>> {
|
|
159
|
+
static constexpr auto name = const_name("collections.abc.Iterator[") + make_caster<T>::name + const_name("]");
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
template <typename T>
|
|
163
|
+
struct handle_type_name<pybind11_extensions::collections::abc::Iterable<T>> {
|
|
164
|
+
static constexpr auto name = const_name("collections.abc.Iterable[") + make_caster<T>::name + const_name("]");
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
template <>
|
|
168
|
+
struct handle_type_name<pybind11_extensions::Iterable<pybind11::object>> {
|
|
169
|
+
static constexpr auto name = const_name("collections.abc.Iterable");
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
template <typename T>
|
|
173
|
+
struct handle_type_name<pybind11_extensions::Iterable<T>> {
|
|
174
|
+
static constexpr auto name = const_name("collections.abc.Iterable[") + make_caster<T>::name + const_name("]");
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
template <typename T>
|
|
178
|
+
struct handle_type_name<pybind11_extensions::collections::abc::Sequence<T>> {
|
|
179
|
+
static constexpr auto name = const_name("collections.abc.Sequence[") + make_caster<T>::name + const_name("]");
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
template <typename K, typename V>
|
|
183
|
+
struct handle_type_name<pybind11_extensions::collections::abc::Mapping<K, V>> {
|
|
184
|
+
static constexpr auto name = const_name("collections.abc.Mapping[") + make_caster<K>::name + const_name(", ") + make_caster<V>::name + const_name("]");
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
template <typename K, typename V>
|
|
188
|
+
struct handle_type_name<pybind11_extensions::collections::abc::MutableMapping<K, V>> {
|
|
189
|
+
static constexpr auto name = const_name("collections.abc.MutableMapping[") + make_caster<K>::name + const_name(", ")
|
|
190
|
+
+ make_caster<V>::name + const_name("]");
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
template <typename K>
|
|
194
|
+
struct handle_type_name<pybind11_extensions::collections::abc::KeysView<K>> {
|
|
195
|
+
static constexpr auto name = const_name("collections.abc.KeysView[") + make_caster<K>::name + const_name("]");
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
template <typename V>
|
|
199
|
+
struct handle_type_name<pybind11_extensions::collections::abc::ValuesView<V>> {
|
|
200
|
+
static constexpr auto name = const_name("collections.abc.ValuesView[") + make_caster<V>::name + const_name("]");
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
template <typename K, typename V>
|
|
204
|
+
struct handle_type_name<pybind11_extensions::collections::abc::ItemsView<K, V>> {
|
|
205
|
+
static constexpr auto name = const_name("collections.abc.ItemsView[") + make_caster<K>::name + const_name(", ")
|
|
206
|
+
+ make_caster<V>::name + const_name("]");
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <filesystem>
|
|
4
|
+
#include <string>
|
|
5
|
+
|
|
6
|
+
#include <pybind11/pybind11.h>
|
|
7
|
+
|
|
8
|
+
static std::string get_version_top(std::string version)
|
|
9
|
+
{
|
|
10
|
+
return version.substr(0, version.find('.', version.find('.') + 1));
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
namespace pybind11_extensions {
|
|
14
|
+
|
|
15
|
+
void init_compiler_config(pybind11::module m)
|
|
16
|
+
{
|
|
17
|
+
pybind11::dict compiler_config;
|
|
18
|
+
compiler_config["pybind11_version"] = PYBIND11_VERSION;
|
|
19
|
+
compiler_config["compiler_id"] = COMPILER_ID;
|
|
20
|
+
compiler_config["compiler_version"] = COMPILER_VERSION;
|
|
21
|
+
m.attr("compiler_config") = compiler_config;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
void check_compatibility(pybind11::module a, pybind11::module b)
|
|
25
|
+
{
|
|
26
|
+
std::string a_name = a.attr("__name__").cast<std::string>();
|
|
27
|
+
std::string b_name = b.attr("__name__").cast<std::string>();
|
|
28
|
+
pybind11::dict a_config = a.attr("compiler_config");
|
|
29
|
+
pybind11::dict b_config = b.attr("compiler_config");
|
|
30
|
+
|
|
31
|
+
std::string a_pybind11_version = a_config["pybind11_version"].cast<std::string>();
|
|
32
|
+
std::string b_pybind11_version = b_config["pybind11_version"].cast<std::string>();
|
|
33
|
+
if (a_pybind11_version != b_pybind11_version) {
|
|
34
|
+
throw std::runtime_error(
|
|
35
|
+
"pybind11 version mismatch. " + a_name + " is compiled for pybind11==" + a_pybind11_version + " and " + b_name + " is compiled for pybind11==" + b_pybind11_version);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
std::string a_compiler_id = a_config["compiler_id"].cast<std::string>();
|
|
39
|
+
std::string b_compiler_id = b_config["compiler_id"].cast<std::string>();
|
|
40
|
+
if (a_compiler_id != b_compiler_id) {
|
|
41
|
+
throw std::runtime_error(
|
|
42
|
+
"compiler mismatch. " + a_name + " is compiled by " + a_compiler_id + " and " + b_name + " is compiled by " + b_compiler_id);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
std::string a_compiler_version = a_config["compiler_version"].cast<std::string>();
|
|
46
|
+
std::string b_compiler_version = b_config["compiler_version"].cast<std::string>();
|
|
47
|
+
if (get_version_top(a_compiler_version) != get_version_top(b_compiler_version)) {
|
|
48
|
+
throw std::runtime_error(
|
|
49
|
+
"compiler version mismatch. " + a_name + " is compiled by " + a_compiler_id + " " + a_compiler_version + " and " + b_name + " is compiled by " + b_compiler_id + " " + b_compiler_version);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <pybind11/pybind11.h>
|
|
4
|
+
#include <pybind11/stl.h>
|
|
5
|
+
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <optional>
|
|
8
|
+
#include <type_traits>
|
|
9
|
+
|
|
10
|
+
namespace pybind11_extensions {
|
|
11
|
+
|
|
12
|
+
namespace contextlib {
|
|
13
|
+
template <typename T, typename ExitT = std::optional<bool>>
|
|
14
|
+
class ContextManager : public pybind11::object {
|
|
15
|
+
PYBIND11_OBJECT_DEFAULT(ContextManager, object, PyObject_Type)
|
|
16
|
+
using object::object;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
namespace detail {
|
|
20
|
+
template <typename T, typename ExitT = std::optional<bool>>
|
|
21
|
+
class ContextManager {
|
|
22
|
+
public:
|
|
23
|
+
std::function<T()> enter;
|
|
24
|
+
std::function<ExitT(pybind11::object, pybind11::object, pybind11::object)> exit;
|
|
25
|
+
ContextManager(
|
|
26
|
+
std::function<T()> enter,
|
|
27
|
+
std::function<ExitT(pybind11::object, pybind11::object, pybind11::object)> exit)
|
|
28
|
+
: enter(enter)
|
|
29
|
+
, exit(exit)
|
|
30
|
+
{
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
template <typename T, typename ExitT = std::optional<bool>>
|
|
36
|
+
auto make_context_manager(
|
|
37
|
+
std::function<T()> enter,
|
|
38
|
+
std::function<ExitT(pybind11::object, pybind11::object, pybind11::object)> exit) -> ContextManager<T, ExitT>
|
|
39
|
+
{
|
|
40
|
+
using ContextManagerT = detail::ContextManager<T, ExitT>;
|
|
41
|
+
if (!pybind11::detail::get_type_info(typeid(ContextManagerT), false)) {
|
|
42
|
+
pybind11::class_<ContextManagerT>(pybind11::handle(), "ContextManager", pybind11::module_local())
|
|
43
|
+
.def(
|
|
44
|
+
"__enter__",
|
|
45
|
+
[](const ContextManagerT& self) -> T {
|
|
46
|
+
return self.enter();
|
|
47
|
+
})
|
|
48
|
+
.def(
|
|
49
|
+
"__exit__",
|
|
50
|
+
[](const ContextManagerT& self, pybind11::object exc_type, pybind11::object exc_val, pybind11::object exc_tb) -> ExitT {
|
|
51
|
+
return self.exit(exc_type, exc_val, exc_tb);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return pybind11::cast(ContextManagerT(enter, exit));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
namespace pybind11 {
|
|
61
|
+
namespace detail {
|
|
62
|
+
template <typename T, typename ExitT>
|
|
63
|
+
struct handle_type_name<pybind11_extensions::contextlib::ContextManager<T, ExitT>> {
|
|
64
|
+
static constexpr auto name = const_name("contextlib.AbstractContextManager[")
|
|
65
|
+
+ make_caster<std::conditional_t<std::is_same_v<void, T>, pybind11::none, T>>::name
|
|
66
|
+
+ const_name(", ") + make_caster<ExitT>::name + const_name("]");
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/numpy.hpp
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <pybind11/pybind11.h>
|
|
3
|
+
#include <pybind11/numpy.h>
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
// The pybind11 type hint for array_t is incorrect
|
|
7
|
+
// This extension adds a subclass of array_t with the correct type hint.
|
|
8
|
+
// See pybind/pybind11/pull/5212
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
namespace pybind11_extensions {
|
|
12
|
+
namespace numpy {
|
|
13
|
+
template <typename T, int ExtraFlags = pybind11::array::forcecast>
|
|
14
|
+
class array_t : public pybind11::array_t<T, ExtraFlags> {
|
|
15
|
+
using pybind11::array_t<T, ExtraFlags>::array_t;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
namespace pybind11 {
|
|
22
|
+
namespace detail {
|
|
23
|
+
template <typename T, int Flags>
|
|
24
|
+
struct handle_type_name<pybind11_extensions::numpy::array_t<T, Flags>> {
|
|
25
|
+
static constexpr auto name
|
|
26
|
+
= const_name("numpy.typing.NDArray[") + npy_format_descriptor<T>::name + const_name("]");
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/py_module.hpp
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <string>
|
|
4
|
+
#include <filesystem>
|
|
5
|
+
|
|
6
|
+
#include <pybind11/pybind11.h>
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
namespace pybind11_extensions {
|
|
10
|
+
namespace detail {
|
|
11
|
+
inline void def_package_path(pybind11::module m_parent, pybind11::module m, std::string name) {
|
|
12
|
+
pybind11::list paths;
|
|
13
|
+
pybind11::list parent_paths = m_parent.attr("__path__").cast<pybind11::list>();
|
|
14
|
+
for (auto py_path : parent_paths) {
|
|
15
|
+
if (pybind11::isinstance<pybind11::str>(py_path)) {
|
|
16
|
+
std::string path = py_path.cast<std::string>();
|
|
17
|
+
path.push_back(std::filesystem::path::preferred_separator);
|
|
18
|
+
path.append(name);
|
|
19
|
+
paths.append(pybind11::cast(path));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
m.attr("__path__") = paths;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// pybind11 enables creation of sub-modules but not sub-packages.
|
|
27
|
+
// This function creates a sub-module and defines __path__ to make it a package.
|
|
28
|
+
// This enables importing from python modules within the package.
|
|
29
|
+
inline pybind11::module def_subpackage(pybind11::module m_parent, std::string name) {
|
|
30
|
+
auto m = m_parent.def_submodule(name.c_str());
|
|
31
|
+
detail::def_package_path(m_parent, m, name);
|
|
32
|
+
return m;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// __path__ is not defined on the root module until after the pybind11 module constructor finishes.
|
|
36
|
+
// This function patches in the root __path__ to enable def_subpackage to access it.
|
|
37
|
+
inline void set_package_path(pybind11::module m, std::string module_name) {
|
|
38
|
+
m.attr("__path__") = pybind11::module::import("importlib.util").attr("find_spec")(module_name).attr("submodule_search_locations");
|
|
39
|
+
}
|
|
40
|
+
}
|
amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/pybind11.hpp
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <pybind11/pybind11.h>
|
|
3
|
+
|
|
4
|
+
#include "builtins.hpp"
|
|
5
|
+
#include "collections.hpp"
|
|
6
|
+
|
|
7
|
+
namespace pybind11_extensions {
|
|
8
|
+
|
|
9
|
+
// Create a python iterator around a C++ class that implements method next()
|
|
10
|
+
// Next should throw py::stop_iteration() to signal the end of the iterator.
|
|
11
|
+
template <
|
|
12
|
+
typename T,
|
|
13
|
+
pybind11::return_value_policy Policy = pybind11::return_value_policy::automatic,
|
|
14
|
+
typename... Extra>
|
|
15
|
+
auto make_iterator(T&& obj, Extra&&... extra) -> pybind11_extensions::collections::abc::Iterator<decltype(obj.next())>
|
|
16
|
+
{
|
|
17
|
+
if (!pybind11::detail::get_type_info(typeid(T), false)) {
|
|
18
|
+
pybind11::class_<T>(pybind11::handle(), "iterator", pybind11::module_local())
|
|
19
|
+
.def(
|
|
20
|
+
"__iter__",
|
|
21
|
+
[](
|
|
22
|
+
pybind11_extensions::PyObjectCpp<T>& s) -> pybind11_extensions::PyObjectCpp<T>& { return s; })
|
|
23
|
+
.def(
|
|
24
|
+
"__next__",
|
|
25
|
+
[](T& s) -> decltype(obj.next()) {
|
|
26
|
+
return s.next();
|
|
27
|
+
},
|
|
28
|
+
std::forward<Extra>(extra)...,
|
|
29
|
+
Policy);
|
|
30
|
+
}
|
|
31
|
+
return pybind11::cast(std::move(obj));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
} // namespace pybind11_extensions
|
amulet_pybind11_extensions-1.0.0a0/src/pybind11_extensions/include/pybind11_extensions/types.hpp
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <pybind11/pybind11.h>
|
|
3
|
+
#include "builtins.hpp"
|
|
4
|
+
|
|
5
|
+
// This extension adds a py::object subclass for types.NotImplementedType.
|
|
6
|
+
// This is used as a return in comparison operators.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
namespace pybind11_extensions {
|
|
10
|
+
namespace types {
|
|
11
|
+
using NotImplementedType = pybind11_extensions::PyObjectStr<"types.NotImplementedType">;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
if (NOT TARGET pybind11_extensions)
|
|
2
|
+
set(pybind11_extensions_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}/include")
|
|
3
|
+
|
|
4
|
+
add_library(pybind11_extensions IMPORTED INTERFACE)
|
|
5
|
+
set_target_properties(pybind11_extensions PROPERTIES
|
|
6
|
+
INTERFACE_INCLUDE_DIRECTORIES "${pybind11_extensions_INCLUDE_DIR}"
|
|
7
|
+
)
|
|
8
|
+
endif()
|