Skip to content

Commit 9dae6f5

Browse files
Merge remote-tracking branch 'upstream/main' into tkinter-photoimage-tk9-options
# Conflicts: # Doc/whatsnew/3.16.rst
2 parents e462332 + 0bd1024 commit 9dae6f5

9 files changed

Lines changed: 93 additions & 11 deletions

File tree

Doc/library/tkinter.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6076,6 +6076,14 @@ Image classes
60766076
it is displayed as transparent and the background of whatever window it
60776077
is displayed in shows through.
60786078

6079+
.. method:: redither()
6080+
6081+
Recalculate the dithered image in each window where it is displayed.
6082+
This is useful when the image data was supplied in pieces, in which case
6083+
the dithered image may not be exactly correct.
6084+
6085+
.. versionadded:: next
6086+
60796087
.. method:: cget(option)
60806088

60816089
Return the current value of the configuration option *option*.

Doc/whatsnew/3.16.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ tkinter
201201
the *withalpha* parameter of :meth:`~tkinter.PhotoImage.get`.
202202
(Contributed by Serhiy Storchaka in :gh:`151890`.)
203203

204+
* Added the :meth:`~tkinter.PhotoImage.redither` method which recalculates the
205+
dithered image when its data was supplied in pieces.
206+
(Contributed by Serhiy Storchaka in :gh:`151888`.)
207+
204208

205209
xml
206210
---

Lib/sysconfig/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ def joinuser(*args):
182182
_CONFIG_VARS = None
183183
# True iff _CONFIG_VARS has been fully initialized.
184184
_CONFIG_VARS_INITIALIZED = False
185+
_config_vars_cached_prefix = None
186+
_config_vars_cached_exec_prefix = None
185187
_USER_BASE = None
186188

187189

@@ -600,16 +602,20 @@ def get_config_vars(*args):
600602
each argument in the configuration variable dictionary.
601603
"""
602604
global _CONFIG_VARS_INITIALIZED
605+
global _config_vars_cached_prefix
606+
global _config_vars_cached_exec_prefix
603607

604608
# Avoid claiming the lock once initialization is complete.
609+
prefix = os.path.normpath(sys.prefix)
610+
exec_prefix = os.path.normpath(sys.exec_prefix)
605611
if _CONFIG_VARS_INITIALIZED:
606612
# GH-126789: If sys.prefix or sys.exec_prefix were updated, invalidate the cache.
607-
prefix = os.path.normpath(sys.prefix)
608-
exec_prefix = os.path.normpath(sys.exec_prefix)
609-
if _CONFIG_VARS['prefix'] != prefix or _CONFIG_VARS['exec_prefix'] != exec_prefix:
613+
if _config_vars_cached_prefix != prefix or _config_vars_cached_exec_prefix != exec_prefix:
610614
with _CONFIG_VARS_LOCK:
611615
_CONFIG_VARS_INITIALIZED = False
612616
_init_config_vars()
617+
_config_vars_cached_prefix = prefix
618+
_config_vars_cached_exec_prefix = exec_prefix
613619
else:
614620
# Initialize the config_vars cache.
615621
with _CONFIG_VARS_LOCK:
@@ -619,6 +625,8 @@ def get_config_vars(*args):
619625
# don't re-enter init_config_vars().
620626
if _CONFIG_VARS is None:
621627
_init_config_vars()
628+
_config_vars_cached_prefix = prefix
629+
_config_vars_cached_exec_prefix = exec_prefix
622630

623631
if args:
624632
vals = []

Lib/test/test_tkinter/test_images.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ def test_blank(self):
311311
self.assertEqual(image.height(), 16)
312312
self.assertEqual(image.get(4, 6), self.colorlist(0, 0, 0))
313313

314+
def test_redither(self):
315+
image = self.create()
316+
pixel = image.get(4, 6)
317+
image.redither() # Recalculates the dithering; the data is unchanged.
318+
self.assertEqual(image.get(4, 6), pixel)
319+
314320
def test_copy(self):
315321
image = self.create()
316322
image2 = image.copy()

Lib/test/test_venv.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,46 @@ def test_special_chars_csh(self):
773773
self.assertTrue(env_name.encode() in lines[0])
774774
self.assertEndsWith(lines[1], env_name.encode())
775775

776+
# gh-140006: the fish prompt override must keep working when a user
777+
# function shadows a builtin it relies on.
778+
@unittest.skipIf(os.name == 'nt', 'fish is not available on Windows')
779+
def test_fish_activate_shadowed_builtins(self):
780+
"""
781+
The fish prompt override restores the exit status through `source` and
782+
prints through `printf`/`echo`/`set_color`. A user function that
783+
shadows one of those builtins (a common pattern for `.`-style directory
784+
navigators) must not hijack the prompt or break status restoration.
785+
"""
786+
fish = shutil.which('fish')
787+
if fish is None:
788+
self.skipTest('fish required for this test')
789+
rmtree(self.env_dir)
790+
builder = venv.EnvBuilder(clear=True)
791+
builder.create(self.env_dir)
792+
activate = os.path.join(self.env_dir, self.bindir, 'activate.fish')
793+
test_script = os.path.join(self.env_dir, 'test_shadowed_builtins.fish')
794+
with open(test_script, "w") as f:
795+
f.write(
796+
# The pre-existing prompt reports the status it receives;
797+
# activation copies it to _old_fish_prompt.
798+
'function fish_prompt; builtin echo "OLDSTATUS=$status"; end\n'
799+
f'source {shlex.quote(activate)}\n'
800+
# Shadow every builtin the override uses. A dot-navigator that
801+
# lists the directory is the reported failure.
802+
'function .; builtin echo DOT_LEAK; end\n'
803+
'function source; builtin echo SOURCE_LEAK; end\n'
804+
'function echo; command echo ECHO_LEAK; end\n'
805+
'function printf; command printf PRINTF_LEAK; end\n'
806+
'function set_color; command true; end\n'
807+
'function _exit7; return 7; end\n'
808+
'_exit7\n'
809+
'fish_prompt\n'
810+
)
811+
out, err = check_output([fish, '--no-config', test_script])
812+
text = out.decode()
813+
self.assertNotIn('LEAK', text)
814+
self.assertIn('OLDSTATUS=7', text)
815+
776816
# gh-124651: test quoted strings on Windows
777817
@unittest.skipUnless(os.name == 'nt', 'only relevant on Windows')
778818
def test_special_chars_windows(self):

Lib/tkinter/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4571,6 +4571,13 @@ def blank(self):
45714571
"""Display a transparent image."""
45724572
self.tk.call(self.name, 'blank')
45734573

4574+
def redither(self):
4575+
"""Recalculate the dithered image in each window where it is displayed.
4576+
4577+
Useful when the image data was supplied in pieces, in which case the
4578+
dithered image may not be exactly correct."""
4579+
self.tk.call(self.name, 'redither')
4580+
45744581
def cget(self, option):
45754582
"""Return the value of OPTION."""
45764583
return self.tk.call(self.name, 'cget', '-' + option)

Lib/venv/scripts/common/activate.fish

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ function deactivate -d "Exit virtual environment and return to normal shell env
1515
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
1616
set -e _OLD_FISH_PROMPT_OVERRIDE
1717
# prevents error when using nested fish instances (Issue #93858)
18-
if functions -q _old_fish_prompt
19-
functions -e fish_prompt
20-
functions -c _old_fish_prompt fish_prompt
21-
functions -e _old_fish_prompt
18+
if builtin functions -q _old_fish_prompt
19+
builtin functions -e fish_prompt
20+
builtin functions -c _old_fish_prompt fish_prompt
21+
builtin functions -e _old_fish_prompt
2222
end
2323
end
2424

2525
set -e VIRTUAL_ENV
2626
set -e VIRTUAL_ENV_PROMPT
2727
if test "$argv[1]" != "nondestructive"
2828
# Self-destruct!
29-
functions -e deactivate
29+
builtin functions -e deactivate
3030
end
3131
end
3232

@@ -52,18 +52,21 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
5252
# fish uses a function instead of an env var to generate the prompt.
5353

5454
# Save the current fish_prompt function as the function _old_fish_prompt.
55-
functions -c fish_prompt _old_fish_prompt
55+
builtin functions -c fish_prompt _old_fish_prompt
5656

5757
# With the original prompt function renamed, we can override with our own.
58+
# Call every builtin through `builtin` so a user function that shadows
59+
# `printf`, `set_color`, `echo`, or `source`/`.` cannot hijack the prompt
60+
# (Issue #140006).
5861
function fish_prompt
5962
# Save the return status of the last command.
6063
set -l old_status $status
6164

6265
# Output the venv prompt; color taken from the blue of the Python logo.
63-
printf "%s(%s)%s " (set_color 4B8BBE) __VENV_PROMPT__ (set_color normal)
66+
builtin printf "%s(%s)%s " (builtin set_color 4B8BBE) __VENV_PROMPT__ (builtin set_color normal)
6467

6568
# Restore the return status of the previous command.
66-
echo "exit $old_status" | .
69+
builtin echo "exit $old_status" | builtin source -
6770
# Output the original/"old" prompt.
6871
_old_fish_prompt
6972
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The :mod:`venv` ``activate.fish`` script now calls fish builtins through
2+
``builtin`` so a user function that shadows ``.``/``source``, ``echo``,
3+
``printf``, ``set_color``, or ``functions`` can no longer hijack the virtual
4+
environment prompt or break exit-status reporting.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add the :meth:`!tkinter.PhotoImage.redither` method, wrapping the photo image
2+
``redither`` Tk command.

0 commit comments

Comments
 (0)