-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-91156: Use locale.getencoding()
instead of getpreferredencoding
#91732
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
Changes from all commits
0bf6bfc
8cd88b9
3005858
951875a
38601b0
b9f088b
d8e8c26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -363,7 +363,7 @@ class TestEnUSCollation(BaseLocalizedTest, TestCollation): | |
locale_type = locale.LC_ALL | ||
|
||
def setUp(self): | ||
enc = codecs.lookup(locale.getpreferredencoding(False) or 'ascii').name | ||
enc = codecs.lookup(locale.getencoding() or 'ascii').name | ||
if enc not in ('utf-8', 'iso8859-1', 'cp1252'): | ||
raise unittest.SkipTest('encoding not suitable') | ||
if enc != 'iso8859-1' and (sys.platform == 'darwin' or is_android or | ||
|
@@ -533,6 +533,14 @@ def test_defaults_UTF8(self): | |
if orig_getlocale is not None: | ||
_locale._getdefaultlocale = orig_getlocale | ||
|
||
def test_getencoding(self): | ||
# Invoke getencoding to make sure it does not cause exceptions. | ||
enc = locale.getencoding() | ||
self.assertIsInstance(enc, str) | ||
self.assertNotEqual(enc, "") | ||
# make sure it is valid | ||
codecs.lookup(enc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _PyUnicode_InitEncodings() fails it config.filesystem_encoding or config.stdio_encoding is not known by codecs.lookup(name). So this call should not fail. If tomorrow this test fails, I suggest to remove it and only check that the string is non-empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When UTF-8 mode is enabled, both of stdio encoding and filesystem encoding are UTF-8, not locale encoding. |
||
|
||
def test_getpreferredencoding(self): | ||
# Invoke getpreferredencoding to make sure it does not cause exceptions. | ||
enc = locale.getpreferredencoding() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import io | ||
import locale | ||
import mimetypes | ||
import pathlib | ||
import sys | ||
|
@@ -33,7 +32,7 @@ def tearDownModule(): | |
class MimeTypesTestCase(unittest.TestCase): | ||
def setUp(self): | ||
self.db = mimetypes.MimeTypes() | ||
|
||
def test_case_sensitivity(self): | ||
eq = self.assertEqual | ||
eq(self.db.guess_type("foobar.HTML"), self.db.guess_type("foobar.html")) | ||
|
@@ -145,11 +144,6 @@ def test_guess_all_types(self): | |
self.assertNotIn('.no-such-ext', all) | ||
|
||
def test_encoding(self): | ||
getpreferredencoding = locale.getpreferredencoding | ||
self.addCleanup(setattr, locale, 'getpreferredencoding', | ||
getpreferredencoding) | ||
locale.getpreferredencoding = lambda: 'ascii' | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This hack doesn't work for most cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove This code is correct. I don't think that the locale encoding is still used: MimeType.read() calls |
||
filename = support.findfile("mime.types") | ||
mimes = mimetypes.MimeTypes([filename]) | ||
exts = mimes.guess_all_extensions('application/vnd.geocube+xml', | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to test the type: add
self.assertIsInstance(enc, str)
.Maybe also ensure that the string is not empty? add
self.assertNotEqual(enc, "")
.