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

Moved pybind11 typed classes to external module #314

Merged
merged 2 commits into from
Nov 3, 2024
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ requires = [
"wheel",
"versioneer",
"pybind11 ~= 2.12",
"Amulet-pybind11-extensions @ git+https://github.com/Amulet-Team/Amulet-pybind11-extensions.git@4d7292f3090bb739a55f8377c8f096d134910720",
"amulet_nbt ~= 4.0a6"
]
build-backend = "setuptools.build_meta"
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from distutils import ccompiler
import sys
import pybind11
import pybind11_extensions
import glob
import os
import amulet_nbt
Expand Down Expand Up @@ -44,6 +45,7 @@
sources=glob.glob("src/**/*.cpp", recursive=True),
include_dirs=[
pybind11.get_include(),
pybind11_extensions.get_include(),
amulet_nbt.get_include(),
"src",
],
Expand Down
7 changes: 4 additions & 3 deletions src/amulet/__init__.py.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <pybind11/pybind11.h>
#include <pybind11_extensions/py_module.hpp>
namespace py = pybind11;

void init_collections(py::module);
Expand All @@ -19,11 +20,11 @@ void init_amulet(py::module m){
if (init_run){ return; }
init_run = true;

py::module::import("amulet_nbt");

// This is normally added after initilsation but we need it to pass to subpackages.
// This may cause issues with frozen installs.
m.attr("__path__") = py::module::import("importlib.util").attr("find_spec")("amulet").attr("submodule_search_locations");

py::module::import("amulet_nbt");
pybind11_extensions::set_package_path(m, "amulet");

py::module::import("amulet._init").attr("init")(m);

Expand Down
2 changes: 1 addition & 1 deletion src/amulet/block.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <amulet/collections/sequence.py.hpp>
#include <amulet/collections/eq.py.hpp>
#include <amulet/pybind11/types.hpp>
#include <pybind11_extensions/types.hpp>
#include <amulet/block.hpp>

namespace py = pybind11;
Expand Down
16 changes: 8 additions & 8 deletions src/amulet/chunk_components/section_array_map.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <pybind11/stl.h>
#include <pybind11/operators.h>
#include <pybind11/numpy.h>
#include <amulet/pybind11/numpy.hpp>
#include <pybind11_extensions/numpy.hpp>

#include <amulet/collections/iterator.py.hpp>
#include <amulet/collections/mapping.py.hpp>
Expand Down Expand Up @@ -228,7 +228,7 @@ void init_section_array_map(py::module section_array_map_module) {
);
SectionArrayMap.def(
"__getitem__",
[asarray](const Amulet::SectionArrayMap& self, std::int64_t cy) -> Amulet::pybind11::numpy::array_t<std::uint32_t> {
[asarray](const Amulet::SectionArrayMap& self, std::int64_t cy) -> pybind11_extensions::numpy::array_t<std::uint32_t> {
try {
return asarray(py::cast(self.get_section(cy)));
}
Expand Down Expand Up @@ -256,14 +256,14 @@ void init_section_array_map(py::module section_array_map_module) {
&Amulet::SectionArrayMap::contains_section
);
Amulet::collections::PyMapping_keys<std::int64_t>(SectionArrayMap);
Amulet::collections::PyMapping_values<Amulet::pybind11::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMapping_items<std::int64_t, Amulet::pybind11::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMapping_get<std::int64_t, Amulet::pybind11::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMapping_values<pybind11_extensions::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMapping_items<std::int64_t, pybind11_extensions::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMapping_get<std::int64_t, pybind11_extensions::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMapping_eq(SectionArrayMap);
Amulet::collections::PyMapping_hash(SectionArrayMap);
Amulet::collections::PyMutableMapping_pop<std::int64_t, Amulet::pybind11::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMutableMapping_popitem<std::int64_t, Amulet::pybind11::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMutableMapping_pop<std::int64_t, pybind11_extensions::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMutableMapping_popitem<std::int64_t, pybind11_extensions::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMutableMapping_update(SectionArrayMap);
Amulet::collections::PyMutableMapping_setdefault<std::int64_t, Amulet::pybind11::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMutableMapping_setdefault<std::int64_t, pybind11_extensions::numpy::array_t<std::uint32_t>>(SectionArrayMap);
Amulet::collections::PyMutableMapping_register(SectionArrayMap);
}
4 changes: 2 additions & 2 deletions src/amulet/collections/eq.py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Better type hinting for equality methods that pybind11 has natively

#include <variant>
#include <amulet/pybind11/types.hpp>
#include <pybind11_extensions/types.hpp>

#include <pybind11/pybind11.h>

Expand Down Expand Up @@ -30,7 +30,7 @@ void Eq_default(clsT cls) {
py::object NotImplemented = py::module::import("builtins").attr("NotImplemented");
cls.def(
"__eq__",
[NotImplemented](const typename clsT::type& self, py::object other) -> std::variant<bool, Amulet::pybind11::types::NotImplementedType> {
[NotImplemented](const typename clsT::type& self, py::object other) -> std::variant<bool, pybind11_extensions::types::NotImplementedType> {
return NotImplemented;
}
);
Expand Down
18 changes: 9 additions & 9 deletions src/amulet/collections/mapping.py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <pybind11/pybind11.h>
#include <pybind11/typing.h>

#include <amulet/pybind11/types.hpp>
#include <amulet/pybind11/type_hints.hpp>
#include <amulet/pybind11/collections.hpp>
#include <pybind11_extensions/types.hpp>
#include <pybind11_extensions/builtins.hpp>
#include <pybind11_extensions/collections.hpp>
#include "iterator.py.hpp"

namespace py = pybind11;
Expand Down Expand Up @@ -42,7 +42,7 @@ namespace collections {
void PyMapping_contains(clsT cls) {
cls.def(
"__contains__",
[](py::object self, Amulet::pybind11::type_hints::PyObjectCpp<KT> key) {
[](py::object self, pybind11_extensions::PyObjectCpp<KT> key) {
try {
self.attr("__getitem__")(key);
return true;
Expand All @@ -64,7 +64,7 @@ namespace collections {
py::object KeysView = py::module::import("collections.abc").attr("KeysView");
cls.def(
"keys",
[KeysView](py::object self) -> Amulet::pybind11::collections::KeysView<KT> { return KeysView(self); }
[KeysView](py::object self) -> pybind11_extensions::collections::abc::KeysView<KT> { return KeysView(self); }
);
}

Expand All @@ -73,7 +73,7 @@ namespace collections {
py::object ValuesView = py::module::import("collections.abc").attr("ValuesView");
cls.def(
"values",
[ValuesView](py::object self) -> Amulet::pybind11::collections::ValuesView<VT> { return ValuesView(self); }
[ValuesView](py::object self) -> pybind11_extensions::collections::abc::ValuesView<VT> { return ValuesView(self); }
);
}

Expand All @@ -82,7 +82,7 @@ namespace collections {
py::object ItemsView = py::module::import("collections.abc").attr("ItemsView");
cls.def(
"items",
[ItemsView](py::object self) -> Amulet::pybind11::collections::ItemsView<KT, VT> { return ItemsView(self); }
[ItemsView](py::object self) -> pybind11_extensions::collections::abc::ItemsView<KT, VT> { return ItemsView(self); }
);
}

Expand All @@ -92,7 +92,7 @@ namespace collections {
"get",
[](
py::object self,
Amulet::pybind11::type_hints::PyObjectCpp<KT> key,
pybind11_extensions::PyObjectCpp<KT> key,
py::typing::Optional<VT> default_ = py::none()
) -> py::typing::Optional<VT> {
try {
Expand Down Expand Up @@ -126,7 +126,7 @@ namespace collections {
](
py::object self,
py::object other
) -> std::variant<bool, Amulet::pybind11::types::NotImplementedType> {
) -> std::variant<bool, pybind11_extensions::types::NotImplementedType> {
if (!isinstance(other, PyMapping)) {
return NotImplemented;
}
Expand Down
12 changes: 6 additions & 6 deletions src/amulet/collections/mutable_mapping.py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace collections {
"pop",
[marker](
py::object self,
Amulet::pybind11::type_hints::PyObjectCpp<KT> key,
Amulet::pybind11::type_hints::PyObjectCpp<VT> default_
) -> Amulet::pybind11::type_hints::PyObjectCpp<VT> {
pybind11_extensions::PyObjectCpp<KT> key,
pybind11_extensions::PyObjectCpp<VT> default_
) -> pybind11_extensions::PyObjectCpp<VT> {
py::object value;
try {
value = self.attr("__getitem__")(key);
Expand Down Expand Up @@ -50,8 +50,8 @@ namespace collections {
cls.def(
"popitem",
[iter, next](py::object self) -> std::pair<
Amulet::pybind11::type_hints::PyObjectCpp<KT>,
Amulet::pybind11::type_hints::PyObjectCpp<VT>
pybind11_extensions::PyObjectCpp<KT>,
pybind11_extensions::PyObjectCpp<VT>
> {
py::object key;
try {
Expand Down Expand Up @@ -145,7 +145,7 @@ namespace collections {
"setdefault",
[](
py::object self,
Amulet::pybind11::type_hints::PyObjectCpp<KT> key,
pybind11_extensions::PyObjectCpp<KT> key,
py::typing::Optional<VT> default_ = py::none()
) -> py::typing::Optional<VT> {
try {
Expand Down
6 changes: 3 additions & 3 deletions src/amulet/collections/sequence.py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <pybind11/pybind11.h>
#include "iterator.py.hpp"
#include <amulet/pybind11/collections.hpp>
#include <pybind11_extensions/collections.hpp>

namespace py = pybind11;

Expand Down Expand Up @@ -54,7 +54,7 @@ namespace Amulet {
void Sequence_iter(clsT cls) {
cls.def(
"__iter__",
[](py::object self) -> Amulet::pybind11::collections::Iterator<elemT> {
[](py::object self) -> pybind11_extensions::collections::abc::Iterator<elemT> {
return py::cast(
static_cast<std::shared_ptr<Amulet::collections::Iterator>>(
std::make_shared<PySequenceIterator>(self, 0, 1)
Expand All @@ -68,7 +68,7 @@ namespace Amulet {
void Sequence_reversed(clsT cls) {
cls.def(
"__reversed__",
[](py::object self) -> Amulet::pybind11::collections::Iterator<elemT> {
[](py::object self) -> pybind11_extensions::collections::abc::Iterator<elemT> {
return py::cast(
static_cast<std::shared_ptr<Amulet::collections::Iterator>>(
std::make_shared<PySequenceIterator>(self, py::len(self) - 1, -1)
Expand Down
4 changes: 2 additions & 2 deletions src/amulet/level/_init_level.py.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <string>
#include <pybind11/pybind11.h>
#include <amulet/pybind11/py_module.hpp>
#include <pybind11_extensions/py_module.hpp>
namespace py = pybind11;

py::module init_java(py::module);

void init_level(py::module m_parent) {
auto m = py::def_subpackage(m_parent, "level");
auto m = pybind11_extensions::def_subpackage(m_parent, "level");

m.attr("Level") = py::module::import("amulet.level.abc").attr("Level");

Expand Down
4 changes: 2 additions & 2 deletions src/amulet/level/java/__init_java.py.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <string>
#include <vector>
#include <pybind11/pybind11.h>
#include <amulet/pybind11/py_module.hpp>
#include <pybind11_extensions/py_module.hpp>
namespace py = pybind11;

void init_long_array(py::module);
Expand All @@ -10,7 +10,7 @@ void init_java_chunk(py::module);
void init_java_raw(py::module);

py::module init_java(py::module m_parent) {
auto m = py::def_subpackage(m_parent, "java");
auto m = pybind11_extensions::def_subpackage(m_parent, "java");

init_long_array(m);
init_java_chunk_components(m);
Expand Down
4 changes: 2 additions & 2 deletions src/amulet/level/java/_raw/__init_java_raw.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <map>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <amulet/pybind11/py_module.hpp>
#include <pybind11_extensions/py_module.hpp>
#include "java_chunk_decode.hpp"
#include "java_chunk_encode.hpp"
namespace py = pybind11;
Expand All @@ -12,7 +12,7 @@ void init_java_chunk_decode(py::module);
void init_java_chunk_encode(py::module);

void init_java_raw(py::module m_parent) {
auto m = py::def_subpackage(m_parent, "_raw");
auto m = pybind11_extensions::def_subpackage(m_parent, "_raw");

auto m_chunk = m.def_submodule("_chunk");
m_chunk.def(
Expand Down
11 changes: 5 additions & 6 deletions src/amulet/level/java/_raw/_chunk.pyi
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from __future__ import annotations

import typing

import amulet.level.abc
import amulet.level.java.chunk
import amulet_nbt

__all__ = ["decode_chunk", "encode_chunk"]

def decode_chunk(
arg0: typing.Any,
arg1: typing.Any,
arg0: amulet.level.abc.Level,
arg1: amulet.level.abc.Dimension,
arg2: dict[str, amulet_nbt.NamedTag],
arg3: int,
arg4: int,
) -> amulet.level.java.chunk.JavaChunk: ...
def encode_chunk(
arg0: typing.Any,
arg1: typing.Any,
arg0: amulet.level.abc.Level,
arg1: amulet.level.abc.Dimension,
arg2: amulet.level.java.chunk.JavaChunk,
arg3: int,
arg4: int,
Expand Down
5 changes: 3 additions & 2 deletions src/amulet/level/java/_raw/java_chunk_decode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm>

#include <pybind11/pybind11.h>
#include <pybind11_extensions/builtins.hpp>

#include <amulet_nbt/tag/named_tag.hpp>
#include <amulet_nbt/tag/compound.hpp>
Expand Down Expand Up @@ -466,8 +467,8 @@ namespace Amulet {
}

std::shared_ptr<JavaChunk> decode_java_chunk(
py::object raw_level,
py::object dimension,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Level"> raw_level,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Dimension"> dimension,
std::map<std::string, NamedTag>& raw_chunk,
std::int64_t cx,
std::int64_t cz
Expand Down
5 changes: 3 additions & 2 deletions src/amulet/level/java/_raw/java_chunk_decode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdint>

#include <pybind11/pybind11.h>
#include <pybind11_extensions/builtins.hpp>

#include <amulet_nbt/tag/named_tag.hpp>

Expand All @@ -14,8 +15,8 @@ namespace py = pybind11;

namespace Amulet {
std::shared_ptr<Amulet::JavaChunk> decode_java_chunk(
py::object raw_level,
py::object dimension,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Level"> raw_level,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Dimension"> dimension,
std::map<std::string, AmuletNBT::NamedTag>& raw_chunk,
std::int64_t cx,
std::int64_t cz
Expand Down
5 changes: 3 additions & 2 deletions src/amulet/level/java/_raw/java_chunk_encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdexcept>

#include <pybind11/pybind11.h>
#include <pybind11_extensions/builtins.hpp>

#include <amulet_nbt/tag/named_tag.hpp>

Expand All @@ -14,8 +15,8 @@ namespace py = pybind11;

namespace Amulet {
std::map<std::string, AmuletNBT::NamedTag> encode_java_chunk(
py::object raw_level,
py::object dimension,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Level"> raw_level,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Dimension"> dimension,
std::shared_ptr<Amulet::JavaChunk> chunk,
std::int64_t cx,
std::int64_t cz
Expand Down
5 changes: 3 additions & 2 deletions src/amulet/level/java/_raw/java_chunk_encode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdint>

#include <pybind11/pybind11.h>
#include <pybind11_extensions/builtins.hpp>

#include <amulet_nbt/tag/named_tag.hpp>

Expand All @@ -14,8 +15,8 @@ namespace py = pybind11;

namespace Amulet {
std::map<std::string, AmuletNBT::NamedTag> encode_java_chunk(
py::object raw_level,
py::object dimension,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Level"> raw_level,
pybind11_extensions::PyObjectStr<"amulet.level.abc.Dimension"> dimension,
std::shared_ptr<Amulet::JavaChunk> chunk,
std::int64_t cx,
std::int64_t cz
Expand Down
Loading
Loading