diff options
author | Barney Gale <barney.gale@gmail.com> | 2025-04-28 19:04:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-28 19:04:20 +0100 |
commit | fbffd70328bb2a73d8154b4dd944e6d707fde9b3 (patch) | |
tree | 4335b168ebb9c41e8bdd8ce445ff0ea650a5e7f4 /Lib/test/test_pathlib/test_read.py | |
parent | 6f0432599297635492597e3766259390e8331c62 (diff) | |
download | cpython-fbffd70328bb2a73d8154b4dd944e6d707fde9b3.tar.gz cpython-fbffd70328bb2a73d8154b4dd944e6d707fde9b3.zip |
GH-128520: pathlib ABCs: raise text encoding warnings at correct stack level (#133051)
Ensure that warnings about unspecified text encodings are emitted from
`ReadablePath.read_text()`, `WritablePath.write_text()` and `magic_open()`
with the correct stack level set.
Diffstat (limited to 'Lib/test/test_pathlib/test_read.py')
-rw-r--r-- | Lib/test/test_pathlib/test_read.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib/test_read.py b/Lib/test/test_pathlib/test_read.py index 753ae5d760a..9bb5535a6eb 100644 --- a/Lib/test/test_pathlib/test_read.py +++ b/Lib/test/test_pathlib/test_read.py @@ -4,6 +4,7 @@ Tests for pathlib.types._ReadablePath import collections.abc import io +import sys import unittest from .support import is_pypi @@ -35,6 +36,17 @@ class ReadTestBase: self.assertIsInstance(f, io.TextIOBase) self.assertEqual(f.read(), 'this is file A\n') + @unittest.skipIf( + not getattr(sys.flags, 'warn_default_encoding', 0), + "Requires warn_default_encoding", + ) + def test_open_r_encoding_warning(self): + p = self.root / 'fileA' + with self.assertWarns(EncodingWarning) as wc: + with magic_open(p, 'r'): + pass + self.assertEqual(wc.filename, __file__) + def test_open_rb(self): p = self.root / 'fileA' with magic_open(p, 'rb') as f: @@ -55,6 +67,16 @@ class ReadTestBase: self.assertEqual(q.read_text(encoding='latin-1'), 'äbcdefg') self.assertEqual(q.read_text(encoding='utf-8', errors='ignore'), 'bcdefg') + @unittest.skipIf( + not getattr(sys.flags, 'warn_default_encoding', 0), + "Requires warn_default_encoding", + ) + def test_read_text_encoding_warning(self): + p = self.root / 'fileA' + with self.assertWarns(EncodingWarning) as wc: + p.read_text() + self.assertEqual(wc.filename, __file__) + def test_read_text_with_newlines(self): p = self.root / 'abc' self.ground.create_file(p, b'abcde\r\nfghlk\n\rmnopq') |