Skip to content

Correctly check whether Python is initialized in tortoize.cpp #4

@marcuscollins

Description

@marcuscollins

From CodeRabbit's review of the first PR:

Guard extract_python_executable_path() against uninitialized Python interpreter.

py::module::import("sys") at line 53 requires an initialized interpreter and GIL. This will crash in C++ unit tests (like tortoize-unit-test.cpp calling tortoize_calculate()), which trigger DataTable::instance() → constructor → extract_python_executable_path() without initializing Python.

Guard with Py_IsInitialized() before the import, or ensure the interpreter is initialized at all entrypoints that eventually trigger DataTable::instance().

fs::path extract_python_executable_path() {

  • if (not Py_IsInitialized())
  •   throw std::runtime_error("Python interpreter not initialized; initialize it before calling tortoize");
    
  • py::gil_scoped_acquire gil;
    py::module py_sys = py::module::import("sys");
    py::str py_exec = py_sys.attr("executable");
    fs::path py_exec_path = fs::path(py_exec.caststd::string());
    return py_exec_path;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions