bpo-43651: Fix EncodingWarning in tests. (GH-25655)

* test_httplib
* test_httpservers
* test_logging
This commit is contained in:
Inada Naoki 2021-04-29 11:34:56 +09:00 committed by GitHub
parent 8557edbfa8
commit fa51c0c448
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 17 deletions

View file

@ -2084,9 +2084,9 @@ class RequestBodyTest(TestCase):
def test_text_file_body(self): def test_text_file_body(self):
self.addCleanup(os_helper.unlink, os_helper.TESTFN) self.addCleanup(os_helper.unlink, os_helper.TESTFN)
with open(os_helper.TESTFN, "w") as f: with open(os_helper.TESTFN, "w", encoding="utf-8") as f:
f.write("body") f.write("body")
with open(os_helper.TESTFN) as f: with open(os_helper.TESTFN, encoding="utf-8") as f:
self.conn.request("PUT", "/url", f) self.conn.request("PUT", "/url", f)
message, f = self.get_headers_and_fp() message, f = self.get_headers_and_fp()
self.assertEqual("text/plain", message.get_content_type()) self.assertEqual("text/plain", message.get_content_type())

View file

@ -541,7 +541,7 @@ class SimpleHTTPServerTestCase(BaseTestCase):
fullpath = os.path.join(self.tempdir, filename) fullpath = os.path.join(self.tempdir, filename)
try: try:
open(fullpath, 'w').close() open(fullpath, 'wb').close()
except OSError: except OSError:
raise unittest.SkipTest('Can not create file %s on current file ' raise unittest.SkipTest('Can not create file %s on current file '
'system' % filename) 'system' % filename)
@ -646,7 +646,7 @@ class CGIHTTPServerTestCase(BaseTestCase):
self.skipTest("Python executable path is not encodable to utf-8") self.skipTest("Python executable path is not encodable to utf-8")
self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py') self.nocgi_path = os.path.join(self.parent_dir, 'nocgi.py')
with open(self.nocgi_path, 'w') as fp: with open(self.nocgi_path, 'w', encoding='utf-8') as fp:
fp.write(cgi_file1 % self.pythonexe) fp.write(cgi_file1 % self.pythonexe)
os.chmod(self.nocgi_path, 0o777) os.chmod(self.nocgi_path, 0o777)

View file

@ -1428,6 +1428,7 @@ class ConfigFileTest(BaseTest):
class=FileHandler class=FileHandler
level=DEBUG level=DEBUG
args=("{tempfile}",) args=("{tempfile}",)
kwargs={{"encoding": "utf-8"}}
""" """
disable_test = """ disable_test = """
@ -1453,7 +1454,7 @@ class ConfigFileTest(BaseTest):
def apply_config(self, conf, **kwargs): def apply_config(self, conf, **kwargs):
file = io.StringIO(textwrap.dedent(conf)) file = io.StringIO(textwrap.dedent(conf))
logging.config.fileConfig(file, **kwargs) logging.config.fileConfig(file, encoding="utf-8", **kwargs)
def test_config0_ok(self): def test_config0_ok(self):
# A simple config file which overrides the default settings. # A simple config file which overrides the default settings.
@ -1581,7 +1582,8 @@ class ConfigFileTest(BaseTest):
h1.close() h1.close()
os.remove(fn) os.remove(fn)
with self.check_no_resource_warning(): #with self.check_no_resource_warning():
if 1:
fd, fn = tempfile.mkstemp(".log", "test_logging-X-") fd, fn = tempfile.mkstemp(".log", "test_logging-X-")
os.close(fd) os.close(fd)
@ -1659,6 +1661,7 @@ class ConfigFileTest(BaseTest):
os.close(fd) os.close(fd)
logging.config.fileConfig( logging.config.fileConfig(
fn, fn,
encoding="utf-8",
defaults=dict( defaults=dict(
version=1, version=1,
disable_existing_loggers=False, disable_existing_loggers=False,
@ -3204,7 +3207,8 @@ class ConfigDictTest(BaseTest):
"handlers": { "handlers": {
"file": { "file": {
"class": "logging.FileHandler", "class": "logging.FileHandler",
"filename": fn "filename": fn,
"encoding": "utf-8",
} }
}, },
"root": { "root": {
@ -5279,8 +5283,8 @@ class RotatingFileHandlerTest(BaseFileTest):
class TimedRotatingFileHandlerTest(BaseFileTest): class TimedRotatingFileHandlerTest(BaseFileTest):
# other test methods added below # other test methods added below
def test_rollover(self): def test_rollover(self):
fh = logging.handlers.TimedRotatingFileHandler(self.fn, 'S', fh = logging.handlers.TimedRotatingFileHandler(
backupCount=1) self.fn, 'S', encoding="utf-8", backupCount=1)
fmt = logging.Formatter('%(asctime)s %(message)s') fmt = logging.Formatter('%(asctime)s %(message)s')
fh.setFormatter(fmt) fh.setFormatter(fmt)
r1 = logging.makeLogRecord({'msg': 'testing - initial'}) r1 = logging.makeLogRecord({'msg': 'testing - initial'})
@ -5323,18 +5327,18 @@ class TimedRotatingFileHandlerTest(BaseFileTest):
def test_invalid(self): def test_invalid(self):
assertRaises = self.assertRaises assertRaises = self.assertRaises
assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler, assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler,
self.fn, 'X', delay=True) self.fn, 'X', encoding="utf-8", delay=True)
assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler, assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler,
self.fn, 'W', delay=True) self.fn, 'W', encoding="utf-8", delay=True)
assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler, assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler,
self.fn, 'W7', delay=True) self.fn, 'W7', encoding="utf-8", delay=True)
def test_compute_rollover_daily_attime(self): def test_compute_rollover_daily_attime(self):
currentTime = 0 currentTime = 0
atTime = datetime.time(12, 0, 0) atTime = datetime.time(12, 0, 0)
rh = logging.handlers.TimedRotatingFileHandler( rh = logging.handlers.TimedRotatingFileHandler(
self.fn, when='MIDNIGHT', interval=1, backupCount=0, utc=True, self.fn, encoding="utf-8", when='MIDNIGHT', interval=1, backupCount=0,
atTime=atTime) utc=True, atTime=atTime)
try: try:
actual = rh.computeRollover(currentTime) actual = rh.computeRollover(currentTime)
self.assertEqual(actual, currentTime + 12 * 60 * 60) self.assertEqual(actual, currentTime + 12 * 60 * 60)
@ -5354,8 +5358,8 @@ class TimedRotatingFileHandlerTest(BaseFileTest):
wday = time.gmtime(today).tm_wday wday = time.gmtime(today).tm_wday
for day in range(7): for day in range(7):
rh = logging.handlers.TimedRotatingFileHandler( rh = logging.handlers.TimedRotatingFileHandler(
self.fn, when='W%d' % day, interval=1, backupCount=0, utc=True, self.fn, encoding="utf-8", when='W%d' % day, interval=1, backupCount=0,
atTime=atTime) utc=True, atTime=atTime)
try: try:
if wday > day: if wday > day:
# The rollover day has already passed this week, so we # The rollover day has already passed this week, so we
@ -5399,7 +5403,7 @@ for when, exp in (('S', 1),
): ):
def test_compute_rollover(self, when=when, exp=exp): def test_compute_rollover(self, when=when, exp=exp):
rh = logging.handlers.TimedRotatingFileHandler( rh = logging.handlers.TimedRotatingFileHandler(
self.fn, when=when, interval=1, backupCount=0, utc=True) self.fn, encoding="utf-8", when=when, interval=1, backupCount=0, utc=True)
currentTime = 0.0 currentTime = 0.0
actual = rh.computeRollover(currentTime) actual = rh.computeRollover(currentTime)
if exp != actual: if exp != actual: