Skip to content

Commit

Permalink
Adjust python version checking to only compare minor and major version
Browse files Browse the repository at this point in the history
  • Loading branch information
Wentzell committed Sep 11, 2024
1 parent 5855dd7 commit 8587dae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
48 changes: 21 additions & 27 deletions src/c2py/pyref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,42 @@

namespace c2py {

bool check_python_version(const char *module_name, long version_hex, long version_major, long version_minor, long version_micro) {
bool check_python_version(const char *module_name, long version_major, long version_minor) {

std::stringstream out;

// 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)
// (PY_MAJOR_VERSION and PY_MINOR_VERSION below, determined by the Python.h used to compile this file)
// are identical.
if (version_hex != PY_VERSION_HEX) {
out << "\n\n Can not load the c2py module " //
<< (module_name ? module_name : "") << " ! \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_hex << std::dec << " i.e. " << version_major << '.' << version_minor << '.' << version_micro
<< "\n They should be identical.\n";
if (version_major != PY_MAJOR_VERSION or version_minor != PY_MINOR_VERSION) {
out << "\n\n Can not load the c2py module " //
<< (module_name ? module_name : "") << " ! \n\n" //
<< " The c2py library was compiled with Python version " //
<< PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "\n" //
<< " but the python extension is compiled with Python version" //
<< version_major << '.' << version_minor << "\n" //
<< " They should be identical.\n";
}

// 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 = PyLong_AsLong(sys.attr("hexversion"));
std::cerr << rt_version_hex << " = ? " << version_hex;
if (rt_version_hex != version_hex) {
auto rt_version = sys.attr("version_info");
out << "\n\n Can not load the c2py module " //
<< (module_name ? module_name : "") << " ! \n\n" //
<< " The c2py library 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. " //
<< PyLong_AsLong(rt_version.attr("major")) << '.' //
<< PyLong_AsLong(rt_version.attr("minor")) << '.' //
<< PyLong_AsLong(rt_version.attr("micro")) << '.' //
<< "\n They should be identical.\n";
auto sys_version_info = pyref::module("sys").attr("version_info");
auto rt_version_major = PyLong_AsLong(sys_version_info.attr("major"));
auto rt_version_minor = PyLong_AsLong(sys_version_info.attr("minor"));
if (rt_version_major != PY_MAJOR_VERSION or rt_version_minor != PY_MINOR_VERSION) {
out << "\n\n Can not load the c2py module " //
<< (module_name ? module_name : "") << " ! \n\n" //
<< " The c2py library was compiled with Python version " //
<< PY_MAJOR_VERSION << '.' << PY_MINOR_VERSION << "\n" //
<< " but the python intepreter has version " //
<< rt_version_major << '.' << rt_version_minor << "\n" //
<< " They should be identical.\n";
}

if (out.str().empty()) return true;
Expand Down
3 changes: 1 addition & 2 deletions src/c2py/pyref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ namespace c2py {
// 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(const char *module_name = nullptr, long version_hex = PY_VERSION_HEX, long version_major = PY_MAJOR_VERSION,
long version_minor = PY_MINOR_VERSION, long version_micro = PY_MICRO_VERSION);
bool check_python_version(const char *module_name = nullptr, long version_major = PY_MAJOR_VERSION, long version_minor = PY_MINOR_VERSION);

/**
* A class to own a reference PyObject *, with proper reference counting.
Expand Down

0 comments on commit 8587dae

Please sign in to comment.