Skip to content

Commit a272d79

Browse files
committed
Refactor GIL usage in Python execution and error handling
Moved _withGIL calls to wrap larger code blocks instead of individual function calls in runPythonProgramInIsolate and getPythonError. This simplifies the code and reduces redundant GIL management, improving readability and maintainability.
1 parent 8633a01 commit a272d79

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

src/serious_python_android/lib/src/cpython.dart

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,14 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
9696
_debug("after Py_Initialize()");
9797
}
9898

99-
final logcatSetupError = _withGIL(cpython, () => _setupLogcatForwarding(cpython));
100-
if (logcatSetupError != null) {
101-
sendPort.send(logcatSetupError);
102-
return logcatSetupError;
103-
}
104-
105-
var result = "";
99+
final result = _withGIL(cpython, () {
100+
final logcatSetupError = _setupLogcatForwarding(cpython);
101+
if (logcatSetupError != null) {
102+
return logcatSetupError;
103+
}
106104

107-
if (script != "") {
108-
// run script
109-
result = _withGIL(cpython, () {
105+
if (script != "") {
106+
// run script
110107
_debug("Running script: $script");
111108
final scriptPtr = script.toNativeUtf8();
112109
int sr = cpython.PyRun_SimpleString(scriptPtr.cast<Char>());
@@ -115,11 +112,8 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
115112
if (sr != 0) {
116113
return getPythonError(cpython);
117114
}
118-
return "";
119-
});
120-
} else {
121-
// run program
122-
result = _withGIL(cpython, () {
115+
} else {
116+
// run program
123117
_debug("Running program module: $programModuleName");
124118
final moduleNamePtr = programModuleName.toNativeUtf8();
125119
var modulePtr = cpython.PyImport_ImportModule(moduleNamePtr.cast<Char>());
@@ -129,9 +123,10 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
129123
return error;
130124
}
131125
malloc.free(moduleNamePtr);
132-
return "";
133-
});
134-
}
126+
}
127+
128+
return "";
129+
});
135130

136131
_debug("Python program finished");
137132

@@ -142,41 +137,38 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
142137

143138
String getPythonError(CPython cpython) {
144139
// get error object
145-
var exPtr = _withGIL(cpython, () => cpython.PyErr_GetRaisedException());
140+
var exPtr = cpython.PyErr_GetRaisedException();
146141

147142
// use 'traceback' module to format exception
148143
final tracebackModuleNamePtr = "traceback".toNativeUtf8();
149-
var tracebackModulePtr = _withGIL(cpython,
150-
() => cpython.PyImport_ImportModule(tracebackModuleNamePtr.cast<Char>()));
144+
var tracebackModulePtr =
145+
cpython.PyImport_ImportModule(tracebackModuleNamePtr.cast<Char>());
151146
cpython.Py_DecRef(tracebackModuleNamePtr.cast());
152147

153148
if (tracebackModulePtr != nullptr) {
154149
//_debug("Traceback module loaded");
155150

156151
final formatFuncName = "format_exception".toNativeUtf8();
157-
final pFormatFunc = _withGIL(
158-
cpython,
159-
() => cpython.PyObject_GetAttrString(
160-
tracebackModulePtr, formatFuncName.cast()));
152+
final pFormatFunc =
153+
cpython.PyObject_GetAttrString(tracebackModulePtr, formatFuncName.cast());
161154
cpython.Py_DecRef(tracebackModuleNamePtr.cast());
162155

163156
if (pFormatFunc != nullptr && cpython.PyCallable_Check(pFormatFunc) != 0) {
164157
// call `traceback.format_exception()` method
165-
final pArgs = _withGIL(cpython, () => cpython.PyTuple_New(1));
166-
_withGIL(cpython, () => cpython.PyTuple_SetItem(pArgs, 0, exPtr));
158+
final pArgs = cpython.PyTuple_New(1);
159+
cpython.PyTuple_SetItem(pArgs, 0, exPtr);
167160

168161
// result is a list
169-
var listPtr =
170-
_withGIL(cpython, () => cpython.PyObject_CallObject(pFormatFunc, pArgs));
162+
var listPtr = cpython.PyObject_CallObject(pFormatFunc, pArgs);
171163

172164
// get and combine list items
173165
var exLines = [];
174-
var listSize = _withGIL(cpython, () => cpython.PyList_Size(listPtr));
166+
var listSize = cpython.PyList_Size(listPtr);
175167
for (var i = 0; i < listSize; i++) {
176-
var itemObj = _withGIL(cpython, () => cpython.PyList_GetItem(listPtr, i));
177-
var itemObjStr = _withGIL(cpython, () => cpython.PyObject_Str(itemObj));
178-
var s = _withGIL(cpython,
179-
() => cpython.PyUnicode_AsUTF8(itemObjStr).cast<Utf8>().toDartString());
168+
var itemObj = cpython.PyList_GetItem(listPtr, i);
169+
var itemObjStr = cpython.PyObject_Str(itemObj);
170+
var s =
171+
cpython.PyUnicode_AsUTF8(itemObjStr).cast<Utf8>().toDartString();
180172
exLines.add(s);
181173
}
182174
return exLines.join("");

0 commit comments

Comments
 (0)