summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2012-06-05 11:43:22 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2012-06-05 11:43:22 (GMT)
commitf86a5e8a93ab293d4cc00a8f2835d6d2cd3baa69 (patch)
tree9d249e4c06f25885dc668edd529b1a4c802c898a /Lib/test
parent91c5a34613fb918c79bb372723e10e106ad9a9be (diff)
downloadcpython-f86a5e8a93ab293d4cc00a8f2835d6d2cd3baa69.zip
cpython-f86a5e8a93ab293d4cc00a8f2835d6d2cd3baa69.tar.gz
cpython-f86a5e8a93ab293d4cc00a8f2835d6d2cd3baa69.tar.bz2
Close #11022: TextIOWrapper doesn't call locale.setlocale() anymore
open() and io.TextIOWrapper are now calling locale.getpreferredencoding(False) instead of locale.getpreferredencoding() in text mode if the encoding is not specified. Don't change temporary the locale encoding using locale.setlocale(), use the current locale encoding instead of the user preferred encoding. Explain also in open() documentation that locale.getpreferredencoding(False) is called if the encoding is not specified.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_builtin.py40
-rw-r--r--Lib/test/test_io.py35
2 files changed, 58 insertions, 17 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index dfe64bf..d0d17c7 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -1,20 +1,21 @@
# Python test set -- built-in functions
-import platform
-import unittest
-import sys
-import warnings
+import ast
+import builtins
import collections
import io
+import locale
import os
-import ast
-import types
-import builtins
+import pickle
+import platform
import random
+import sys
import traceback
-from test.support import TESTFN, unlink, run_unittest, check_warnings
+import types
+import unittest
+import warnings
from operator import neg
-import pickle
+from test.support import TESTFN, unlink, run_unittest, check_warnings
try:
import pty, signal
except ImportError:
@@ -961,6 +962,27 @@ class BuiltinTest(unittest.TestCase):
fp.close()
unlink(TESTFN)
+ def test_open_default_encoding(self):
+ old_environ = dict(os.environ)
+ try:
+ # try to get a user preferred encoding different than the current
+ # locale encoding to check that open() uses the current locale
+ # encoding and not the user preferred encoding
+ for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
+ if key in os.environ:
+ del os.environ[key]
+
+ self.write_testfile()
+ current_locale_encoding = locale.getpreferredencoding(False)
+ fp = open(TESTFN, 'w')
+ try:
+ self.assertEqual(fp.encoding, current_locale_encoding)
+ finally:
+ fp.close()
+ finally:
+ os.environ.clear()
+ os.environ.update(old_environ)
+
def test_ord(self):
self.assertEqual(ord(' '), 32)
self.assertEqual(ord('A'), 65)
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index f5bb732..1951a06 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -19,20 +19,21 @@
# test both implementations. This file has lots of examples.
################################################################################
+import abc
+import array
+import errno
+import locale
import os
+import pickle
+import random
+import signal
import sys
import time
-import array
-import random
import unittest
-import weakref
-import abc
-import signal
-import errno
import warnings
-import pickle
-from itertools import cycle, count
+import weakref
from collections import deque
+from itertools import cycle, count
from test import support
import codecs
@@ -1881,6 +1882,24 @@ class TextIOWrapperTest(unittest.TestCase):
t.write("A\rB")
self.assertEqual(r.getvalue(), b"XY\nZA\rB")
+ def test_default_encoding(self):
+ old_environ = dict(os.environ)
+ try:
+ # try to get a user preferred encoding different than the current
+ # locale encoding to check that TextIOWrapper() uses the current
+ # locale encoding and not the user preferred encoding
+ for key in ('LC_ALL', 'LANG', 'LC_CTYPE'):
+ if key in os.environ:
+ del os.environ[key]
+
+ current_locale_encoding = locale.getpreferredencoding(False)
+ b = self.BytesIO()
+ t = self.TextIOWrapper(b)
+ self.assertEqual(t.encoding, current_locale_encoding)
+ finally:
+ os.environ.clear()
+ os.environ.update(old_environ)
+
def test_encoding(self):
# Check the encoding attribute is always set, and valid
b = self.BytesIO()