Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch _tkinter.c to look in base prefix #421

Merged
merged 5 commits into from
Dec 17, 2024
Merged

Conversation

charliermarsh
Copy link
Member

@charliermarsh charliermarsh commented Dec 15, 2024

Summary

This PR attempts to adresss one of the known quirks around Tcl/tk files. On Windows, CPython will look for the Tcl/tk files at {base_prefix}/lib. That works well for python-build-standalone, since we ship the files in that location. However, on Unix, CPython does not check that location -- instead, for python-build-standalone, it just looks relative to the executable. This works for non-virtual environments, since CPython will look in {prefix}/lib. But not for virtual environments.

The patch applied here ports the Windows discovery logic to Unix, so that we always hook up the right paths.

The advantage of this approach (vis-a-vis others that were considered) is that it doesn't rely on injecting an environment variable into the user's environment.

For now, I've only patched Python 3.13. Unfortunately, I'll need a slightly different patch for each Python minor. I've already authored them locally, but I want to hold off on wiring them all up until we're aligned on this general approach.

In theory, I think this change could be upstreamed.

Test Plan

  • Build Python locally.
  • ./python/install/bin/python -c "from tkinter import Tk; Tk()"
  • ./python/install/bin/python -m venv .venv
  • .venv/bin/python -c "from tkinter import Tk; Tk()"

@charliermarsh charliermarsh force-pushed the charlie/tkinter branch 6 times, most recently from bf37889 to b9e7b1d Compare December 15, 2024 22:26
@charliermarsh
Copy link
Member Author

(To clarify, I'm looking for sign-off on the approach and input on whether we want to apply a separate patch for each Python minor, or cutoff support at some later version, like 3.11, or 3.12, or 3.13.)

Copy link
Collaborator

@indygreg indygreg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Historically I've tried to ensure that all Python X.Y versions behave the same. So nominally we should carry patch variants for all versions, as necessary.

I agree that this is the type of patch that upstream may want to take. However, I could see aspects of it getting stalled up. e.g. I wouldn't be surprised if some distros didn't install things at /usr/lib/tcl and the hardcoding of this path is probably wrong. (This is the type of thing that should be controllable via a configure argument and the actual path should also be sniffed via configure.)

I glanced at the patch and looking for the tcl files in <prefix>/lib/tcl should be reasonable for PBS. So the new functionality passes the smell test.

@@ -179,6 +179,15 @@ else
patch -p1 -i ${ROOT}/patch-ctypes-callproc-legacy.patch
fi

# On Windows, CPython looks for the Tcl/Tk libraries relative to the base prefix,
# with we want. But on Unix, it doesn't. This patch applies similar behavior on Unix,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"which we want"?

@charliermarsh
Copy link
Member Author

Sounds good. I'll go ahead and add patches for the remaining minors.

Yeah, I briefly chatted with @zooba about this in DMs, just to ask for any context on why the Tcl/Tk behavior differs, and he suggested that it could make sense for us to add a build flag in CPython to enable standalone behavior, since it tends to be easier to get patches merged if they're not enabled by default and therefore don't impact redistributors.

@charliermarsh
Copy link
Member Author

Ok, I somewhat painstakingly added and manually tested a patch for each minor.

@charliermarsh charliermarsh marked this pull request as ready for review December 16, 2024 19:48
@charliermarsh charliermarsh merged commit b545a9f into main Dec 17, 2024
278 of 280 checks passed
Copy link

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know anything about Tcl/Tk, so I don't fully understand what this code needs to do, but commented on a couple things I noticed. Couple other things:

  • It seems like the return value of _get_tcl_lib_path is never decrefed and is just leaked? But if this is only called in one-time init code paths, probably nobody cares, one-time leaks are normal. And it looks like that was already the case in the Windows-only codepaths.
  • It seems like some code duplication between Windows and non-Windows could probably be reduced here, but for now this keeps the patches simpler. Might be worth doing that if this is upstreamed in future.

+ return NULL;
+ }
+ if (str_path != NULL) {
+ setenv("TCL_LIBRARY", path, 1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like path is used un-initialized here?

(Same in other versions.)

Tcl_FindExecutable(PyBytes_AS_STRING(cexe));
+
+ if (set_var) {
+ unsetenv("TCL_LIBRARY");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should path be freed here? (If it were ever allocated/initialized above, which it doesn't seem to be.) The equivalent in the Windows code path (wcs_path) is freed here.

(Same in other versions.)

@charliermarsh
Copy link
Member Author

Thank you @carljm!

@charliermarsh charliermarsh deleted the charlie/tkinter branch December 17, 2024 13:38
charliermarsh added a commit that referenced this pull request Dec 17, 2024
## 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants