Skip to content

Commit 4446f7d

Browse files
Convert to UTF-8 prior to setting Tkinter path (#425)
## Summary In #421, @carljm pointed out that `path` was uninitialized in `setenv("TCL_LIBRARY", path, 1);`. It turns out we need to set it via `PyUnicode_AsUTF8`. I confirmed that the existing code _was_ running; it's just that`setenv` returns an error (rather than crashing), and we weren't checking the result (though the same is true in the Windows path). If you, e.g., try to `printf` `path` just before, you get a segmentation fault. This code isn't necessary to fix the motivating Tkinter limitation, so that's another reason it wasn't caught.
1 parent ed051a7 commit 4446f7d

5 files changed

+35
-13
lines changed

cpython-unix/patch-tkinter-3.10.patch

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
2-
index 2a3e65b6c97..b17c5bfd6b1 100644
2+
index 2a3e65b6c97..04f2ab0ea10 100644
33
--- a/Modules/_tkinter.c
44
+++ b/Modules/_tkinter.c
55
@@ -115,6 +115,7 @@ Copyright (C) 1994 Steen Lumholt.
@@ -82,7 +82,7 @@ index 2a3e65b6c97..b17c5bfd6b1 100644
8282
str_path = _get_tcl_lib_path();
8383
if (str_path == NULL && PyErr_Occurred()) {
8484
return NULL;
85-
@@ -3628,7 +3674,27 @@ PyInit__tkinter(void)
85+
@@ -3628,7 +3674,32 @@ PyInit__tkinter(void)
8686
PyMem_Free(wcs_path);
8787
}
8888
#else
@@ -97,6 +97,11 @@ index 2a3e65b6c97..b17c5bfd6b1 100644
9797
+ return NULL;
9898
+ }
9999
+ if (str_path != NULL) {
100+
+ path = PyUnicode_AsUTF8(str_path);
101+
+ if (path == NULL) {
102+
+ Py_DECREF(m);
103+
+ return NULL;
104+
+ }
100105
+ setenv("TCL_LIBRARY", path, 1);
101106
+ set_var = 1;
102107
+ }

cpython-unix/patch-tkinter-3.11.patch

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
2-
index 005036d3ff2..c130ed7b186 100644
2+
index 005036d3ff2..0e64706584a 100644
33
--- a/Modules/_tkinter.c
44
+++ b/Modules/_tkinter.c
55
@@ -28,9 +28,7 @@ Copyright (C) 1994 Steen Lumholt.
@@ -93,7 +93,7 @@ index 005036d3ff2..c130ed7b186 100644
9393
str_path = _get_tcl_lib_path();
9494
if (str_path == NULL && PyErr_Occurred()) {
9595
return NULL;
96-
@@ -3428,7 +3472,27 @@ PyInit__tkinter(void)
96+
@@ -3428,7 +3472,32 @@ PyInit__tkinter(void)
9797
PyMem_Free(wcs_path);
9898
}
9999
#else
@@ -108,6 +108,11 @@ index 005036d3ff2..c130ed7b186 100644
108108
+ return NULL;
109109
+ }
110110
+ if (str_path != NULL) {
111+
+ path = PyUnicode_AsUTF8(str_path);
112+
+ if (path == NULL) {
113+
+ Py_DECREF(m);
114+
+ return NULL;
115+
+ }
111116
+ setenv("TCL_LIBRARY", path, 1);
112117
+ set_var = 1;
113118
+ }

cpython-unix/patch-tkinter-3.12.patch

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
2-
index 6b5fcb8a365..99d44ccf1d8 100644
2+
index 6b5fcb8a365..7b196f40166 100644
33
--- a/Modules/_tkinter.c
44
+++ b/Modules/_tkinter.c
55
@@ -28,9 +28,7 @@ Copyright (C) 1994 Steen Lumholt.
@@ -93,7 +93,7 @@ index 6b5fcb8a365..99d44ccf1d8 100644
9393
str_path = _get_tcl_lib_path();
9494
if (str_path == NULL && PyErr_Occurred()) {
9595
return NULL;
96-
@@ -3542,7 +3586,27 @@ PyInit__tkinter(void)
96+
@@ -3542,7 +3586,32 @@ PyInit__tkinter(void)
9797
PyMem_Free(wcs_path);
9898
}
9999
#else
@@ -108,6 +108,11 @@ index 6b5fcb8a365..99d44ccf1d8 100644
108108
+ return NULL;
109109
+ }
110110
+ if (str_path != NULL) {
111+
+ path = PyUnicode_AsUTF8(str_path);
112+
+ if (path == NULL) {
113+
+ Py_DECREF(m);
114+
+ return NULL;
115+
+ }
111116
+ setenv("TCL_LIBRARY", path, 1);
112117
+ set_var = 1;
113118
+ }

cpython-unix/patch-tkinter-3.13.patch

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
2-
index 45897817a56..671b3dfc3d0 100644
2+
index 45897817a56..5633187730a 100644
33
--- a/Modules/_tkinter.c
44
+++ b/Modules/_tkinter.c
55
@@ -26,9 +26,8 @@ Copyright (C) 1994 Steen Lumholt.
@@ -94,7 +94,7 @@ index 45897817a56..671b3dfc3d0 100644
9494
str_path = _get_tcl_lib_path();
9595
if (str_path == NULL && PyErr_Occurred()) {
9696
return NULL;
97-
@@ -3552,7 +3597,27 @@ PyInit__tkinter(void)
97+
@@ -3552,7 +3597,32 @@ PyInit__tkinter(void)
9898
PyMem_Free(wcs_path);
9999
}
100100
#else
@@ -109,6 +109,11 @@ index 45897817a56..671b3dfc3d0 100644
109109
+ return NULL;
110110
+ }
111111
+ if (str_path != NULL) {
112+
+ path = PyUnicode_AsUTF8(str_path);
113+
+ if (path == NULL) {
114+
+ Py_DECREF(m);
115+
+ return NULL;
116+
+ }
112117
+ setenv("TCL_LIBRARY", path, 1);
113118
+ set_var = 1;
114119
+ }
@@ -122,6 +127,3 @@ index 45897817a56..671b3dfc3d0 100644
122127
#endif /* MS_WINDOWS */
123128
}
124129
Py_XDECREF(cexe);
125-
diff --git a/patch-tkinter.patch b/patch-tkinter.patch
126-
new file mode 100644
127-
index 00000000000..e69de29bb2d

cpython-unix/patch-tkinter-3.9.patch

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
2-
index e153047b778..5dbaf2e3e6e 100644
2+
index e153047b778..02f5d12db1a 100644
33
--- a/Modules/_tkinter.c
44
+++ b/Modules/_tkinter.c
55
@@ -115,6 +115,7 @@ Copyright (C) 1994 Steen Lumholt.
@@ -82,7 +82,7 @@ index e153047b778..5dbaf2e3e6e 100644
8282
str_path = _get_tcl_lib_path();
8383
if (str_path == NULL && PyErr_Occurred()) {
8484
return NULL;
85-
@@ -3631,7 +3677,27 @@ PyInit__tkinter(void)
85+
@@ -3631,7 +3677,32 @@ PyInit__tkinter(void)
8686
PyMem_Free(wcs_path);
8787
}
8888
#else
@@ -97,6 +97,11 @@ index e153047b778..5dbaf2e3e6e 100644
9797
+ return NULL;
9898
+ }
9999
+ if (str_path != NULL) {
100+
+ path = PyUnicode_AsUTF8(str_path);
101+
+ if (path == NULL) {
102+
+ Py_DECREF(m);
103+
+ return NULL;
104+
+ }
100105
+ setenv("TCL_LIBRARY", path, 1);
101106
+ set_var = 1;
102107
+ }

0 commit comments

Comments
 (0)