10000 bpo-43651: PEP 597: Fix EncodingWarning in some tests by methane · Pull Request #25189 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-43651: PEP 597: Fix EncodingWarning in some tests #25189

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

Merged
merged 11 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix test_logging
  • Loading branch information
methane committed Apr 5, 2021
commit 98148f6bbb27e56c9ec39ea6359dac8f172bbc6c
7 changes: 5 additions & 2 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,8 @@ def __init__(self, filename, mode='a', encoding=None, delay=False, errors=None):
#may come a cropper when the current directory changes
self.baseFilename = os.path.abspath(filename)
self.mode = mode
self.encoding = encoding
if "b" not in mode:
self.encoding = io.text_encoding(encoding)
self.errors = errors
self.delay = delay
# bpo-26789: FileHandler keeps a reference to the builtin open()
Expand Down Expand Up @@ -2022,8 +2023,10 @@ def basicConfig(**kwargs):
filename = kwargs.pop("filename", None)
mode = kwargs.pop("filemode", 'a')
if filename:
if 'b'in mode:
if 'b' in mode:
errors = None
else:
encoding = io.text_encoding(encoding)
h = FileHandler(filename, mode,
encoding=encoding, errors=errors)
else:
Expand Down
8 changes: 7 additions & 1 deletion Lib/logging/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
To use, simply 'import logging.handlers' and log away!
"""

import logging, socket, os, pickle, struct, time, re
import io, logging, socket, os, pickle, struct, time, re
from stat import ST_DEV, ST_INO, ST_MTIME
import queue
import threading
Expand Down Expand Up @@ -150,6 +150,8 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
# on each run.
if maxBytes > 0:
mode = 'a'
if "b" not in mode:
encoding = io.text_encoding(encoding)
BaseRotatingHandler.__init__(self, filename, mode, encoding=encoding,
delay=delay, errors=errors)
self.maxBytes = maxBytes
Expand Down Expand Up @@ -205,6 +207,8 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
def __init__(self, filename, when='h', interval=1, backupCount=0,
encoding=None, delay=False, utc=False, atTime=None,
errors=None):
if "b" not in mode:
encoding = io.text_encoding(encoding)
BaseRotatingHandler.__init__(self, filename, 'a', encoding=encoding,
delay=delay, errors=errors)
self.when = when.upper()
Expand Down Expand Up @@ -442,6 +446,8 @@ class WatchedFileHandler(logging.FileHandler):
"""
def __init__(self, filename, mode='a', encoding=None, delay=False,
errors=None):
if "b" not in mode:
encoding = io.text_encoding(encoding)
logging.FileHandler.__init__(self, filename, mode=mode,
encoding=encoding, delay=delay,
errors=errors)
Expand Down
31 changes: 16 additions & 15 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()

Expand All @@ -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):
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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({}))
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand Down
0