@@ -77,10 +77,7 @@ The following functions can be safely called before Python is initialized:
77
77
78
78
Despite their apparent similarity to some of the functions listed above,
79
79
the following functions **should not be called ** before the interpreter has
80
- been initialized::c:func: `Py_EncodeLocale `,:c:func: `Py_GetPath `,
81
- :c:func: `Py_GetPrefix `,:c:func: `Py_GetExecPrefix `,
82
- :c:func: `Py_GetProgramFullPath `,:c:func: `Py_GetPythonHome `,
83
- :c:func: `Py_GetProgramName `,:c:func: `PyEval_InitThreads `, and
80
+ been initialized::c:func: `Py_EncodeLocale `,:c:func: `PyEval_InitThreads `, and
84
81
:c:func: `Py_RunMain `.
85
82
86
83
@@ -145,9 +142,6 @@ to 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2.
145
142
:c:member: `PyConfig.pathconfig_warnings ` should be used instead, see
146
143
:ref: `Python Initialization Configuration <init-config >`.
147
144
148
- Suppress error messages when calculating the module search path in
149
- :c:func: `Py_GetPath `.
150
-
151
145
Private flag used by ``_freeze_module `` and ``frozenmain `` programs.
152
146
153
147
..deprecated-removed ::3.12 3.15
@@ -203,7 +197,7 @@ to 1 and ``-bb`` sets :c:data:`Py_BytesWarningFlag` to 2.
203
197
204
198
Set by the:option: `-i ` option.
205
199
206
- ..deprecated ::3.12
200
+ ..deprecated-removed ::3.12 3.15
207
201
208
202
..c :var ::int Py_IsolatedFlag
209
203
@@ -586,7 +580,6 @@ Process-wide parameters
586
580
..index ::
587
581
single: Py_Initialize()
588
582
single: main()
589
- single: Py_GetPath()
590
583
591
584
This API is kept for backward compatibility: setting
592
585
:c:member: `PyConfig.program_name ` should be used instead, see:ref: `Python
@@ -596,7 +589,7 @@ Process-wide parameters
596
589
the first time, if it is called at all. It tells the interpreter the value
597
590
of the ``argv[0] `` argument to the:c:func: `main ` function of the program
598
591
(converted to wide characters).
599
- This is used by:c:func:`Py_GetPath` and some other functions below to find
592
+ This is used by some other functions below to find
600
593
the Python run-time libraries relative to the interpreter executable. The
601
594
default value is ``'python'``. The argument should point to a
602
595
zero-terminated wide character string in static storage whose contents will not
@@ -609,146 +602,6 @@ Process-wide parameters
609
602
.. deprecated-removed:: 3.11 3.15
610
603
611
604
612
- .. c:function:: wchar_t* Py_GetProgramName()
613
-
614
- Return the program name set with :c:member:`PyConfig.program_name`, or the default.
615
- The returned string points into static storage; the caller should not modify its
616
- value.
617
-
618
- This function should not be called before:c:func: `Py_Initialize `, otherwise
619
- it returns ``NULL ``.
620
-
621
- ..versionchanged ::3.10
622
- It now returns ``NULL `` if called before:c:func: `Py_Initialize `.
623
-
624
- ..deprecated-removed ::3.13 3.15
625
- Use:c:func: `PyConfig_Get("executable") <PyConfig_Get> `
626
- (:data: `sys.executable `) instead.
627
-
628
-
629
- .. c:function:: wchar_t* Py_GetPrefix()
630
-
631
- Return the *prefix* for installed platform-independent files. This is derived
632
- through a number of complicated rules from the program name set with
633
- :c:member:`PyConfig.program_name` and some environment variables;for example,if the
634
- program name is ``'/usr/local/bin/python' ``, the prefix is ``'/usr/local' ``. The
635
- returned string points into static storage; the caller should not modify its
636
- value. This corresponds to the:makevar: `prefix ` variable in the top-level
637
- :file: `Makefile ` and the:option: `--prefix ` argument to the:program: `configure `
638
- script at build time. The value is available to Python code as ``sys.base_prefix ``.
639
- It is only useful on Unix. See also the next function.
640
-
641
- This function should not be called before:c:func: `Py_Initialize `, otherwise
642
- it returns ``NULL ``.
643
-
644
- ..versionchanged ::3.10
645
- It now returns ``NULL `` if called before:c:func: `Py_Initialize `.
646
-
647
- ..deprecated-removed ::3.13 3.15
648
- Use:c:func: `PyConfig_Get("base_prefix") <PyConfig_Get> `
649
- (:data: `sys.base_prefix `) instead. Use :c:func:`PyConfig_Get("prefix")
650
- <PyConfig_Get>` (:data: `sys.prefix `) if :ref:`virtual environments
651
- <venv-def>` need to be handled.
652
-
653
-
654
- .. c:function:: wchar_t* Py_GetExecPrefix()
655
-
656
- Return the *exec-prefix* for installed platform-*dependent* files. This is
657
- derived through a number of complicated rules from the program name set with
658
- :c:member:`PyConfig.program_name` and some environment variables;for example,if the
659
- program name is ``'/usr/local/bin/python' ``, the exec-prefix is
660
- ``'/usr/local' ``. The returned string points into static storage; the caller
661
- should not modify its value. This corresponds to the:makevar: `exec_prefix `
662
- variable in the top-level:file: `Makefile ` and the ``--exec-prefix ``
663
- argument to the:program: `configure ` script at build time. The value is
664
- available to Python code as ``sys.base_exec_prefix ``. It is only useful on
665
- Unix.
666
-
667
- Background: The exec-prefix differs from the prefix when platform dependent
668
- files (such as executables and shared libraries) are installed in a different
669
- directory tree. In a typical installation, platform dependent files may be
670
- installed in the :file:`/usr/local/plat` subtree while platform independent may
671
- be installed in :file:`/usr/local`.
672
-
673
- Generally speaking, a platform is a combination of hardware and software
674
- families, e.g. Sparc machines running the Solaris 2.x operating system are
675
- considered the same platform, but Intel machines running Solaris 2.x are another
676
- platform, and Intel machines running Linux are yet another platform. Different
677
- major revisions of the same operating system generally also form different
678
- platforms. Non-Unix operating systems are a different story; the installation
679
- strategies on those systems are so different that the prefix and exec-prefix are
680
- meaningless, and set to the empty string. Note that compiled Python bytecode
681
- files are platform independent (but not independent from the Python version by
682
- which they were compiled!).
683
-
684
- System administrators will know how to configure the:program: `mount ` or
685
- :program: `automount ` programs to share:file: `/usr/local ` between platforms
686
- while having:file: `/usr/local/plat ` be a different filesystem for each
687
- platform.
688
-
689
- This function should not be called before:c:func: `Py_Initialize `, otherwise
690
- it returns ``NULL ``.
691
-
692
- ..versionchanged ::3.10
693
- It now returns ``NULL `` if called before:c:func: `Py_Initialize `.
694
-
695
- ..deprecated-removed ::3.13 3.15
696
- Use:c:func: `PyConfig_Get("base_exec_prefix") <PyConfig_Get> `
697
- (:data: `sys.base_exec_prefix `) instead. Use
698
- :c:func: `PyConfig_Get("exec_prefix") <PyConfig_Get> `
699
- (:data: `sys.exec_prefix `) if:ref: `virtual environments <venv-def >` need
700
- to be handled.
701
-
702
- ..c :function ::wchar_t *Py_GetProgramFullPath ()
703
-
704
- .. index::
705
- single: executable (in module sys)
706
-
707
- Return the full program name of the Python executable; this is computed as a
708
- side-effect of deriving the default module search path from the program name
709
- (set by:c:member: `PyConfig.program_name `). The returned string points into
710
- static storage; the caller should not modify its value. The value is available
711
- to Python code as ``sys.executable ``.
712
-
713
- This function should not be called before:c:func: `Py_Initialize `, otherwise
714
- it returns ``NULL ``.
715
-
716
- ..versionchanged ::3.10
717
- It now returns ``NULL `` if called before:c:func: `Py_Initialize `.
718
-
719
- ..deprecated-removed ::3.13 3.15
720
- Use:c:func: `PyConfig_Get("executable") <PyConfig_Get> `
721
- (:data: `sys.executable `) instead.
722
-
723
-
724
- ..c :function ::wchar_t *Py_GetPath ()
725
-
726
- .. index::
727
- triple: module; search; path
728
- single: path (in module sys)
729
-
730
- Return the default module search path; this is computed from the program name
731
- (set by:c:member: `PyConfig.program_name `) and some environment variables.
732
- The returned string consists of a series of directory names separated by a
733
- platform dependent delimiter character. The delimiter character is ``':' ``
734
- on Unix and macOS, ``';' `` on Windows. The returned string points into
735
- static storage; the caller should not modify its value. The list
736
- :data: `sys.path ` is initialized with this value on interpreter startup; it
737
- can be (and usually is) modified later to change the search path for loading
738
- modules.
739
-
740
- This function should not be called before:c:func: `Py_Initialize `, otherwise
741
- it returns ``NULL ``.
742
-
743
- .. XXX should give the exact rules
744
-
745
- ..versionchanged ::3.10
746
- It now returns ``NULL `` if called before:c:func: `Py_Initialize `.
747
-
748
- ..deprecated-removed ::3.13 3.15
749
- Use:c:func: `PyConfig_Get("module_search_paths") <PyConfig_Get> `
750
- (:data: `sys.path `) instead.
751
-
752
605
.. c:function::const char* Py_GetVersion()
753
606
754
607
Return the version of this Python interpreter. This is a string that looks
@@ -919,23 +772,6 @@ Process-wide parameters
919
772
.. deprecated-removed:: 3.11 3.15
920
773
921
774
922
- .. c:function:: wchar_t* Py_GetPythonHome()
923
-
924
- Return the default" home" , that is, the value set by
925
- :c:member:`PyConfig.home`, or the value of the :envvar:`PYTHONHOME`
926
- environment variable if it is set.
927
-
928
- This function should not be called before :c:func:`Py_Initialize`, otherwise
929
- it returns ``NULL``.
930
-
931
- .. versionchanged:: 3.10
932
- It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
933
-
934
- .. deprecated-removed:: 3.13 3.15
935
- Use :c:func:`PyConfig_Get(" home" ) <PyConfig_Get>` or the
936
- :envvar:`PYTHONHOME` environment variable instead.
937
-
938
-
939
775
.. _threads:
940
776
941
777
Thread State and the Global Interpreter Lock
@@ -1083,8 +919,36 @@ Note that the ``PyGILState_*`` functions assume there is only one global
1083
919
interpreter (created automatically by :c:func:`Py_Initialize`). Python
1084
920
supports the creation of additional interpreters (using
1085
921
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
1086
- ``PyGILState_*`` API is unsupported.
922
+ ``PyGILState_*`` API is unsupported. This is because :c:func:`PyGILState_Ensure`
923
+ and similar functions default to :term:`attaching <attached thread state>` a
924
+ :term:`thread state` for the main interpreter, meaning that the thread can't safely
925
+ interact with the calling subinterpreter.
926
+
927
+ Supporting subinterpreters in non-Python threads
928
+ ------------------------------------------------
929
+
930
+ If you would like to support subinterpreters with non-Python created threads, you
931
+ must use the ``PyThreadState_*`` API instead of the traditional ``PyGILState_*``
932
+ API.
933
+
934
+ In particular, you must store the interpreter state from the calling
935
+ function and pass it to :c:func:`PyThreadState_New`, which will ensure that
936
+ the :term:`thread state` is targeting the correct interpreter::
937
+
938
+ /* The return value of PyInterpreterState_Get() from the
939
+ function that created this thread. */
940
+ PyInterpreterState *interp = ThreadData->interp;
941
+ PyThreadState *tstate = PyThreadState_New(interp);
942
+ PyThreadState_Swap(tstate);
943
+
944
+ /* GIL of the subinterpreter is now held.
945
+ Perform Python actions here. */
946
+ result = CallSomeFunction();
947
+ /* evaluate result or handle exception */
1087
948
949
+ /* Destroy the thread state. No Python API allowed beyond this point. */
950
+ PyThreadState_Clear(tstate);
951
+ PyThreadState_DeleteCurrent();
1088
952
1089
953
.. _fork-and-threads:
1090
954
@@ -1261,6 +1125,10 @@ code, or when embedding the Python interpreter:
1261
1125
.. seealso:
1262
1126
:c:func:`PyEval_ReleaseThread`
1263
1127
1128
+ .. note::
1129
+ Similar to :c:func:`PyGILState_Ensure`, this function will hang the
1130
+ thread if the runtime is finalizing.
1131
+
1264
1132
1265
1133
The following functions use thread-local storage, and are not compatible
1266
1134
with sub-interpreters:
@@ -1287,10 +1155,10 @@ with sub-interpreters:
1287
1155
When the function returns, there will be an :term:`attached thread state`
1288
1156
and the thread will be able to call arbitrary Python code. Failure is a fatal error.
1289
1157
1290
- ..note ::
1291
- Calling this functionfrom a thread when the runtime is finalizingwill
1292
- hang the thread until the programexits, even if the thread was not
1293
- created by Python. Refer to
1158
+ ..warning ::
1159
+ Calling this function when the runtime is finalizingis unsafe. Doing
1160
+ so will either hang the thread until the programends, or fully crash
1161
+ the interpreter in rare cases. Refer to
1294
1162
:ref:`cautions-regarding-runtime-finalization` for more details.
1295
1163
1296
1164
.. versionchanged:: 3.14
@@ -1307,28 +1175,37 @@ with sub-interpreters:
1307
1175
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
1308
1176
:c:func:`PyGILState_Release` on the same thread.
1309
1177
1310
-
1311
1178
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
1312
1179
1313
1180
Get the :term:`attached thread state` for this thread. May return ``NULL`` if no
1314
1181
GILState API has been used on the current thread. Note that the main thread
1315
1182
always has such a thread-state, even if no auto-thread-state call has been
1316
1183
made on the main thread. This is mainly a helper/diagnostic function.
1317
1184
1318
- .. seealso: :c:func:`PyThreadState_Get``
1185
+ .. note::
1186
+ This function does not account for :term:`thread states <thread state>` created
1187
+ by something other than :c:func:`PyGILState_Ensure` (such as :c:func:`PyThreadState_New`).
1188
+ Prefer :c:func:`PyThreadState_Get` or :c:func:`PyThreadState_GetUnchecked`
1189
+ for most cases.
1319
1190
1191
+ .. seealso: :c:func:`PyThreadState_Get``
1320
1192
1321
1193
.. c:function:: int PyGILState_Check()
1322
1194
1323
1195
Return ``1`` if the current thread is holding the :term:`GIL` and ``0`` otherwise.
1324
1196
This function can be called from any thread at any time.
1325
- Only if it has had itsPython thread stateinitialized and currently is
1326
- holding the :term:`GIL ` will it return ``1``.
1197
+ Only if it has had its:term:` thread state<attached thread state>` initialized
1198
+ via :c:func:`PyGILState_Ensure ` will it return ``1``.
1327
1199
This is mainly a helper/diagnostic function. It can be useful
1328
1200
for example in callback contexts or memory allocation functions when
1329
1201
knowing that the :term:`GIL` is locked can allow the caller to perform sensitive
1330
1202
actions or otherwise behave differently.
1331
1203
1204
+ .. note::
1205
+ If the current Python process has ever created a subinterpreter, this
1206
+ function will *always* return ``1``. Prefer :c:func:`PyThreadState_GetUnchecked`
1207
+ for most cases.
1208
+
1332
1209
.. versionadded:: 3.4
1333
1210
1334
1211