Skip to content

Commit

Permalink
Fix #1. Improve Python version check
Browse files Browse the repository at this point in the history
- also fix a warning in a test
  • Loading branch information
parcollet committed Aug 1, 2024
1 parent bb49883 commit b0ee963
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/c2py/converters/wrapped.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "./wrapped.hpp"
namespace c2py {

// FIXME: use
// Fetch the pointer to the converter table from __main__
std::shared_ptr<pto_table_t> get_pto_table_from_main() {
// Fetch __main__ module
Expand Down
3 changes: 3 additions & 0 deletions src/c2py/cpp_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace c2py {

// FIXME : in lookup use cpp_name, then typeid(T).name
// specialization of cpp_name <T> for wrapped name
//
// ------------ cpp_name --------------
// name of the c++ type for error messages and signatures

Expand Down
47 changes: 39 additions & 8 deletions src/c2py/pyref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,47 @@

namespace c2py {

// A little safety check that the compiled Python version is the same as the one used to compile c2py
bool check_python_version(long version, long version_major, long version_minor, long version_micro) {
bool b = version == PY_VERSION_HEX;
if (not b)
std::cerr << "\n\n ******* FATAL ERROR in c2py ******** !!! \n\n The c2py library was compiled with Python version " //
<< std::hex << PY_VERSION_HEX << std::dec << " i.e. " << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << '.' << PY_MICRO_VERSION
bool check_python_version(long version_hex, long version_major, long version_minor, long version_micro) {
// Check that the python version of Python.h used to:
// - compile the module including c2py.hpp
// (arguments of this function and frozen at compile time of the module).
// - compile this file, hence libc2py.
// (PY_VERSION_HEX et al. below, determined by the Python.h used to compile this file)
// are identical.
if (version_hex != PY_VERSION_HEX) {
std::cerr << "\n\n ******* FATAL ERROR in c2py ******** !!! \n\n " //
<< "The c2py library was compiled with Python version " //
<< std::hex << PY_VERSION_HEX << std::dec //
<< " i.e. " << PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << '.' << PY_MICRO_VERSION
<< "\n but the python extension is compiled with Python version " //
<< std::hex << version << std::dec << " i.e. " << version_major << '.' << version_minor << '.' << version_micro
<< std::hex << version_hex << std::dec << " i.e. " << version_major << '.' << version_minor << '.' << version_micro
<< "\n They should be identical.\n\n ***********\n";
return b;
return false;
}

// Check that the python version of :
// - the interpreter currently running, picked up from the sys module at runtime.
// - Python.h used to compile the module including c2py.hpp
// (arguments of this function and frozen at compile time of the module).
// are identical.
auto sys = pyref::module("sys");
auto rt_version_hex = sys.attr("hexversion").as<long>();
if (rt_version_hex != version_hex) {
auto rt_version = sys.attr("version");
std::cerr << "\n\n ******* FATAL ERROR in c2py ******** !!! \n\n "
<< "The extension module was compiled with Python version " //
<< std::hex << version_hex << std::dec //
<< " i.e. " << version_major << '.' << version_minor << '.' << version_micro //
<< "\n but the python intepreter has version " //
<< std::hex << rt_version_hex << std::dec << " i.e. " //
<< rt_version.attr("major").as<std::string>() << '.' //
<< rt_version.attr("minor").as<std::string>() << '.' //
<< rt_version.attr("micro").as<std::string>() << '.' //
<< "\n They should be identical.\n\n ***********\n";
return false;
}

return true;
}

//-------------------
Expand Down
8 changes: 6 additions & 2 deletions src/c2py/pyref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
namespace c2py {

using namespace std::string_literals;
bool check_python_version(long version = PY_VERSION_HEX, long version_major = PY_MAJOR_VERSION, long version_minor = PY_MINOR_VERSION,

// Check python version.
// The version passed here is the version of libpython used to compile the module
// as the module includes this file.
// MUST be in cpp. do NOT put this function in hpp.
bool check_python_version(long version_hex = PY_VERSION_HEX, long version_major = PY_MAJOR_VERSION, long version_minor = PY_MINOR_VERSION,
long version_micro = PY_MICRO_VERSION);
// must be in cpp. Mark the version of Python against which c2py is compiled.

/**
* A class to own a reference PyObject *, with proper reference counting.
Expand Down
7 changes: 5 additions & 2 deletions src/c2py/user_api.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <tuple>

// The user facing part of the c2py library
// i.e. what is used in the c2py_module section of the user file.
Expand All @@ -20,8 +21,10 @@ namespace c2py {

// ---------- c2py::dispatch to declare a specific dispatch -------------

template <auto... T> struct dispatch_t {};
template <auto... T> constexpr dispatch_t<T...> dispatch = {};
template <auto... a> struct dispatch_t {
static constexpr auto value = std::tuple{a...};
};
template <auto... a> constexpr dispatch_t<a...> dispatch = {};

// ----------- Some cast operator for the user ---------------------------
// when f is overloaded, cast<arguments....>(f) will disambiguite it
Expand Down
27 changes: 21 additions & 6 deletions test/basicfun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <complex>

int f1(int x) { return x * 3; }
int f1(double x) { return -x * 10; }
double f1(double x) { return -x * 10; }

/**
* A doc for f(x)
Expand Down Expand Up @@ -36,7 +36,7 @@ int ignored(int x) { return x * 3; }
using dcomplex = std::complex<double>;

namespace N {
auto tpl(auto x) { return -2; }
auto tpl(auto) { return -2; }
template <int N> int tplxx() { return 4; }

auto h(auto x) { return x + 4; }
Expand All @@ -50,15 +50,30 @@ namespace N {

// ========== Declare the module ==========

// #pragma clang diagnostic ignored "-Wunused-const-variable"

namespace c2py {
struct dispatch2_t{};
}

constexpr auto ddd(auto && ...x) {return c2py::dispatch2_t{};}
consteval int lambda(auto && x) {return 1;}//c2py::dispatch2_t{};}

constexpr auto mylambda = [](int x) {return x + 1;};
namespace c2py_module {

auto documentation = "Module documentation";
constexpr auto documentation = "Module documentation";

namespace add {
//auto f = c2py::dispatch<c2py::cast<int>(::f), c2py::cast<double>(::f)>;
auto h = c2py::dispatch<N::h<int>, N::h<double>>;
auto hf = c2py::dispatch<c2py::cast<int>(::f1), N::h<double>>;

constexpr auto h = c2py::dispatch<N::h<int>, N::h<double>>;
constexpr auto hf = c2py::dispatch<c2py::cast<int>(::f1), N::h<double>>;
// constexpr auto hf = c2py::dispatch<c2py::cast<int>(::f1), N::h<double>, +[](int x) {return x + 1;}>;

//constexpr auto hf2 = ddd(c2py::cast<int>(::f1), N::h<double>, [](int x) {return x + 1;});

constexpr auto with_lambda = c2py::dispatch<[](int x) {return x + 1;}>;
//constexpr auto with_lambda = c2py::dispatch<mylambda>;
} // namespace add

} // namespace c2py_module

0 comments on commit b0ee963

Please sign in to comment.