bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25189)
* Fix _sitebuiltins
* Fix test_inspect
* Fix test_interpreters
* Fix test_io
* Fix test_iter
* Fix test_json
* Fix test_linecache
* Fix test_lltrace
* Fix test_logging
* Fix logging
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 859baa4..452ba78 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -553,7 +553,7 @@ def test_builtin_handlers(self):
os.close(fd)
if not existing:
os.unlink(fn)
- h = logging.handlers.WatchedFileHandler(fn, delay=True)
+ h = logging.handlers.WatchedFileHandler(fn, encoding='utf-8', delay=True)
if existing:
dev, ino = h.dev, h.ino
self.assertEqual(dev, -1)
@@ -616,7 +616,7 @@ def test_path_objects(self):
if sys.platform in ('linux', 'darwin'):
cases += ((logging.handlers.WatchedFileHandler, (pfn, 'w')),)
for cls, args in cases:
- h = cls(*args)
+ h = cls(*args, encoding="utf-8")
self.assertTrue(os.path.exists(fn))
h.close()
os.unlink(fn)
@@ -645,7 +645,7 @@ def remove_loop(fname, tries):
remover = threading.Thread(target=remove_loop, args=(fn, del_count))
remover.daemon = True
remover.start()
- h = logging.handlers.WatchedFileHandler(fn, delay=delay)
+ h = logging.handlers.WatchedFileHandler(fn, encoding='utf-8', delay=delay)
f = logging.Formatter('%(asctime)s: %(levelname)s: %(message)s')
h.setFormatter(f)
try:
@@ -677,7 +677,7 @@ class _OurHandler(logging.Handler):
def __init__(self):
super().__init__()
self.sub_handler = logging.StreamHandler(
- stream=open('/dev/null', 'wt'))
+ stream=open('/dev/null', 'wt', encoding='utf-8'))
def emit(self, record):
self.sub_handler.acquire()
@@ -4355,7 +4355,7 @@ def __del__(self):
# basicConfig() opens the file, but logging.shutdown() closes
# it at Python exit. When A.__del__() is called,
# FileHandler._open() must be called again to re-open the file.
- logging.basicConfig(filename={filename!r})
+ logging.basicConfig(filename={filename!r}, encoding="utf-8")
a = A()
@@ -4365,7 +4365,7 @@ def __del__(self):
""")
assert_python_ok("-c", code)
- with open(filename) as fp:
+ with open(filename, encoding="utf-8") as fp:
self.assertEqual(fp.read().rstrip(), "ERROR:root:log in __del__")
def test_recursion_error(self):
@@ -4557,13 +4557,13 @@ def cleanup(h1, h2, fn):
h2.close()
os.remove(fn)
- logging.basicConfig(filename='test.log')
+ logging.basicConfig(filename='test.log', encoding='utf-8')
self.assertEqual(len(logging.root.handlers), 1)
handler = logging.root.handlers[0]
self.assertIsInstance(handler, logging.FileHandler)
- expected = logging.FileHandler('test.log', 'a')
+ expected = logging.FileHandler('test.log', 'a', encoding='utf-8')
self.assertEqual(handler.stream.mode, expected.stream.mode)
self.assertEqual(handler.stream.name, expected.stream.name)
self.addCleanup(cleanup, handler, expected, 'test.log')
@@ -5161,7 +5161,7 @@ def assertLogFile(self, filename):
class FileHandlerTest(BaseFileTest):
def test_delay(self):
os.unlink(self.fn)
- fh = logging.FileHandler(self.fn, delay=True)
+ fh = logging.FileHandler(self.fn, encoding='utf-8', delay=True)
self.assertIsNone(fh.stream)
self.assertFalse(os.path.exists(self.fn))
fh.handle(logging.makeLogRecord({}))
@@ -5176,19 +5176,20 @@ def next_rec(self):
def test_should_not_rollover(self):
# If maxbytes is zero rollover never occurs
- rh = logging.handlers.RotatingFileHandler(self.fn, maxBytes=0)
+ rh = logging.handlers.RotatingFileHandler(
+ self.fn, encoding="utf-8", maxBytes=0)
self.assertFalse(rh.shouldRollover(None))
rh.close()
def test_should_rollover(self):
- rh = logging.handlers.RotatingFileHandler(self.fn, maxBytes=1)
+ rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8", maxBytes=1)
self.assertTrue(rh.shouldRollover(self.next_rec()))
rh.close()
def test_file_created(self):
# checks that the file is created and assumes it was created
# by us
- rh = logging.handlers.RotatingFileHandler(self.fn)
+ rh = logging.handlers.RotatingFileHandler(self.fn, encoding="utf-8")
rh.emit(self.next_rec())
self.assertLogFile(self.fn)
rh.close()
@@ -5197,7 +5198,7 @@ def test_rollover_filenames(self):
def namer(name):
return name + ".test"
rh = logging.handlers.RotatingFileHandler(
- self.fn, backupCount=2, maxBytes=1)
+ self.fn, encoding="utf-8", backupCount=2, maxBytes=1)
rh.namer = namer
rh.emit(self.next_rec())
self.assertLogFile(self.fn)
@@ -5218,7 +5219,7 @@ def rotator(self, source, dest):
os.rename(source, dest + ".rotated")
rh = HandlerWithNamerAndRotator(
- self.fn, backupCount=2, maxBytes=1)
+ self.fn, encoding="utf-8", backupCount=2, maxBytes=1)
self.assertEqual(rh.namer(self.fn), self.fn + ".test")
rh.emit(self.next_rec())
self.assertLogFile(self.fn)
@@ -5241,7 +5242,7 @@ def rotator(source, dest):
os.remove(source)
rh = logging.handlers.RotatingFileHandler(
- self.fn, backupCount=2, maxBytes=1)
+ self.fn, encoding="utf-8", backupCount=2, maxBytes=1)
rh.rotator = rotator
rh.namer = namer
m1 = self.next_rec()