-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-module.cpp
More file actions
976 lines (868 loc) · 39.7 KB
/
Copy pathpython-module.cpp
File metadata and controls
976 lines (868 loc) · 39.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* indent-tabs-mode: nil -*- */
/*
python Qore module
Copyright (C) 2020 - 2026 Qore Technologies, s.r.o.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "python-module.h"
#include "QC_PythonProgram.h"
#include "QorePythonProgram.h"
#include "QorePythonStackLocationHelper.h"
#include <string>
static void python_module_init(QoreModuleInitContext& ctx, ExceptionSink& xsink);
static void python_module_ns_init(QoreNamespace* rns, QoreNamespace* qns, ExceptionSink& xsink);
static void python_module_delete();
static void python_module_parse_cmd(const QoreString& cmd, ExceptionSink* xsink);
static QoreStringNode* python_module_init_intern(bool repeat);
// module declaration for Qore 0.9.5+
void python_qore_module_desc(QoreModuleInfo& mod_info) {
mod_info.name = QORE_PYTHON_MODULE_NAME;
mod_info.version = PACKAGE_VERSION;
mod_info.desc = "python module";
mod_info.author = "David Nichols";
mod_info.url = "http://qore.org";
mod_info.api_major = QORE_MODULE_API_MAJOR;
mod_info.api_minor = QORE_MODULE_API_MINOR;
mod_info.init = python_module_init;
mod_info.ns_init = python_module_ns_init;
mod_info.del = python_module_delete;
mod_info.parse_cmd = python_module_parse_cmd;
mod_info.license = QL_MIT;
mod_info.license_str = "MIT";
mod_info.info = new QoreHashNode(autoTypeInfo);
mod_info.info->setKeyValue("python_version", new QoreStringNodeMaker(PY_VERSION), nullptr);
mod_info.info->setKeyValue("python_major", PY_MAJOR_VERSION, nullptr);
mod_info.info->setKeyValue("python_minor", PY_MINOR_VERSION, nullptr);
mod_info.info->setKeyValue("python_micro", PY_MICRO_VERSION, nullptr);
}
QoreNamespace* PNS = nullptr;
PyThreadState* mainThreadState = nullptr;
QorePythonClass* QC_PYTHONBASEOBJECT;
qore_classid_t CID_PYTHONBASEOBJECT;
// module cmd type
using qore_python_module_cmd_t = void (*) (ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
static void py_mc_import(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
static void py_mc_import_ns(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
static void py_mc_alias(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
static void py_mc_parse(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
static void py_mc_export_class(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
static void py_mc_export_func(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
static void py_mc_add_module_path(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
//static void py_mc_reset_python(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm);
struct qore_python_cmd_info_t {
qore_python_module_cmd_t cmd;
bool requires_arg = true;
DLLLOCAL qore_python_cmd_info_t(qore_python_module_cmd_t cmd, bool requires_arg)
: cmd(cmd), requires_arg(requires_arg) {
}
};
// module cmds
typedef std::map<std::string, qore_python_cmd_info_t> mcmap_t;
static mcmap_t mcmap = {
{"import", qore_python_cmd_info_t(py_mc_import, true)},
{"import-ns", qore_python_cmd_info_t(py_mc_import_ns, true)},
{"alias", qore_python_cmd_info_t(py_mc_alias, true)},
{"parse", qore_python_cmd_info_t(py_mc_parse, true)},
{"export-class", qore_python_cmd_info_t(py_mc_export_class, true)},
{"export-func", qore_python_cmd_info_t(py_mc_export_func, true)},
{"add-module-path", qore_python_cmd_info_t(py_mc_add_module_path, true)},
#if 0
{"reset-python", qore_python_cmd_info_t(py_mc_reset_python, false)},
#endif
};
static bool python_needs_shutdown = false;
static bool python_initialized = false;
bool python_shutdown = false;
int python_u_tld_key = -1;
int python_qobj_key = -1;
static sig_vec_t sig_vec = {
#ifndef _Q_WINDOWS
SIGSEGV, SIGBUS
#endif
};
static void check_python_version() {
QorePythonReferenceHolder mod(PyImport_ImportModule("sys"));
if (!mod) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "Python could not load module 'sys'");
}
// returns a borrowed reference
PyObject* mod_dict = PyModule_GetDict(*mod);
if (!mod_dict) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "Python module 'sys' has no dictionary");
}
// returns a borrowed reference
PyObject* value = PyDict_GetItemString(mod_dict, "version_info");
if (!value) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "symbol 'sys.version_info' not found; cannot verify the " \
"runtime version of the Python library");
}
if (!PyObject_HasAttrString(value, "major")) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "symbol 'sys.version.major' was not found; cannot " \
"verify the runtime version of the Python library");
}
QorePythonReferenceHolder py_major(PyObject_GetAttrString(value, "major"));
if (!PyLong_Check(*py_major)) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "symbol 'sys.version.major' has type '%s'; expecting " \
"'int'; cannot verify the runtime version of the Python library", Py_TYPE(*py_major)->tp_name);
}
long major = PyLong_AsLong(*py_major);
if (major != PY_MAJOR_VERSION) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "Python runtime major version is %ld, but the module was " \
"compiled with major version %d (%s)", major, PY_MAJOR_VERSION, PY_VERSION);
}
QorePythonReferenceHolder py_minor(PyObject_GetAttrString(value, "minor"));
if (!PyLong_Check(*py_minor)) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "symbol 'sys.version.minor' has type '%s'; expecting " \
"'int'; cannot verify the runtime version of the Python library", Py_TYPE(*py_minor)->tp_name);
}
long minor = PyLong_AsLong(*py_minor);
if (minor != PY_MINOR_VERSION) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "Python runtime version is %ld.%ld, but the module was " \
"compiled with version %d.%d (%s)", major, minor, PY_MAJOR_VERSION, PY_MINOR_VERSION, PY_VERSION);
}
//printd(5, "python runtime version OK: %ld.%ld.x =~ '%s'\n", major, minor, PY_VERSION);
}
static void python_module_shutdown() {
if (python_initialized) {
_QORE_PYTHREAD_STATE_SWAP(nullptr);
_qore_acquire_thread_state(mainThreadState);
_qore_PyGILState_SetThisThreadState(mainThreadState);
}
python_shutdown = true;
if (python_needs_shutdown) {
int rc = Py_FinalizeEx();
if (rc) {
printd(0, "Unkown error shutting down Python: rc: %d\n", rc);
}
python_needs_shutdown = false;
}
}
#if 0
// does not work with modules like tensorflow that do not unload cleanly
int q_reset_python(ExceptionSink* xsink) {
if (!python_needs_shutdown) {
xsink->raiseException("PYTHON-RESET-ERROR", "The module was loaded into an existing Python process and " \
"therefore cannot be reset externally");
return -1;
}
unsigned cnt = QorePythonProgram::getProgramCount();
if (cnt) {
if (cnt <= 2) {
QoreProgram* pgm = getProgram();
if (pgm) {
QorePythonProgramData* pypgm = static_cast<QorePythonProgramData*>(pgm->removeExternalData(QORE_PYTHON_MODULE_NAME));
if (pypgm) {
pypgm->destructor(xsink);
pypgm->weakDeref();
if (*xsink) {
return -1;
}
--cnt;
}
}
}
if (cnt == 1 && qore_python_pgm) {
qore_python_pgm->destructor(xsink);
qore_python_pgm->weakDeref();
qore_python_pgm = nullptr;
--cnt;
}
if (cnt) {
xsink->raiseException("PYTHON-RESET-ERROR", "Cannot reset the Python library with %d Python program%s " \
"still valid", cnt, cnt == 1 ? "" : "s");
return -1;
}
}
python_module_shutdown();
SimpleRefHolder<QoreStringNode> err(python_module_init_intern(true));
if (err) {
xsink->raiseException("PYTHON-RESET-ERROR", err.release());
return -1;
}
return 0;
}
#endif
static void python_module_init(QoreModuleInitContext& ctx, ExceptionSink& xsink) {
QoreStringNode* err = python_module_init_intern(false);
if (err) {
xsink.raiseException("MODULE-INIT-ERROR", err);
}
}
// defined in the generated ql_python.cpp (from ql_python.qpp)
DLLLOCAL void init_python_functions(QoreNamespace& ns);
static QoreStringNode* python_module_init_intern(bool repeat) {
if (!PNS) {
PNS = new QoreNamespace("Python");
PNS->addSystemClass(initPythonProgramClass(*PNS));
// register Python namespace functions (e.g. Python::set_save_object_callback())
init_python_functions(*PNS);
QC_PYTHONBASEOBJECT = new QorePythonClass("__qore_base__", "::Python::__qore_base__");
CID_PYTHONBASEOBJECT = QC_PYTHONBASEOBJECT->getID();
PNS->addSystemClass(QC_PYTHONBASEOBJECT->copy());
// Add constant to indicate if this is a free-threading Python build
#ifdef Py_GIL_DISABLED
PNS->addConstant("FreeThreading", true);
#else
PNS->addConstant("FreeThreading", false);
#endif
}
if (!repeat) {
python_u_tld_key = q_get_unique_thread_local_data_key();
python_qobj_key = q_get_unique_thread_local_data_key();
}
// initialize python library; do not register signal handlers
if (!Py_IsInitialized()) {
if (PyImport_AppendInittab("qoreloader", PyInit_qoreloader) == -1) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "cannot append the qoreloader module to Python");
}
Py_InitializeEx(0);
#ifdef QORE_ALLOW_PYTHON_SHUTDOWN
// issue# 4290: if we actively shut down Python on exit, then exit handlers in modules
// (such as the h5py module in version 3.3.0) will cause a crash when the process exits,
// as it requires the Python library to be still in place and initialized
python_needs_shutdown = true;
#endif
python_initialized = true;
//printd(5, "python_module_init() Python initialized\n");
}
if (!repeat) {
#ifndef _Q_WINDOWS
sig_vec_t new_sig_vec;
for (int sig : sig_vec) {
QoreStringNode *err = qore_reassign_signal(sig, QORE_PYTHON_MODULE_NAME);
if (err) {
// ignore errors; already assigned to another module
err->deref();
}
new_sig_vec.push_back(sig);
}
if (!new_sig_vec.empty()) {
sigset_t mask;
// setup signal mask
sigemptyset(&mask);
for (auto& sig : new_sig_vec) {
//printd(LogLevel, "python_module_init() unblocking signal %d\n", sig);
sigaddset(&mask, sig);
}
// unblock threads
pthread_sigmask(SIG_UNBLOCK, &mask, 0);
}
#endif
}
// ensure that runtime version matches compiled version
check_python_version();
// Ensure sys.path is initialized when embedding (esp. for debug builds).
const char* pyhome = getenv("PYTHONHOME");
const char* pypath = getenv("PYTHONPATH");
if ((pyhome && *pyhome) || (pypath && *pypath)) {
#ifdef _Q_WINDOWS
const char path_sep = ';';
#else
const char path_sep = ':';
#endif
std::string path;
if (pypath) {
path += pypath;
}
if (pyhome && *pyhome) {
if (!path.empty()) {
path += path_sep;
}
path += pyhome;
path += "/Lib";
path += path_sep;
path += pyhome;
path += "/Modules";
}
PyObject* sys_path = PySys_GetObject("path"); // borrowed
if (!sys_path || !PyList_Check(sys_path)) {
sys_path = PyList_New(0);
if (sys_path) {
PySys_SetObject("path", sys_path);
Py_DECREF(sys_path);
}
}
if (sys_path && PyList_Check(sys_path)) {
size_t start = 0;
// Preserve empty entries to keep CWD semantics (ex: leading/trailing separators).
while (start <= path.size()) {
size_t end = path.find(path_sep, start);
if (end == std::string::npos) {
end = path.size();
}
std::string entry = path.substr(start, end - start);
PyObject* py_entry = PyUnicode_DecodeFSDefault(entry.c_str());
if (py_entry) {
PyList_Append(sys_path, py_entry);
Py_DECREF(py_entry);
}
start = end + 1;
}
}
}
// Initialize thread-local state tracking to match Python's state
// This must be done before creating any QorePythonProgram instances
#ifdef Py_GIL_DISABLED
// In free-threading mode, ensure main thread state is attached before any Python API calls
mainThreadState = _qore_safe_thread_state_get();
//printd(5, "python_module_init_intern() mainThreadState: %p current: %p\n",
// mainThreadState, PyGILState_GetThisThreadState());
if (!PyGILState_GetThisThreadState()) {
PyThreadState_Swap(mainThreadState);
}
#else
// In GIL mode, PyGILState_GetThisThreadState() might return NULL during early init
// even though we have the GIL. Use _qore_safe_thread_state_get() which tolerates missing TSS.
PyThreadState* init_tstate = PyGILState_GetThisThreadState();
if (!init_tstate) {
// TSS not set up yet - get the actual thread state and set it up
init_tstate = PyThreadState_Get();
// Also update mainThreadState for later use
mainThreadState = init_tstate;
}
_qore_PyGILState_SetThisThreadState(init_tstate);
// We have the GIL at this point after Py_InitializeEx()
_qore_gil_held = true;
#endif
if (init_global_qore_python_pgm()) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "failed to initialize \"python\" module");
}
#ifdef Py_GIL_DISABLED
// In free-threading mode, use PyGILState_Ensure to properly set up the thread for Python ops
// This ensures the mimalloc heap is properly initialized for this thread
PyGILState_STATE gstate = PyGILState_Ensure();
//printd(5, "python_module_init_intern() after PyGILState_Ensure: current: %p gstate: %d\n",
// PyGILState_GetThisThreadState(), (int)gstate);
#endif
if (QorePythonProgram::staticInit() || QorePythonStackLocationHelper::staticInit()) {
#ifdef Py_GIL_DISABLED
PyGILState_Release(gstate);
#endif
throw QoreStandardException("PYTHON-MODULE-ERROR", "failed to initialize \"python\" module");
}
#ifdef Py_GIL_DISABLED
PyGILState_Release(gstate);
//printd(5, "python_module_init_intern() after PyGILState_Release: current: %p\n",
// PyGILState_GetThisThreadState());
#endif
#ifndef Py_GIL_DISABLED
mainThreadState = PyThreadState_Get();
if (python_initialized) {
#if PY_VERSION_HEX >= 0x030D0000
// Python 3.13+ changed thread state management significantly
// Use PyEval_ReleaseThread which properly clears both TSS and fast TLS
printd(5, "python_module_init: before release, mainThreadState: %p TSS: %p GIL: %d\n",
mainThreadState, PyGILState_GetThisThreadState(), PyGILState_Check());
PyEval_ReleaseThread(mainThreadState);
printd(5, "python_module_init: after release, TSS: %p GIL: %d\n",
PyGILState_GetThisThreadState(), PyGILState_Check());
_qore_PyGILState_SetThisThreadState(nullptr);
#else
// release the current thread state after initialization
_qore_release_thread_state(mainThreadState);
// Our tracking should be cleared by _qore_release_thread_state
assert(!_qore_PyRuntimeGILState_GetThreadState());
_qore_PyGILState_SetThisThreadState(nullptr);
// NOTE: In Python 3.12, PyEval_ReleaseThread does NOT clear PyGILState_GetThisThreadState()
// because it doesn't update the autoTSSkey. This is different from earlier Python versions.
// We only check haveGil() which uses our own tracking.
assert(!QorePythonProgram::haveGil());
#endif
}
#else
// In free-threading mode, don't release the thread state after initialization
// We keep the main thread state attached for Python operations
#endif
if (!repeat) {
tclist.push(QorePythonProgram::pythonThreadCleanup, nullptr);
}
return nullptr;
}
static void python_module_ns_init(QoreNamespace* rns, QoreNamespace* qns, ExceptionSink& xsink) {
QoreProgram* pgm = getProgram();
assert(pgm->getRootNS() == rns);
if (!pgm->getExternalData(QORE_PYTHON_MODULE_NAME)) {
QoreNamespace* pyns = PNS->copy();
rns->addNamespace(pyns);
// NOTE: Use QoreProgramContextHelper instead of QoreExternalProgramContextHelper
// because QoreExternalProgramContextHelper uses runtime=true which triggers
// doTopLevelInstantiation(), setting tlpd->inst = true. This happens before the
// user's top-level local variables are defined (during %requires processing), so
// no variables are actually instantiated. When the helper destructs, tlpd->inst
// stays true even though no variables were instantiated. Later when runTopLevel()
// is called, it skips doTopLevelInstantiation because tlpd->inst is true, causing
// crashes when accessing top-level local variables defined after %requires python.
// QoreProgramContextHelper just sets the current program without triggering
// thread-local variable instantiation.
//
// Exception handling note: QoreProgramContextHelper doesn't use ExceptionSink because
// it only manages program context (save/restore), which doesn't throw. The
// QorePythonProgram constructor handles its own exceptions internally - any Python
// initialization errors are logged and handled within the constructor. This differs
// from QoreExternalProgramContextHelper which needed ExceptionSink for its runtime
// thread-local operations, not for the external data setup itself.
QoreProgramContextHelper pch(pgm);
pgm->setExternalData(QORE_PYTHON_MODULE_NAME, new QorePythonProgram(pgm, pyns));
}
#ifndef Py_GIL_DISABLED
#if PY_VERSION_HEX < 0x030C0000
// In Python 3.12+, PyGILState_Check() behavior changed - it returns 1 even after
// releasing the GIL because PyEval_ReleaseThread doesn't clear the TSS.
// In Python 3.13+, sub-interpreters also affect this behavior.
assert(!python_initialized || !PyGILState_Check());
#endif
// haveGil() uses our own tracking which should be accurate
assert(!python_initialized || !QorePythonProgram::haveGil());
#endif
}
static void python_module_delete() {
if (qore_python_pgm) {
qore_python_pgm->doDeref();
qore_python_pgm = nullptr;
}
if (PNS) {
delete PNS;
PNS = nullptr;
}
python_module_shutdown();
}
static void python_module_parse_cmd(const QoreString& cmd, ExceptionSink* xsink) {
//printd(5, "python_module_parse_cmd() cmd: '%s'\n", cmd.c_str());
const char* p = strchr(cmd.c_str(), ' ');
QoreString str;
QoreString arg;
if (p) {
QoreString nstr(&cmd, p - cmd.c_str());
str = nstr;
arg = cmd;
arg.replace(0, p - cmd.c_str() + 1, (const char*)nullptr);
arg.trim();
} else {
str = cmd;
str.trim();
}
mcmap_t::const_iterator i = mcmap.find(str.c_str());
if (i == mcmap.end()) {
QoreStringNode* desc = new QoreStringNodeMaker("unrecognized command '%s' in '%s' (valid commands: ", str.c_str(), cmd.c_str());
for (mcmap_t::const_iterator i = mcmap.begin(), e = mcmap.end(); i != e; ++i) {
if (i != mcmap.begin())
desc->concat(", ");
desc->sprintf("'%s'", i->first.c_str());
}
desc->concat(')');
xsink->raiseException("PYTHON-PARSE-COMMAND-ERROR", desc);
return;
}
if (i->second.requires_arg) {
if (arg.empty()) {
xsink->raiseException("PYTHON-PARSE-COMMAND-ERROR", "missing argument / command name in parse command: '%s'", cmd.c_str());
return;
}
} else {
if (!arg.empty()) {
xsink->raiseException("PYTHON-PARSE-COMMAND-ERROR", "extra argument / command name in parse command: '%s'", cmd.c_str());
return;
}
}
QoreProgram* pgm = getProgram();
QorePythonProgram* pypgm = static_cast<QorePythonProgram*>(pgm->getExternalData(QORE_PYTHON_MODULE_NAME));
//printd(5, "parse-cmd '%s' pypgm: %p pythonns: %p\n", arg.c_str(), pypgm, pypgm->getPythonNamespace());
if (!pypgm) {
QoreNamespace* pyns = PNS->copy();
pgm->getRootNS()->addNamespace(pyns);
pypgm = new QorePythonProgram(pgm, pyns);
pgm->setExternalData(QORE_PYTHON_MODULE_NAME, pypgm);
pgm->addFeature(QORE_PYTHON_MODULE_NAME);
}
i->second.cmd(xsink, arg, pypgm);
}
// %module-cmd(python) import
static void py_mc_import(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
// process import statement
//printd(5, "py_mc_import() pypgm: %p arg: %s\n", pypgm, arg.c_str());
QorePythonHelper qph(pypgm, xsink);
if (qph.wasInterrupted()) {
return;
}
// see if there is a dot (.) in the name
qore_offset_t i = arg.find('.');
if (i < 0 || i == static_cast<qore_offset_t>(arg.size() - 1)) {
pypgm->import(xsink, arg.c_str());
return;
}
const char* symbol = arg.c_str() + i + 1;
arg.replaceChar(i, '\0');
arg.terminate(i);
if (!strcmp(symbol, "*")) {
pypgm->import(xsink, arg.c_str());
return;
}
pypgm->import(xsink, arg.c_str(), symbol);
}
// %module-cmd(python) import-ns <qore-namespace> <python-module-path>
static void py_mc_import_ns(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
// find end of qore namespace
qore_offset_t end = arg.find(' ');
if (end == -1) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "syntax: import-ns <qore-namespace> " \
"<python-module-path>: missing python module path argument; value given: '%s'", arg.c_str());
}
QoreString qore_ns(&arg, end);
QoreString py_mod_path(arg.c_str() + end + 1);
QoreProgram* pgm = getProgram();
if (!pgm) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "import-ns error: no current Program context");
}
QoreNamespace* ns = pgm->findNamespace(qore_ns);
if (!ns || ns == pgm->getRootNS()) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "import-ns error: Qore namespace '%s' not found",
qore_ns.c_str());
}
pypgm->importQoreNamespaceToPython(*ns, py_mod_path, xsink);
}
// %module-cmd(python) alias <python-source-path> <python-target-path>
static void py_mc_alias(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
// find end of qore namespace
qore_offset_t end = arg.find(' ');
if (end == -1 || (size_t)end == (arg.size() - 1)) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "syntax: alias <python-source-path> " \
"<python-target-path: python target path argument; value given: '%s'", arg.c_str());
}
QoreString source_path(&arg, end);
QoreString target_path(arg.c_str() + end + 1);
pypgm->aliasDefinition(source_path, target_path);
}
// %module-cmd(python) parse <label> <source code>
static void py_mc_parse(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
// find end of qore namespace
qore_offset_t end = arg.find(' ');
if (end == -1 || (size_t)end == (arg.size() - 1)) {
throw QoreStandardException("PYTHON-MODULE-ERROR", "syntax: alias <python-source-path> " \
"<python-target-path: python target path argument; value given: '%s'", arg.c_str());
}
QoreString source_label(&arg, end);
QoreString source_code(arg.c_str() + end + 1);
ValueHolder val(pypgm->eval(xsink, source_code, source_label, Py_file_input, false), xsink);
}
// %module-cmd(python) export-class <python path>
/** export a Python class to Qore
*/
static void py_mc_export_class(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
pypgm->exportClass(xsink, arg);
}
// %module-cmd(python) export-func <python path>
/** export a Python function to Qore
*/
static void py_mc_export_func(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
pypgm->exportFunction(xsink, arg);
}
// %module-cmd(python) add-module-path <fs path>
/** add a path to the module path
*/
static void py_mc_add_module_path(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
pypgm->addModulePath(xsink, arg);
}
#if 0
// %module-cmd(python) reset-python
static void py_mc_reset_python(ExceptionSink* xsink, QoreString& arg, QorePythonProgram* pypgm) {
q_reset_python(xsink);
}
#endif
// exported function
extern "C" int python_module_import(ExceptionSink* xsink, QoreProgram* pgm, const char* module, const char* symbol) {
QorePythonProgram* pypgm = static_cast<QorePythonProgram*>(pgm->getExternalData(QORE_PYTHON_MODULE_NAME));
if (!pypgm) {
QoreNamespace* pyns = PNS->copy();
pgm->getRootNS()->addNamespace(pyns);
pypgm = new QorePythonProgram(pgm, pyns);
pgm->setExternalData(QORE_PYTHON_MODULE_NAME, pypgm);
pgm->addFeature(QORE_PYTHON_MODULE_NAME);
}
// the following call adds the class to the current program as well
QorePythonHelper qph(pypgm, xsink);
if (qph.wasInterrupted()) {
return -1;
}
return pypgm->import(xsink, module, symbol);
}
QorePythonHelper::QorePythonHelper(const QorePythonProgram* pypgm, ExceptionSink* xsink)
: old_pgm(q_swap_thread_local_data(python_u_tld_key, (void*)pypgm)),
old_state(pypgm->setContext(xsink != nullptr)), new_pypgm(pypgm) {
//printd(5, "QorePythonHelper::QorePythonHelper() new: %p old: %p\n", pypgm, old_pgm);
if (xsink) {
if (wasInterrupted()) {
xsink->raiseException("PROGRAM-INTERRUPTED",
"program execution was interrupted while acquiring the Python GIL");
} else if (!old_state.valid && !new_pypgm->isValid()) {
// The interpreter has been deleted - raise an exception
xsink->raiseException("PYTHON-INTERPRETER-DELETED",
"cannot execute Python callable: the Python interpreter has been deleted");
}
}
}
QorePythonHelper::~QorePythonHelper() {
new_pypgm->releaseContext(old_state);
q_swap_thread_local_data(python_u_tld_key, (void*)old_pgm);
}
bool QorePythonHelper::isValid() const {
return old_state.valid || !new_pypgm->isValid();
}
bool QorePythonHelper::wasInterrupted() const {
return !old_state.valid && new_pypgm->isValid();
}
bool _qore_has_gil(PyThreadState* t_state) {
if (!_qore_PyCeval_GetGilLockedStatus()) {
return false;
}
#if PY_VERSION_HEX < 0x030C0000
// In Python < 3.12, PyGILState_Check() is reliable. If it says we have the GIL,
// we have it even if t_state is NULL (can happen during early init before TSS is set).
if (t_state == nullptr && PyGILState_Check()) {
return true;
}
#endif
return _qore_PyCeval_GetThreadState() == t_state;
}
static bool _qore_has_gil(PyThreadState* state0, PyThreadState* state1) {
if (!_qore_PyCeval_GetGilLockedStatus()) {
return false;
}
#if PY_VERSION_HEX < 0x030C0000
// In Python < 3.12, PyGILState_Check() is reliable. If it says we have the GIL,
// we have it even if both states are NULL (can happen during early init).
if (state0 == nullptr && state1 == nullptr && PyGILState_Check()) {
return true;
}
#endif
PyThreadState* gs = _qore_PyCeval_GetThreadState();
return gs == state0 || gs == state1;
}
QorePythonGilHelper::QorePythonGilHelper(PyThreadState* new_thread_state)
: new_thread_state(new_thread_state), state(nullptr), t_state(nullptr), release_gil(true) {
// CRITICAL: Check if Python is shutting down or not initialized before ANY Python API calls.
// After Py_FinalizeEx(), calling PyGILState_Ensure() will crash because the TSS points
// to a stale thread state with a freed interpreter.
if (python_shutdown || !Py_IsInitialized()) {
printd(5, "QorePythonGilHelper ctor: Python is shutting down, skipping initialization\n");
return;
}
assert(new_thread_state);
// Now safe to call Python APIs
state = _qore_PyRuntimeGILState_GetThreadState();
t_state = PyGILState_GetThisThreadState();
release_gil = !_qore_has_gil(t_state, new_thread_state);
#ifdef Py_GIL_DISABLED
// In free-threading mode, use PyGILState_Ensure to properly
// initialize the thread and ensure a valid thread state is attached.
gstate = PyGILState_Ensure();
// Now we have a thread state attached
t_state = PyGILState_GetThisThreadState();
if (t_state != new_thread_state) {
PyThreadState_Swap(new_thread_state);
}
_qore_PyGILState_SetThisThreadState(new_thread_state);
_QORE_GILSTATE_COUNTER_INC(new_thread_state);
#elif PY_VERSION_HEX >= 0x030D0000
// Python 3.13+ GIL mode - use PyEval_AcquireThread/ReleaseThread which properly
// handle TSS and fast TLS synchronization
if (release_gil) {
// If bound_gilstate is stale, clear it to avoid tstate_activate assertions.
if (qore_py_get_bound_gilstate(new_thread_state)
&& PyGILState_GetThisThreadState() != new_thread_state) {
qore_py_set_bound_gilstate(new_thread_state, 0);
}
// Need to acquire the GIL with our specific thread state
// CRITICAL: If there's a stale TSS from a previous interpreter, we must clear it first.
// PyEval_AcquireThread -> _PyThreadState_Attach tries to detach any existing thread state,
// and if the TSS doesn't match, it will assert.
PyEval_AcquireThread(new_thread_state);
} else {
// Already have the GIL - swap to our thread state if needed
if (t_state != new_thread_state) {
printd(5, "QorePythonGilHelper ctor: swapping from %p to %p\n", t_state, new_thread_state);
PyThreadState_Swap(new_thread_state);
}
}
// CRITICAL: Update our tracking AFTER acquiring the GIL.
// _qore_gil_held must be set for _qore_PyCeval_GetGilLockedStatus() to return true.
_qore_gil_held = true;
_QORE_GILSTATE_COUNTER_INC(new_thread_state);
_qore_PyGILState_SetThisThreadState(new_thread_state);
#else
if (release_gil) {
#if PY_VERSION_HEX >= 0x030C0000
// Ensure bound_gilstate and TSS are consistent before acquiring the GIL in Python 3.12.
PyThreadState* tss_before = PyGILState_GetThisThreadState();
if (qore_py_get_bound_gilstate(new_thread_state) && tss_before != new_thread_state) {
_qore_PyGILState_SetTSS(new_thread_state);
PyThreadState* tss_after = PyGILState_GetThisThreadState();
if (tss_after != new_thread_state) {
_qore_PyGILState_ClearTSS();
qore_py_set_bound_gilstate(new_thread_state, 0);
} else {
qore_py_set_bound_gilstate(new_thread_state, 1);
}
}
#endif
_qore_acquire_thread_state(new_thread_state);
// Use _qore_tss_tstate for assertion since Python's TSS (PyGILState_GetThisThreadState)
// might be stale from a deleted interpreter in Python 3.12
assert(_qore_tss_tstate == new_thread_state);
} else {
// NOTE: In Python 3.12, t_state (from PyGILState_GetThisThreadState()) may be stale
// after PyEval_ReleaseThread() since Python's TSS isn't cleared.
// _qore_has_gil() verified we have the GIL with either t_state or new_thread_state,
// so check against both possibilities.
PyThreadState* ceval_ts = _qore_PyCeval_GetThreadState();
assert(ceval_ts == t_state || ceval_ts == new_thread_state);
}
// NOTE: even if the current thread state is equal to the new one, we still need to set all thread states in all
// locations
_QORE_GILSTATE_COUNTER_INC(new_thread_state);
_QORE_PYTHREAD_STATE_SWAP(new_thread_state);
// set this thread state
_qore_PyGILState_SetThisThreadState(new_thread_state);
#if PY_VERSION_HEX < 0x030C0000
// NOTE: In Python 3.12+, PyGILState_GetThisThreadState() can be unreliable because
// PyEval_ReleaseThread() doesn't clear the autoTSSkey. Our tracking is authoritative.
assert(PyGILState_GetThisThreadState() == new_thread_state);
assert(PyGILState_Check());
#else
// Python 3.12+: PyGILState_Check() and PyGILState_GetThisThreadState() rely on Python's
// internal TSS (autoTSSkey) which can be stale after PyEval_ReleaseThread() or corrupted
// by external modules like JNI. Our thread-local tracking (_qore_tss_tstate) is reliable.
assert(_qore_PyCeval_GetGilLockedStatus());
#endif
#endif
initialized = true;
}
QorePythonGilHelper::~QorePythonGilHelper() {
// CRITICAL: If Python was shutting down during construction, skip all cleanup
if (!initialized) {
return;
}
#ifdef Py_GIL_DISABLED
if (gstate_released) {
// gstate was released in releaseBeforeSubInterpreter() for sub-interpreter creation.
// The sub-interpreter now owns the thread state - we don't restore or release anything.
// When the sub-interpreter is destroyed, it will clean up its own thread state.
return;
}
// First swap back to the original thread state if needed
PyThreadState* current = PyGILState_GetThisThreadState();
if (current && current != t_state && t_state) {
PyThreadState_Swap(t_state);
}
// Decrement counter before release
_QORE_GILSTATE_COUNTER_DEC(new_thread_state);
// Release the GIL state
PyGILState_Release(gstate);
_qore_PyGILState_SetThisThreadState(nullptr);
#elif PY_VERSION_HEX >= 0x030D0000
// Python 3.13+ GIL mode
printd(5, "QorePythonGilHelper dtor: release_gil: %d t_state: %p new_thread_state: %p\n",
release_gil, t_state, new_thread_state);
_QORE_GILSTATE_COUNTER_DEC(new_thread_state);
if (release_gil) {
// We acquired the GIL, so release it
// Use PyEval_SaveThread which properly releases the GIL
PyEval_SaveThread();
_qore_PyGILState_SetThisThreadState(nullptr);
_qore_gil_held = false; // Track that we no longer hold the GIL
} else {
// We already had the GIL - restore the original tracking
// Keep _qore_gil_held = true since we still have the GIL
_qore_PyGILState_SetThisThreadState(t_state);
}
#else
_QORE_GILSTATE_COUNTER_DEC(new_thread_state);
if (release_gil) {
//printd(5, "QorePythonGilHelper::~QorePythonGilHelper() releasing %llx state: %llx t_state: %llx\n",
// new_thread_state, state, t_state);
// We acquired the GIL with new_thread_state in the constructor.
// During our lifetime, setContext() might have changed the ceval thread state to a different
// PythonProgram's thread state. We need to ensure we release the correct state.
// Swap to new_thread_state before releasing to avoid "wrong thread state" error.
// Use _qore_PyCeval_GetThreadState() instead of PyThreadState_Get() - the latter crashes
// in Python 3.11 if the GIL is not held properly (e.g., after PyInterpreterState_Delete).
PyThreadState* current = _qore_PyCeval_GetThreadState();
if (current != new_thread_state) {
PyThreadState_Swap(new_thread_state);
}
_qore_release_thread_state(new_thread_state);
// _qore_release_thread_state already cleared _qore_tss_tstate to nullptr.
} else {
//printd(5, "QorePythonGilHelper::~QorePythonGilHelper() swapping %llx state: %llx t_state: %llx\n",
// new_thread_state, state, t_state);
// We already had the GIL - restore to original state
_QORE_PYTHREAD_STATE_SWAP(state);
_qore_PyCeval_SwapThreadState(t_state);
// restore the old TLD state - only when we already had the GIL
_qore_PyGILState_SetThisThreadState(t_state);
}
#endif
}
#ifdef Py_GIL_DISABLED
void QorePythonGilHelper::releaseBeforeSubInterpreter() {
// Prepare for sub-interpreter creation in free-threading mode.
// In free-threading mode with mimalloc, each thread state has thread-local heap data.
// PyGILState_Ensure() set up heap data for the main interpreter. Before creating a
// sub-interpreter, we need to detach so Py_NewInterpreterFromConfig can properly
// initialize heap data for the new interpreter.
// Swap to NULL thread state to detach from main interpreter's thread state.
// This allows Py_NewInterpreterFromConfig to properly attach a new thread state
// with correctly initialized mimalloc heap for the sub-interpreter.
PyThreadState_Swap(nullptr);
gstate_released = true;
}
#endif
void QorePythonGilHelper::set(PyThreadState* other_state) {
// This function is called after Py_NewInterpreter() to switch from the main interpreter's
// thread state (which was used to acquire the GIL in the constructor) to the new sub-interpreter's
// thread state.
//
// GIL State Counter Balancing:
// - Constructor incremented the MAIN interpreter's gilstate_counter
// - Destructor will decrement new_thread_state's gilstate_counter
// - Since we're switching new_thread_state to point to other_state (sub-interpreter),
// we must:
// 1. Decrement the MAIN interpreter's counter to balance the constructor's increment
// 2. Increment the SUB interpreter's counter to balance the destructor's decrement
//
// Without step 1, the main thread state's counter would leak (+1 for each sub-interpreter
// creation), potentially breaking later GIL bookkeeping assertions.
// Without step 2, Py_NewInterpreter's thread state (which starts with gilstate_counter=1)
// would be decremented to 0, causing PyEval_RestoreThread to fail its assertion.
//
// See design/python-module.md for detailed explanation.
#ifdef Py_GIL_DISABLED
// In free-threading mode, Py_NewInterpreterFromConfig already attached the new thread state.
// Just update our tracking variable so the destructor restores correctly.
// Do NOT call PyThreadState_Swap again as it may corrupt the thread's heap state.
// gilstate_counter balancing is not needed in free-threading mode.
new_thread_state = other_state;
#else
assert(_qore_PyCeval_GetGilLockedStatus() && _qore_PyCeval_GetThreadState());
// CRITICAL: Decrement the main thread state's counter to balance the increment in the
// constructor. This must be done BEFORE changing new_thread_state.
_QORE_GILSTATE_COUNTER_DEC(new_thread_state);
// Update new_thread_state so the destructor releases the correct thread state
new_thread_state = other_state;
// CRITICAL: Increment the new thread state's gilstate_counter to balance the decrement
// in the destructor.
_QORE_GILSTATE_COUNTER_INC(other_state);
_QORE_PYTHREAD_STATE_SWAP(other_state);
_qore_PyCeval_SwapThreadState(other_state);
_qore_PyGILState_SetThisThreadState(other_state);
#endif
}