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_write.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_write.py')
-rw-r--r-- | Lib/test/test_pathlib/test_write.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib/test_write.py b/Lib/test/test_pathlib/test_write.py index d302e0a9caa..2f3c06b433d 100644 --- a/Lib/test/test_pathlib/test_write.py +++ b/Lib/test/test_pathlib/test_write.py @@ -4,6 +4,7 @@ Tests for pathlib.types._WritablePath import io import os +import sys import unittest from .support import is_pypi @@ -35,6 +36,17 @@ class WriteTestBase: f.write('this is file A\n') self.assertEqual(self.ground.readtext(p), 'this is file A\n') + @unittest.skipIf( + not getattr(sys.flags, 'warn_default_encoding', 0), + "Requires warn_default_encoding", + ) + def test_open_w_encoding_warning(self): + p = self.root / 'fileA' + with self.assertWarns(EncodingWarning) as wc: + with magic_open(p, 'w'): + pass + self.assertEqual(wc.filename, __file__) + def test_open_wb(self): p = self.root / 'fileA' with magic_open(p, 'wb') as f: @@ -61,6 +73,16 @@ class WriteTestBase: self.assertRaises(TypeError, p.write_text, b'somebytes') self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') + @unittest.skipIf( + not getattr(sys.flags, 'warn_default_encoding', 0), + "Requires warn_default_encoding", + ) + def test_write_text_encoding_warning(self): + p = self.root / 'fileA' + with self.assertWarns(EncodingWarning) as wc: + p.write_text('abcdefg') + self.assertEqual(wc.filename, __file__) + def test_write_text_with_newlines(self): # Check that `\n` character change nothing p = self.root / 'fileA' |