Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Progress towards Python 3.11 support #255

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions python/include/python_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef PYTHON_ACTION_H
#define PYTHON_ACTION_H

#include <Python.h>
#include "pythontarget.h"
#include <structmember.h>
#include <stdbool.h>
#include "python_capsule_extension.h"
#include "lf_types.h"
#include "pythontarget.h"

extern PyTypeObject py_action_capsule_t;

Expand Down
31 changes: 24 additions & 7 deletions python/lib/pythontarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* Implementation of functions defined in @see pythontarget.h
*/

#include "pythontarget.h"
#include "modal_models/definitions.h"
#include "platform.h" // defines MAX_PATH on Windows
#include "python_action.h"
#include "python_port.h"
#include "python_tag.h"
#include "python_time.h"
#include "pythontarget.h"
#include "reactor_common.h"
#include "reactor.h"
#include "tag.h"
Expand Down Expand Up @@ -234,6 +234,22 @@ const char** _lf_py_parse_argv_impl(PyObject* py_argv, size_t* argc) {
return argv;
}

static bool py_initialized = false;

/**
* @brief Initialize the Python interpreter if it hasn't already been.
*/
void py_initialize_interpreter(void) {
if (!py_initialized) {
py_initialized = true;

// Initialize the Python interpreter
Py_Initialize();

LF_PRINT_DEBUG("Initialized the Python interpreter.");
}
}

//////////////////////////////////////////////////////////////
///////////// Main function callable from Python code
/**
Expand All @@ -249,8 +265,7 @@ PyObject* py_main(PyObject* self, PyObject* py_args) {
size_t argc;
const char** argv = _lf_py_parse_argv_impl(py_args, &argc);

// Initialize the Python interpreter
Py_Initialize();
py_initialize_interpreter();

// Load the pickle module
if (global_pickler == NULL) {
Expand All @@ -267,8 +282,6 @@ PyObject* py_main(PyObject* self, PyObject* py_args) {
int num_environments = _lf_get_environments(&top_level_environment);
lf_assert(num_environments == 1, "Python target only supports programs with a single environment/enclave");

LF_PRINT_DEBUG("Initialized the Python interpreter.");

Py_BEGIN_ALLOW_THREADS
lf_reactor_c_main(argc, argv);
Py_END_ALLOW_THREADS
Expand Down Expand Up @@ -313,6 +326,7 @@ static PyModuleDef MODULE_NAME = {

//////////////////////////////////////////////////////////////
///////////// Module Initialization

/*
* The Python runtime will call this function to initialize the module.
* The name of this function is dynamically generated to follow
Expand All @@ -324,8 +338,13 @@ static PyModuleDef MODULE_NAME = {
*/
PyMODINIT_FUNC
GEN_NAME(PyInit_,MODULE_NAME)(void) {

PyObject *m;

// As of Python 11, this function may be called before py_main, so we need to
// initialize the interpreter.
py_initialize_interpreter();

// Initialize the port_capsule type
if (PyType_Ready(&py_port_capsule_t) < 0) {
return NULL;
Expand Down Expand Up @@ -362,7 +381,6 @@ GEN_NAME(PyInit_,MODULE_NAME)(void) {
return NULL;
}


// Add the action_capsule type to the module's dictionary
Py_INCREF(&py_action_capsule_t);
if (PyModule_AddObject(m, "action_capsule_t", (PyObject *) &py_action_capsule_t) < 0) {
Expand All @@ -386,7 +404,6 @@ GEN_NAME(PyInit_,MODULE_NAME)(void) {
Py_DECREF(m);
return NULL;
}

return m;
}

Expand Down