|
5 | 5 | import sys |
6 | 6 | import textwrap |
7 | 7 | import unittest |
8 | | -import warnings |
9 | 8 | import weakref |
10 | 9 | import tkinter |
11 | 10 | from tkinter import TclError |
@@ -366,24 +365,6 @@ def test_getdouble(self): |
366 | 365 | self.assertEqual(self.root.getdouble(3), 3.0) |
367 | 366 | self.assertRaises(ValueError, self.root.getdouble, 'spam') |
368 | 367 |
|
369 | | - def test_readprofile(self): |
370 | | - # gh-153333: readprofile() reads the profile scripts with the source |
371 | | - # file's encoding (here a Latin-1 coding cookie), not the locale |
372 | | - # default encoding. |
373 | | - name = 'readprofiletest' |
374 | | - script = ("# -*- coding: latin-1 -*-\n" |
375 | | - "self.readprofile_result = 'caf\xe9'\n") |
376 | | - self.addCleanup(self.root.__dict__.pop, 'readprofile_result', None) |
377 | | - with os_helper.temp_dir() as home: |
378 | | - with open(os.path.join(home, '.%s.py' % name), 'wb') as f: |
379 | | - f.write(script.encode('latin-1')) |
380 | | - with os_helper.EnvironmentVarGuard() as env: |
381 | | - env['HOME'] = home |
382 | | - with warnings.catch_warnings(): |
383 | | - warnings.simplefilter('error', EncodingWarning) |
384 | | - self.root.readprofile(name, name) |
385 | | - self.assertEqual(self.root.readprofile_result, 'caf\xe9') |
386 | | - |
387 | 368 | def test_getvar(self): |
388 | 369 | self.root.setvar('test_var', 'hello') |
389 | 370 | self.assertEqual(self.root.getvar('test_var'), 'hello') |
@@ -829,6 +810,26 @@ def test_iterable_protocol(self): |
829 | 810 |
|
830 | 811 | class TkTest(AbstractTkTest, unittest.TestCase): |
831 | 812 |
|
| 813 | + def test_readprofile(self): |
| 814 | + # gh-153333: profile scripts are decoded with their own coding cookie, |
| 815 | + # not the locale encoding. Two cookies so no locale can mask the bug. |
| 816 | + profiles = { |
| 817 | + '.RpClass.py': ('latin-1', "self._rp_latin1 = 'caf\xe9'"), |
| 818 | + '.rpbase.py': ('utf-8', "self._rp_utf8 = 'caf\xe9'"), |
| 819 | + } |
| 820 | + self.addCleanup(self.root.__dict__.pop, '_rp_latin1', None) |
| 821 | + self.addCleanup(self.root.__dict__.pop, '_rp_utf8', None) |
| 822 | + with (os_helper.temp_dir() as home, |
| 823 | + os_helper.EnvironmentVarGuard() as env): |
| 824 | + env['HOME'] = home |
| 825 | + for filename, (encoding, body) in profiles.items(): |
| 826 | + script = '# -*- coding: %s -*-\n%s\n' % (encoding, body) |
| 827 | + with open(os.path.join(home, filename), 'wb') as f: |
| 828 | + f.write(script.encode(encoding)) |
| 829 | + self.root.readprofile('rpbase', 'RpClass') |
| 830 | + self.assertEqual(self.root._rp_latin1, 'caf\xe9') |
| 831 | + self.assertEqual(self.root._rp_utf8, 'caf\xe9') |
| 832 | + |
832 | 833 | def test_className(self): |
833 | 834 | # The className argument sets the class of the root window. Tk |
834 | 835 | # title-cases it: the first letter is upper-cased, the rest lower-cased. |
|
0 commit comments