diff --git a/src/c2py/converters/wrapped.cpp b/src/c2py/converters/wrapped.cpp index c2749a6..cb3f8b8 100644 --- a/src/c2py/converters/wrapped.cpp +++ b/src/c2py/converters/wrapped.cpp @@ -1,6 +1,7 @@ #include "./wrapped.hpp" namespace c2py { + // FIXME: use // Fetch the pointer to the converter table from __main__ std::shared_ptr get_pto_table_from_main() { // Fetch __main__ module diff --git a/src/c2py/cpp_name.hpp b/src/c2py/cpp_name.hpp index b6b4c2f..f0ceb04 100644 --- a/src/c2py/cpp_name.hpp +++ b/src/c2py/cpp_name.hpp @@ -5,6 +5,9 @@ namespace c2py { + // FIXME : in lookup use cpp_name, then typeid(T).name + // specialization of cpp_name for wrapped name + // // ------------ cpp_name -------------- // name of the c++ type for error messages and signatures diff --git a/src/c2py/pyref.cpp b/src/c2py/pyref.cpp index 8817372..4481d24 100644 --- a/src/c2py/pyref.cpp +++ b/src/c2py/pyref.cpp @@ -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(); + 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() << '.' // + << rt_version.attr("minor").as() << '.' // + << rt_version.attr("micro").as() << '.' // + << "\n They should be identical.\n\n ***********\n"; + return false; + } + + return true; } //------------------- diff --git a/src/c2py/pyref.hpp b/src/c2py/pyref.hpp index e8fea9b..a374e62 100644 --- a/src/c2py/pyref.hpp +++ b/src/c2py/pyref.hpp @@ -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. diff --git a/src/c2py/user_api.hpp b/src/c2py/user_api.hpp index 57f22ea..9d3cf25 100644 --- a/src/c2py/user_api.hpp +++ b/src/c2py/user_api.hpp @@ -1,4 +1,5 @@ #pragma once +#include // The user facing part of the c2py library // i.e. what is used in the c2py_module section of the user file. @@ -20,8 +21,10 @@ namespace c2py { // ---------- c2py::dispatch to declare a specific dispatch ------------- - template struct dispatch_t {}; - template constexpr dispatch_t dispatch = {}; + template struct dispatch_t { + static constexpr auto value = std::tuple{a...}; + }; + template constexpr dispatch_t dispatch = {}; // ----------- Some cast operator for the user --------------------------- // when f is overloaded, cast(f) will disambiguite it diff --git a/test/basicfun.cpp b/test/basicfun.cpp index 79814fd..3b4fc08 100644 --- a/test/basicfun.cpp +++ b/test/basicfun.cpp @@ -3,7 +3,7 @@ #include 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) @@ -36,7 +36,7 @@ int ignored(int x) { return x * 3; } using dcomplex = std::complex; namespace N { - auto tpl(auto x) { return -2; } + auto tpl(auto) { return -2; } template int tplxx() { return 4; } auto h(auto x) { return x + 4; } @@ -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(::f), c2py::cast(::f)>; - auto h = c2py::dispatch, N::h>; - auto hf = c2py::dispatch(::f1), N::h>; - + constexpr auto h = c2py::dispatch, N::h>; + constexpr auto hf = c2py::dispatch(::f1), N::h>; + // constexpr auto hf = c2py::dispatch(::f1), N::h, +[](int x) {return x + 1;}>; + + //constexpr auto hf2 = ddd(c2py::cast(::f1), N::h, [](int x) {return x + 1;}); + + constexpr auto with_lambda = c2py::dispatch<[](int x) {return x + 1;}>; + //constexpr auto with_lambda = c2py::dispatch; } // namespace add } // namespace c2py_module