Skip to content

Commit c67121a

Browse files
authored
gh-105145: Deprecate Py_GetPath() function (#105179)
Deprecate old Python initialization functions: * PySys_ResetWarnOptions() * Py_GetExecPrefix() * Py_GetPath() * Py_GetPrefix() * Py_GetProgramFullPath() * Py_GetProgramName() * Py_GetPythonHome() _tkinter.c uses sys.executable instead of Py_GetProgramName() and uses sys.prefix instead of Py_GetPrefix().
1 parent ec0082c commit c67121a

File tree

7 files changed

+57
-12
lines changed

7 files changed

+57
-12
lines changed

Doc/c-api/init.rst

+19
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ Process-wide parameters
429429
.. versionchanged:: 3.10
430430
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
431431

432+
.. deprecated-removed:: 3.13 3.15
433+
Get :data:`sys.executable` instead.
434+
432435

433436
.. c:function:: wchar_t* Py_GetPrefix()
434437
@@ -448,6 +451,9 @@ Process-wide parameters
448451
.. versionchanged:: 3.10
449452
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
450453

454+
.. deprecated-removed:: 3.13 3.15
455+
Get :data:`sys.prefix` instead.
456+
451457

452458
.. c:function:: wchar_t* Py_GetExecPrefix()
453459
@@ -489,6 +495,9 @@ Process-wide parameters
489495
.. versionchanged:: 3.10
490496
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
491497

498+
.. deprecated-removed:: 3.13 3.15
499+
Get :data:`sys.exec_prefix` instead.
500+
492501

493502
.. c:function:: wchar_t* Py_GetProgramFullPath()
494503
@@ -507,6 +516,9 @@ Process-wide parameters
507516
.. versionchanged:: 3.10
508517
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
509518

519+
.. deprecated-removed:: 3.13 3.15
520+
Get :data:`sys.executable` instead.
521+
510522

511523
.. c:function:: wchar_t* Py_GetPath()
512524
@@ -532,6 +544,9 @@ Process-wide parameters
532544
.. versionchanged:: 3.10
533545
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
534546

547+
.. deprecated-removed:: 3.13 3.15
548+
Get :data:`sys.path` instead.
549+
535550

536551
.. c:function:: const char* Py_GetVersion()
537552
@@ -615,6 +630,10 @@ Process-wide parameters
615630
.. versionchanged:: 3.10
616631
It now returns ``NULL`` if called before :c:func:`Py_Initialize`.
617632
633+
.. deprecated-removed:: 3.13 3.15
634+
Get :c:member:`PyConfig.home` or :envvar:`PYTHONHOME` environment
635+
variable instead.
636+
618637
619638
.. _threads:
620639

Doc/c-api/sys.rst

+3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ accessible to C code. They all work with the current interpreter thread's
237237
Reset :data:`sys.warnoptions` to an empty list. This function may be
238238
called prior to :c:func:`Py_Initialize`.
239239
240+
.. deprecated-removed:: 3.13 3.15
241+
Clear :data:`sys.warnoptions` and :data:`!warnings.filters` instead.
242+
240243
.. c:function:: void PySys_WriteStdout(const char *format, ...)
241244
242245
Write the output string described by *format* to :data:`sys.stdout`. No

Doc/whatsnew/3.13.rst

+14
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,20 @@ Deprecated
357357
``PY_UNICODE_TYPE`` are just aliases to ``wchar_t``.
358358
(Contributed by Victor Stinner in :gh:`105156`.)
359359

360+
* Deprecate old Python initialization functions:
361+
362+
* :c:func:`PySys_ResetWarnOptions`:
363+
clear :data:`sys.warnoptions` and :data:`!warnings.filters` instead.
364+
* :c:func:`Py_GetExecPrefix`: get :data:`sys.exec_prefix` instead.
365+
* :c:func:`Py_GetPath`: get :data:`sys.path` instead.
366+
* :c:func:`Py_GetPrefix`: get :data:`sys.prefix` instead.
367+
* :c:func:`Py_GetProgramFullPath`: get :data:`sys.executable` instead.
368+
* :c:func:`Py_GetProgramName`: get :data:`sys.executable` instead.
369+
* :c:func:`Py_GetPythonHome`: get :c:member:`PyConfig.home` or
370+
:envvar:`PYTHONHOME` environment variable instead.
371+
372+
(Contributed by Victor Stinner in :gh:`105145`.)
373+
360374
Removed
361375
-------
362376

Include/pylifecycle.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ PyAPI_FUNC(int) Py_Main(int argc, wchar_t **argv);
3434
PyAPI_FUNC(int) Py_BytesMain(int argc, char **argv);
3535

3636
/* In pathconfig.c */
37-
PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
38-
PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
39-
PyAPI_FUNC(wchar_t *) Py_GetProgramFullPath(void);
40-
PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
41-
PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
42-
PyAPI_FUNC(wchar_t *) Py_GetPath(void);
37+
Py_DEPRECATED(3.13) PyAPI_FUNC(wchar_t *) Py_GetProgramName(void);
38+
Py_DEPRECATED(3.13) PyAPI_FUNC(wchar_t *) Py_GetPythonHome(void);
39+
Py_DEPRECATED(3.13) PyAPI_FUNC(wchar_t *) Py_GetProgramFullPath(void);
40+
Py_DEPRECATED(3.13) PyAPI_FUNC(wchar_t *) Py_GetPrefix(void);
41+
Py_DEPRECATED(3.13) PyAPI_FUNC(wchar_t *) Py_GetExecPrefix(void);
42+
Py_DEPRECATED(3.13) PyAPI_FUNC(wchar_t *) Py_GetPath(void);
4343
#ifdef MS_WINDOWS
4444
int _Py_CheckPython3(void);
4545
#endif

Include/sysmodule.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ PyAPI_FUNC(void) PySys_WriteStderr(const char *format, ...)
1717
PyAPI_FUNC(void) PySys_FormatStdout(const char *format, ...);
1818
PyAPI_FUNC(void) PySys_FormatStderr(const char *format, ...);
1919

20-
PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
20+
Py_DEPRECATED(3.13) PyAPI_FUNC(void) PySys_ResetWarnOptions(void);
2121

2222
PyAPI_FUNC(PyObject *) PySys_GetXOptions(void);
2323

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Deprecate old Python initialization functions:
2+
3+
* :c:func:`PySys_ResetWarnOptions`
4+
* :c:func:`Py_GetExecPrefix`
5+
* :c:func:`Py_GetPath`
6+
* :c:func:`Py_GetPrefix`
7+
* :c:func:`Py_GetProgramFullPath`
8+
* :c:func:`Py_GetProgramName`
9+
* :c:func:`Py_GetPythonHome`
10+
11+
Patch by Victor Stinner.

Modules/_tkinter.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,10 @@ _get_tcl_lib_path(void)
130130
static int already_checked = 0;
131131

132132
if (already_checked == 0) {
133-
PyObject *prefix;
134133
struct stat stat_buf;
135134
int stat_return_value;
136135

137-
prefix = PyUnicode_FromWideChar(Py_GetPrefix(), -1);
136+
PyObject *prefix = PySys_GetObject("prefix"); // borrowed reference
138137
if (prefix == NULL) {
139138
return NULL;
140139
}
@@ -3289,8 +3288,8 @@ PyInit__tkinter(void)
32893288

32903289
/* This helps the dynamic loader; in Unicode aware Tcl versions
32913290
it also helps Tcl find its encodings. */
3292-
uexe = PyUnicode_FromWideChar(Py_GetProgramName(), -1);
3293-
if (uexe) {
3291+
uexe = PySys_GetObject("executable"); // borrowed reference
3292+
if (uexe && PyUnicode_Check(uexe)) { // sys.executable can be None
32943293
cexe = PyUnicode_EncodeFSDefault(uexe);
32953294
if (cexe) {
32963295
#ifdef MS_WINDOWS
@@ -3329,7 +3328,6 @@ PyInit__tkinter(void)
33293328
#endif /* MS_WINDOWS */
33303329
}
33313330
Py_XDECREF(cexe);
3332-
Py_DECREF(uexe);
33333331
}
33343332

33353333
if (PyErr_Occurred()) {

0 commit comments

Comments
 (0)