Skip to content

Commit fb39db7

Browse files
committed
Add detailed logging to Python isolate runner
Introduced a _pyLog helper for consistent timestamped debug output and replaced debugPrint calls with _pyLog in runPythonProgramInIsolate. Added more granular logging for interpreter initialization, script execution, module import, and thread state management to aid debugging and traceability.
1 parent 9391c4c commit fb39db7

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/serious_python_android/lib/src/cpython.dart

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ CPython? _cpython;
1616
bool _pythonInitialized = false;
1717
Pointer<PyThreadState>? _mainThreadState;
1818

19+
void _pyLog(String msg) {
20+
debugPrint("[PY] ${DateTime.now().toIso8601String()} $msg");
21+
}
22+
1923
CPython getCPython(String dynamicLibPath) {
2024
return _cpython ??= _cpython = CPython(DynamicLibrary.open(dynamicLibPath));
2125
}
@@ -50,19 +54,22 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
5054
var programDirPath = p.dirname(pythonProgramPath);
5155
var programModuleName = p.basenameWithoutExtension(pythonProgramPath);
5256

53-
debugPrint("dynamicLibPath: $dynamicLibPath");
54-
debugPrint("programDirPath: $programDirPath");
55-
debugPrint("programModuleName: $programModuleName");
57+
_pyLog("dynamicLibPath: $dynamicLibPath");
58+
_pyLog("programDirPath: $programDirPath");
59+
_pyLog("programModuleName: $programModuleName");
60+
_pyLog("script provided: ${script.isNotEmpty} length=${script.length}");
5661

5762
final cpython = getCPython(dynamicLibPath);
5863
if (!_pythonInitialized) {
64+
_pyLog("calling Py_Initialize()");
5965
cpython.Py_Initialize();
6066
// Release the GIL from the init thread so other threads can reacquire it.
6167
_mainThreadState = cpython.PyEval_SaveThread();
6268
_pythonInitialized = true;
63-
debugPrint("after Py_Initialize()");
69+
_pyLog(
70+
"after Py_Initialize(), saved thread state: ${_mainThreadState?.address.toRadixString(16)}");
6471
} else {
65-
debugPrint("Python already initialized; reusing interpreter");
72+
_pyLog("Python already initialized; reusing interpreter");
6673
}
6774

6875
var result = "";
@@ -73,31 +80,38 @@ Future<String> runPythonProgramInIsolate(List<Object> arguments) async {
7380
throw StateError("Python main thread state is null after initialization");
7481
}
7582

83+
_pyLog("restoring thread state ${mainState.address.toRadixString(16)}");
7684
cpython.PyEval_RestoreThread(mainState);
7785

7886
try {
7987
if (script != "") {
8088
// run script
8189
final scriptPtr = script.toNativeUtf8();
90+
_pyLog("calling PyRun_SimpleString");
8291
int sr = cpython.PyRun_SimpleString(scriptPtr.cast<Char>());
83-
debugPrint("PyRun_SimpleString for script result: $sr");
92+
_pyLog("PyRun_SimpleString result: $sr");
8493
malloc.free(scriptPtr);
8594
if (sr != 0) {
8695
result = getPythonError(cpython);
8796
}
8897
} else {
8998
// run program
9099
final moduleNamePtr = programModuleName.toNativeUtf8();
100+
_pyLog("calling PyImport_ImportModule for $programModuleName");
91101
var modulePtr =
92102
cpython.PyImport_ImportModule(moduleNamePtr.cast<Char>());
93103
if (modulePtr == nullptr) {
94104
result = getPythonError(cpython);
105+
} else {
106+
_pyLog("PyImport_ImportModule returned ${modulePtr.address.toRadixString(16)}");
95107
}
96108
malloc.free(moduleNamePtr);
97109
}
98110
} finally {
99111
// Drop the GIL again so subsequent runs/threads can re-acquire it cleanly.
100112
_mainThreadState = cpython.PyEval_SaveThread();
113+
_pyLog(
114+
"saved thread state after run: ${_mainThreadState?.address.toRadixString(16)}");
101115
}
102116

103117
// cpython.Py_Finalize();

0 commit comments

Comments
 (0)