Skip to content

Commit

Permalink
Update deprecated functions (#629)
Browse files Browse the repository at this point in the history
* Update minimum cmake version

* Update deprecated FindPythonInterp

CMake Policy CMP0148

* Replace deprecated with PyConfig

* Clean headers

* Clean up

* Handle error

* Fix formatting

* Fix python home

* Clean up
  • Loading branch information
IsabelParedes committed Jun 24, 2024
1 parent 6431151 commit dd50285
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 35 deletions.
32 changes: 16 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
# The full license is in the file LICENSE, distributed with this software. #
############################################################################

cmake_minimum_required(VERSION 3.4.3)
cmake_minimum_required(VERSION 3.20)
project(xeus-python)

set(XEUS_PYTHON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

# Versionning
# ===========
# Versioning
# ==========

file(STRINGS "${XEUS_PYTHON_INCLUDE_DIR}/xeus-python/xeus_python_config.hpp" xpyt_version_defines
REGEX "#define XPYT_VERSION_(MAJOR|MINOR|PATCH)")
Expand All @@ -36,11 +36,6 @@ if (NOT DEFINED XPYTHON_KERNELSPEC_PATH)
set(XPYTHON_KERNELSPEC_PATH "${CMAKE_INSTALL_FULL_BINDIR}/")
endif ()

# Running find_package(PythonInterp) to retrieve the Python version
# which is not exported by Pybind11's cmake.
# Cf. https://github.com/pybind/pybind11/issues/2268
find_package(PythonInterp ${PythonLibsNew_FIND_VERSION} REQUIRED)

if(EMSCRIPTEN)
# the variables PYTHON_VERSION_MAJOR and PYTHON_VERSION_MINOR
# might be wrong for the EMSCRIPTEN case since it may contains
Expand All @@ -65,20 +60,20 @@ configure_file (

# Compilation options

OPTION(XPYT_ENABLE_PYPI_WARNING "Enable warning on PyPI wheels" OFF)
option(XPYT_ENABLE_PYPI_WARNING "Enable warning on PyPI wheels" OFF)

option(XPYT_BUILD_STATIC "Build xeus-python static library" ON)
OPTION(XPYT_BUILD_SHARED "Split xpython build into executable and library" ON)
OPTION(XPYT_BUILD_XPYTHON_EXECUTABLE "Build the xpython executable" ON)
OPTION(XPYT_BUILD_XPYTHON_EXTENSION "Build the xpython extension module" OFF)
option(XPYT_BUILD_SHARED "Split xpython build into executable and library" ON)
option(XPYT_BUILD_XPYTHON_EXECUTABLE "Build the xpython executable" ON)
option(XPYT_BUILD_XPYTHON_EXTENSION "Build the xpython extension module" OFF)

OPTION(XPYT_USE_SHARED_XEUS "Link xpython or xpython_extension with the xeus shared library (instead of the static library)" ON)
OPTION(XPYT_USE_SHARED_XEUS_PYTHON "Link xpython and xpython_extension with the xeus-python shared library (instead of the static library)" ON)
option(XPYT_USE_SHARED_XEUS "Link xpython or xpython_extension with the xeus shared library (instead of the static library)" ON)
option(XPYT_USE_SHARED_XEUS_PYTHON "Link xpython and xpython_extension with the xeus-python shared library (instead of the static library)" ON)

OPTION(XPYT_EMSCRIPTEN_WASM_BUILD "Build for wasm with emscripten" OFF)
option(XPYT_EMSCRIPTEN_WASM_BUILD "Build for wasm with emscripten" OFF)

# Test options
OPTION(XPYT_BUILD_TESTS "xeus-python test suite" OFF)
option(XPYT_BUILD_TESTS "xeus-python test suite" OFF)

function(cat IN_FILE OUT_FILE)
file(READ ${IN_FILE} CONTENTS)
Expand Down Expand Up @@ -112,7 +107,12 @@ set(pybind11_REQUIRED_VERSION 2.6.1)
set(pybind11_json_REQUIRED_VERSION 0.2.8)
set(pyjs_REQUIRED_VERSION 2.0.0)

find_package(Python COMPONENTS Interpreter REQUIRED)

if (NOT TARGET pybind11::headers)
# Defaults to ON for cmake >= 3.18
# https://github.com/pybind/pybind11/blob/35ff42b56e9d34d9a944266eb25f2c899dbdfed7/CMakeLists.txt#L96
set(PYBIND11_FINDPYTHON OFF)
find_package(pybind11 ${pybind11_REQUIRED_VERSION} REQUIRED)
endif ()
if (NOT TARGET pybind11_json)
Expand Down
2 changes: 2 additions & 0 deletions include/xeus-python/xpaths.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace xpyt

XEUS_PYTHON_API std::string get_python_prefix();
XEUS_PYTHON_API std::string get_python_path();

[[deprecated("Use PyConfig.home instead")]]
XEUS_PYTHON_API void set_pythonhome();
}

Expand Down
38 changes: 27 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,52 @@ int main(int argc, char* argv[])
#endif
signal(SIGINT, xpyt::sigkill_handler);

// Python initialization
PyStatus status;

PyConfig config;
PyConfig_InitPythonConfig(&config);
// config.isolated = 1;

// Setting Program Name
static const std::string executable(xpyt::get_python_path());
static const std::wstring wexecutable(executable.cbegin(), executable.cend());
config.program_name = const_cast<wchar_t*>(wexecutable.c_str());

// On windows, sys.executable is not properly set with Py_SetProgramName
// Cf. https://bugs.python.org/issue34725
// A private undocumented API was added as a workaround in Python 3.7.2.
// _Py_SetProgramFullPath(const_cast<wchar_t*>(wexecutable.c_str()));
Py_SetProgramName(const_cast<wchar_t*>(wexecutable.c_str()));

// Setting PYTHONHOME
xpyt::set_pythonhome();
// Setting Python Home
static const std::string pythonhome{ xpyt::get_python_prefix() };
static const std::wstring wstr(pythonhome.cbegin(), pythonhome.cend());;
config.home = const_cast<wchar_t*>(wstr.c_str());
xpyt::print_pythonhome();

// Instanciating the Python interpreter
py::scoped_interpreter guard;
// Implicitly pre-initialize Python
status = PyConfig_SetBytesArgv(&config, argc, argv);
if (PyStatus_Exception(status)) {
std::cerr << "Error:" << status.err_msg << std::endl;
}

// Setting argv
wchar_t** argw = new wchar_t*[size_t(argc)];
for(auto i = 0; i < argc; ++i)
{
argw[i] = Py_DecodeLocale(argv[i], nullptr);
}
PySys_SetArgvEx(argc, argw, 0);
PyWideStringList py_argw;
py_argw.length = argc;
py_argw.items = argw;

config.argv = py_argw;
config.parse_argv = 1;

for(auto i = 0; i < argc; ++i)
{
PyMem_RawFree(argw[i]);
}
delete[] argw;

// Instantiating the Python interpreter
py::scoped_interpreter guard;

std::unique_ptr<xeus::xcontext> context = xeus::make_zmq_context();

// Instantiating the xeus xinterpreter
Expand Down
8 changes: 5 additions & 3 deletions src/xpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ namespace xpyt
#endif
}

[[deprecated]]
void set_pythonhome()
{
static const std::string pythonhome = get_python_prefix();
static const std::wstring wstr(pythonhome.cbegin(), pythonhome.cend());;
Py_SetPythonHome(const_cast<wchar_t*>(wstr.c_str()));
// static const std::string pythonhome = get_python_prefix();
// static const std::wstring wstr(pythonhome.cbegin(), pythonhome.cend());;
// Py_SetPythonHome(const_cast<wchar_t*>(wstr.c_str()));
}

}
15 changes: 11 additions & 4 deletions src/xutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,19 @@ namespace xpyt
void print_pythonhome()
{
std::setlocale(LC_ALL, "en_US.utf8");
// Py_GetPythonHome will return NULL if called before Py_Initialize()
wchar_t* ph = Py_GetPythonHome();

char mbstr[1024];
std::wcstombs(mbstr, ph, 1024);

std::clog << "PYTHONHOME set to " << mbstr << std::endl;
if (ph)
{
char mbstr[1024];
std::wcstombs(mbstr, ph, 1024);
std::clog << "PYTHONHOME set to " << mbstr << std::endl;
}
else
{
std::clog << "PYTHONHOME not set or not initialized." << std::endl;
}
}

// Compares 2 versions and return true if version1 < version2.
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Unit tests
# ==========

cmake_minimum_required(VERSION 3.1)
cmake_minimum_required(VERSION 3.20)

if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
project(xeus-python-test)
Expand Down

0 comments on commit dd50285

Please sign in to comment.