Skip to content

Commit a582720

Browse files
committed
Solve issues with debug prints on python.
1 parent 4eea25b commit a582720

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

source/loaders/py_loader/source/py_loader_impl.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@
5454
#define DEBUG_ENABLED 0
5555
#endif
5656

57+
/* Set this variable to 1 in order to debug garbage collection data
58+
* and threading flow for improving the debug of memory leaks and async bugs.
59+
* Set it to 0 in order to remove all the noise.
60+
*/
61+
#define DEBUG_PRINT_ENABLED 1
62+
5763
typedef struct loader_impl_py_function_type
5864
{
5965
PyObject *func;
@@ -160,7 +166,7 @@ static value py_loader_impl_error_value(loader_impl_py py_impl);
160166

161167
static value py_loader_impl_error_value_from_exception(loader_impl_py py_impl, PyObject *type_obj, PyObject *value_obj, PyObject *traceback_obj);
162168

163-
#if DEBUG_ENABLED
169+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
164170
#if (defined(__ADDRESS_SANITIZER__) || defined(__MEMORY_SANITIZER__))
165171
static void py_loader_impl_gc_print(loader_impl_py py_impl);
166172
#endif
@@ -2221,7 +2227,7 @@ int py_loader_impl_initialize_traceback(loader_impl impl, loader_impl_py py_impl
22212227
return 1;
22222228
}
22232229

2224-
#if DEBUG_ENABLED
2230+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
22252231
int py_loader_impl_initialize_gc(loader_impl_py py_impl)
22262232
{
22272233
PyObject *flags;
@@ -2396,10 +2402,10 @@ int py_loader_impl_initialize_thread_background_module(loader_impl_py py_impl)
23962402
"import asyncio\n"
23972403
"import threading\n"
23982404
"import sys\n"
2399-
#if DEBUG_ENABLED
2405+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
24002406
"def trace_calls(frame, event, arg):\n"
24012407
" t = threading.current_thread()\n"
2402-
" print(f\"[{t.native_id}] {t.name}: {event} {frame.f_code.co_name}\")\n"
2408+
" print(f\"[{t.native_id}] {t.name}: {event} {frame.f_code.co_name}\", flush=True)\n"
24032409
"threading.settrace(trace_calls)\n"
24042410
"sys.settrace(trace_calls)\n"
24052411
#endif
@@ -2686,7 +2692,7 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl, configuration confi
26862692

26872693
loader_impl_py py_impl = malloc(sizeof(struct loader_impl_py_type));
26882694
int traceback_initialized = 1;
2689-
#if DEBUG_ENABLED
2695+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
26902696
int gc_initialized = 1;
26912697
#endif
26922698
int gil_release;
@@ -2762,7 +2768,7 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl, configuration confi
27622768
traceback_initialized = 0;
27632769
}
27642770

2765-
#if DEBUG_ENABLED
2771+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
27662772
/* Initialize GC module */
27672773
{
27682774
gc_initialized = py_loader_impl_initialize_gc(py_impl);
@@ -2862,7 +2868,7 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl, configuration confi
28622868
Py_DecRef(py_impl->traceback_format_exception);
28632869
Py_DecRef(py_impl->traceback_module);
28642870
}
2865-
#if DEBUG_ENABLED
2871+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
28662872
if (gc_initialized == 0)
28672873
{
28682874
Py_DecRef(py_impl->gc_set_debug);
@@ -2917,7 +2923,7 @@ int py_loader_impl_execution_path(loader_impl impl, const loader_path path)
29172923
goto clear_current_path;
29182924
}
29192925

2920-
#if DEBUG_ENABLED
2926+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
29212927
py_loader_impl_sys_path_print(system_paths);
29222928
#endif
29232929

@@ -4199,7 +4205,7 @@ value py_loader_impl_error_value_from_exception(loader_impl_py py_impl, PyObject
41994205
return ret;
42004206
}
42014207

4202-
#if DEBUG_ENABLED
4208+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
42034209
#if (defined(__ADDRESS_SANITIZER__) || defined(__MEMORY_SANITIZER__))
42044210
void py_loader_impl_gc_print(loader_impl_py py_impl)
42054211
{
@@ -4355,7 +4361,7 @@ int py_loader_impl_destroy(loader_impl impl)
43554361
Py_DecRef(py_impl->thread_background_stop);
43564362
Py_DecRef(py_impl->thread_background_register_atexit);
43574363

4358-
#if DEBUG_ENABLED
4364+
#if DEBUG_ENABLED && DEBUG_PRINT_ENABLED
43594365
{
43604366
#if (defined(__ADDRESS_SANITIZER__) || defined(__MEMORY_SANITIZER__))
43614367
py_loader_impl_gc_print(py_impl);

0 commit comments

Comments
 (0)