Skip to content

Commit 4274556

Browse files
committed
Improve Python interpreter initialization
1 parent ab754d9 commit 4274556

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

Source/PythonProcessor.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ PythonProcessor::~PythonProcessor()
5353
{
5454
delete pyModule;
5555
delete pyObject;
56-
py::gil_scoped_release release;
56+
// py::gil_scoped_release release;
5757
}
5858
py::finalize_interpreter();
5959
}
@@ -125,7 +125,8 @@ void PythonProcessor::process(AudioBuffer<float>& buffer)
125125
for (auto stream : getDataStreams())
126126
{
127127

128-
if (stream->getStreamId() == currentStream)
128+
if (stream->getStreamId() == currentStream
129+
&& (*stream)["enable_stream"])
129130
{
130131
const uint16 streamId = stream->getStreamId();
131132

@@ -150,8 +151,7 @@ void PythonProcessor::process(AudioBuffer<float>& buffer)
150151
// Call python script on this block
151152
pyObject->attr("process")(numpyArray);
152153

153-
154-
// // Write back from numpy array
154+
// Write back from numpy array
155155
for (int i = 0; i < numChannels; ++i) {
156156
int globalChannelIndex = getGlobalChannelIndex(stream->getStreamId(), i);
157157

@@ -424,25 +424,25 @@ bool PythonProcessor::initInterpreter(String pythonHome)
424424
try
425425
{
426426
getParameter("python_home")->currentValue = targetFolder.getFullPathName();
427-
Py_SetPythonHome(targetFolder.getFullPathName().toWideCharPointer());
428-
429-
String pythonPaths = String();
430-
431-
#if JUCE_WINDOWS
432-
pythonPaths = targetFolder.getFullPathName()
433-
+ ";"
434-
+ targetFolder.getChildFile("lib").getFullPathName()
435-
+ ";"
436-
+ targetFolder.getChildFile("lib/site-packages").getFullPathName()
437-
+ ";"
438-
+ targetFolder.getChildFile("DLLs").getFullPathName();
427+
428+
// Use PyConfig instead of direct Py_SetPythonHome/Py_SetPath calls
429+
PyConfig config;
430+
PyStatus status;
439431

440-
Py_SetPath(pythonPaths.toWideCharPointer());
441-
#endif
432+
PyConfig_InitPythonConfig(&config);
433+
434+
// Set Python home
435+
status = PyConfig_SetString(&config, &config.home,
436+
targetFolder.getFullPathName().toWideCharPointer());
437+
if (PyStatus_Exception(status)) {
438+
PyConfig_Clear(&config);
439+
throw std::runtime_error("Failed to set Python home");
440+
}
442441

443-
py::initialize_interpreter();
442+
// Initialize with PyConfig
443+
py::initialize_interpreter(&config);
444444
{
445-
py::gil_scoped_acquire acquire;
445+
// py::gil_scoped_acquire acquire;
446446

447447
if(Py_IsInitialized() > 0)
448448
{
@@ -472,6 +472,12 @@ bool PythonProcessor::initInterpreter(String pythonHome)
472472
handlePythonException(errText, "", e);
473473
return false;
474474
}
475+
catch(const std::runtime_error& e)
476+
{
477+
String errText = "Python configuration error: " + String(e.what());
478+
LOGE(errText);
479+
return false;
480+
}
475481
}
476482

477483
bool PythonProcessor::importModule()
@@ -596,7 +602,7 @@ void PythonProcessor::handlePythonException(const String& title, const String& m
596602
{
597603
LOGE("Python Exception:\n", e.what());
598604

599-
TextEditor* customMsgBox = new TextEditor();
605+
std::unique_ptr<TextEditor> customMsgBox = std::make_unique<TextEditor>();
600606
customMsgBox->setReadOnly(true);
601607
customMsgBox->setMultiLine(true);
602608
customMsgBox->setFont(FontOptions(14.0f));
@@ -613,8 +619,8 @@ void PythonProcessor::handlePythonException(const String& title, const String& m
613619
AlertWindow::WarningIcon);
614620

615621
KeyPress dismissKey(KeyPress::returnKey, 0, 0);
616-
exceptionWindow.addButton("OK", 1, dismissKey);
617-
exceptionWindow.addCustomComponent(customMsgBox);
622+
exceptionWindow.addButton("OK", 0, dismissKey);
623+
exceptionWindow.addCustomComponent(customMsgBox.get());
618624

619625
exceptionWindow.runModalLoop();
620626

0 commit comments

Comments
 (0)