Skip to content

Commit

Permalink
Improve compatiblity (#330)
Browse files Browse the repository at this point in the history
* Moved compatibility checking to pybind11_extensions

* Added compatibility check to tests
  • Loading branch information
gentlegiantJGC authored Jan 23, 2025
1 parent 2fc0b4a commit c09c571
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
python -m pip install --upgrade pip
pip install --no-cache-dir -vv .
pip install pybind11[global]==2.13.6
pip install Amulet_pybind11_extensions@git+https://github.com/Amulet-Team/Amulet-pybind11-extensions.git@5a45161fefc63c06bfa88c3a1005488824726a19
pip install Amulet_pybind11_extensions@git+https://github.com/Amulet-Team/Amulet-pybind11-extensions.git@f85ed8af51dad936d7245581206b166929ce8876
python tests/compile.py
- name: Test with unittest
run: python -m unittest discover -v -s tests
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = [
"wheel",
"versioneer",
"pybind11[global] == 2.13.6",
"Amulet_pybind11_extensions @ git+https://github.com/Amulet-Team/Amulet-pybind11-extensions.git@5a45161fefc63c06bfa88c3a1005488824726a19",
"Amulet_pybind11_extensions @ git+https://github.com/Amulet-Team/Amulet-pybind11-extensions.git@f85ed8af51dad936d7245581206b166929ce8876",
"amulet_nbt == 4.0a10",
"amulet_leveldb == 2.0a3",
]
Expand Down
60 changes: 8 additions & 52 deletions src/amulet/__init__.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>

#include <pybind11/pybind11.h>
#include <pybind11_extensions/compatibility.hpp>
#include <pybind11_extensions/py_module.hpp>
namespace py = pybind11;

Expand All @@ -18,60 +19,14 @@ void init_chunk_components(py::module);
void init_level(py::module);
void init_block_mesh(py::module);

static std::string get_version_top(std::string version){
return version.substr(0, version.find('.', version.find('.') + 1));
}

static void check_compatibility(py::module a, std::string a_name, py::module b, std::string b_name){
py::dict a_config = a.attr("compiler_config");
py::dict b_config = b.attr("compiler_config");

std::string a_pybind11_version = a_config["pybind11_version"].cast<std::string>();
std::string b_pybind11_version = b_config["pybind11_version"].cast<std::string>();
if (a_pybind11_version != b_pybind11_version){
throw std::runtime_error(
"pybind11 version mismatch. " +
a_name + " is compiled for pybind11==" + a_pybind11_version +
" and " +
b_name + " is compiled for pybind11==" + b_pybind11_version
);
}

std::string a_compiler_id = a_config["compiler_id"].cast<std::string>();
std::string b_compiler_id = b_config["compiler_id"].cast<std::string>();
if (a_compiler_id != b_compiler_id){
throw std::runtime_error(
"compiler mismatch. " +
a_name + " is compiled by " + a_compiler_id +
" and " +
b_name + " is compiled by " + b_compiler_id
);
}

std::string a_compiler_version = a_config["compiler_version"].cast<std::string>();
std::string b_compiler_version = b_config["compiler_version"].cast<std::string>();
if (get_version_top(a_compiler_version) != get_version_top(b_compiler_version)){
throw std::runtime_error(
"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
);
}
}

void init_module(py::module m){
void init_module(py::module m)
{
auto amulet_nbt = py::module::import("amulet_nbt");
auto leveldb = py::module::import("leveldb");

py::dict compiler_config;
compiler_config["pybind11_version"] = PYBIND11_VERSION;
compiler_config["compiler_id"] = COMPILER_ID;
compiler_config["compiler_version"] = COMPILER_VERSION;
m.attr("compiler_config") = compiler_config;

check_compatibility(amulet_nbt, "amulet_nbt", m, "amulet");
check_compatibility(leveldb, "leveldb", m, "amulet");
pybind11_extensions::init_compiler_config(m);
pybind11_extensions::check_compatibility(amulet_nbt, m);
pybind11_extensions::check_compatibility(leveldb, m);

// Submodules
init_collections(m);
Expand All @@ -88,7 +43,8 @@ void init_module(py::module m){
init_block_mesh(m);
}

PYBIND11_MODULE(_amulet, m) {
PYBIND11_MODULE(_amulet, m)
{
py::options options;
options.disable_function_signatures();
m.def("init", &init_module, py::doc("init(arg0: types.ModuleType) -> None"));
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ file(GLOB_RECURSE TEST_AMULET_SOURCES LIST_DIRECTORIES false ${SOURCE_PATH}/*.py

pybind11_add_module(_test_amulet)
target_compile_definitions(_test_amulet PRIVATE PYBIND11_DETAILED_ERROR_MESSAGES)
target_compile_definitions(_test_amulet PRIVATE PYBIND11_VERSION="${pybind11_VERSION}")
target_compile_definitions(_test_amulet PRIVATE COMPILER_ID="${CMAKE_CXX_COMPILER_ID}")
target_compile_definitions(_test_amulet PRIVATE COMPILER_VERSION="${CMAKE_CXX_COMPILER_VERSION}")
target_link_libraries(_test_amulet PRIVATE pybind11_extensions)
target_link_libraries(_test_amulet PRIVATE amulet_nbt)
target_link_libraries(_test_amulet PRIVATE leveldb_mcpe)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_amulet/_test_amulet.py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
#include <string>

#include <pybind11/pybind11.h>
#include <pybind11_extensions/compatibility.hpp>
#include <pybind11_extensions/py_module.hpp>
namespace py = pybind11;

void init_test_util(py::module);

void init_module(py::module m){
auto amulet = py::module::import("amulet");

pybind11_extensions::init_compiler_config(m);
pybind11_extensions::check_compatibility(amulet, m);

init_test_util(m);
}

Expand Down

0 comments on commit c09c571

Please sign in to comment.