| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6f04325 commit fbffd70
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,8 +3,8 @@ | |||
| 3 | 3 | """ | |
| 4 | 4 | ||
| 5 | 5 | from errno import * | |
| 6 | + from io import TextIOWrapper, text_encoding | ||
| 6 | 7 | from stat import S_ISDIR, S_ISREG, S_ISLNK, S_IMODE | |
| 7 | - import io | ||
| 8 | 8 | import os | |
| 9 | 9 | import sys | |
| 10 | 10 | try: | |
@@ -172,12 +172,16 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None, | |||
| 172 | 172 | Open the file pointed to by this path and return a file object, as | |
| 173 | 173 | the built-in open() function does. | |
| 174 | 174 | """ | |
| 175 | + text = 'b' not in mode | ||
| 176 | + if text: | ||
| 177 | + # Call io.text_encoding() here to ensure any warning is raised at an | ||
| 178 | + # appropriate stack level. | ||
| 179 | + encoding = text_encoding(encoding) | ||
| 175 | 180 | try: | |
| 176 | - return io.open(path, mode, buffering, encoding, errors, newline) | ||
| 181 | + return open(path, mode, buffering, encoding, errors, newline) | ||
| 177 | 182 | except TypeError: | |
| 178 | 183 | pass | |
| 179 | 184 | cls = type(path) | |
| 180 | - text = 'b' not in mode | ||
| 181 | 185 | mode = ''.join(sorted(c for c in mode if c not in 'bt')) | |
| 182 | 186 | if text: | |
| 183 | 187 | try: | |
@@ -200,7 +204,7 @@ def magic_open(path, mode='r', buffering=-1, encoding=None, errors=None, | |||
| 200 | 204 | else: | |
| 201 | 205 | stream = attr(path, buffering) | |
| 202 | 206 | if text: | |
| 203 | - stream = io.TextIOWrapper(stream, encoding, errors, newline) | ||
| 207 | + stream = TextIOWrapper(stream, encoding, errors, newline) | ||
| 204 | 208 | return stream | |
| 205 | 209 | ||
| 206 | 210 | raise TypeError(f"{cls.__name__} can't be opened with mode {mode!r}") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ | |||
| 12 | 12 | ||
| 13 | 13 | from abc import ABC, abstractmethod | |
| 14 | 14 | from glob import _PathGlobber | |
| 15 | + from io import text_encoding | ||
| 15 | 16 | from pathlib._os import magic_open, ensure_distinct_paths, ensure_different_files, copyfileobj | |
| 16 | 17 | from pathlib import PurePath, Path | |
| 17 | 18 | from typing import Optional, Protocol, runtime_checkable | |
@@ -262,6 +263,9 @@ def read_text(self, encoding=None, errors=None, newline=None): | |||
| 262 | 263 | """ | |
| 263 | 264 | Open the file in text mode, read it, and close the file. | |
| 264 | 265 | """ | |
| 266 | + # Call io.text_encoding() here to ensure any warning is raised at an | ||
| 267 | + # appropriate stack level. | ||
| 268 | + encoding = text_encoding(encoding) | ||
| 265 | 269 | with magic_open(self, mode='r', encoding=encoding, errors=errors, newline=newline) as f: | |
| 266 | 270 | return f.read() | |
| 267 | 271 | ||
@@ -391,6 +395,9 @@ def write_text(self, data, encoding=None, errors=None, newline=None): | |||
| 391 | 395 | """ | |
| 392 | 396 | Open the file in text mode, write to it, and close the file. | |
| 393 | 397 | """ | |
| 398 | + # Call io.text_encoding() here to ensure any warning is raised at an | ||
| 399 | + # appropriate stack level. | ||
| 400 | + encoding = text_encoding(encoding) | ||
| 394 | 401 | if not isinstance(data, str): | |
| 395 | 402 | raise TypeError('data must be str, not %s' % | |
| 396 | 403 | data.__class__.__name__) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | 5 | import collections.abc | |
| 6 | 6 | import io | |
| 7 | + import sys | ||
| 7 | 8 | import unittest | |
| 8 | 9 | ||
| 9 | 10 | from .support import is_pypi | |
@@ -35,6 +36,17 @@ def test_open_r(self): | |||
| 35 | 36 | self.assertIsInstance(f, io.TextIOBase) | |
| 36 | 37 | self.assertEqual(f.read(), 'this is file A\n') | |
| 37 | 38 | ||
| 39 | + @unittest.skipIf( | ||
| 40 | + not getattr(sys.flags, 'warn_default_encoding', 0), | ||
| 41 | + "Requires warn_default_encoding", | ||
| 42 | + ) | ||
| 43 | + def test_open_r_encoding_warning(self): | ||
| 44 | + p = self.root / 'fileA' | ||
| 45 | + with self.assertWarns(EncodingWarning) as wc: | ||
| 46 | + with magic_open(p, 'r'): | ||
| 47 | + pass | ||
| 48 | + self.assertEqual(wc.filename, __file__) | ||
| 49 | + | ||
| 38 | 50 | def test_open_rb(self): | |
| 39 | 51 | p = self.root / 'fileA' | |
| 40 | 52 | with magic_open(p, 'rb') as f: | |
@@ -55,6 +67,16 @@ def test_read_text(self): | |||
| 55 | 67 | self.assertEqual(q.read_text(encoding='latin-1'), 'äbcdefg') | |
| 56 | 68 | self.assertEqual(q.read_text(encoding='utf-8', errors='ignore'), 'bcdefg') | |
| 57 | 69 | ||
| 70 | + @unittest.skipIf( | ||
| 71 | + not getattr(sys.flags, 'warn_default_encoding', 0), | ||
| 72 | + "Requires warn_default_encoding", | ||
| 73 | + ) | ||
| 74 | + def test_read_text_encoding_warning(self): | ||
| 75 | + p = self.root / 'fileA' | ||
| 76 | + with self.assertWarns(EncodingWarning) as wc: | ||
| 77 | + p.read_text() | ||
| 78 | + self.assertEqual(wc.filename, __file__) | ||
| 79 | + | ||
| 58 | 80 | def test_read_text_with_newlines(self): | |
| 59 | 81 | p = self.root / 'abc' | |
| 60 | 82 | self.ground.create_file(p, b'abcde\r\nfghlk\n\rmnopq') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | 5 | import io | |
| 6 | 6 | import os | |
| 7 | + import sys | ||
| 7 | 8 | import unittest | |
| 8 | 9 | ||
| 9 | 10 | from .support import is_pypi | |
@@ -35,6 +36,17 @@ def test_open_w(self): | |||
| 35 | 36 | f.write('this is file A\n') | |
| 36 | 37 | self.assertEqual(self.ground.readtext(p), 'this is file A\n') | |
| 37 | 38 | ||
| 39 | + @unittest.skipIf( | ||
| 40 | + not getattr(sys.flags, 'warn_default_encoding', 0), | ||
| 41 | + "Requires warn_default_encoding", | ||
| 42 | + ) | ||
| 43 | + def test_open_w_encoding_warning(self): | ||
| 44 | + p = self.root / 'fileA' | ||
| 45 | + with self.assertWarns(EncodingWarning) as wc: | ||
| 46 | + with magic_open(p, 'w'): | ||
| 47 | + pass | ||
| 48 | + self.assertEqual(wc.filename, __file__) | ||
| 49 | + | ||
| 38 | 50 | def test_open_wb(self): | |
| 39 | 51 | p = self.root / 'fileA' | |
| 40 | 52 | with magic_open(p, 'wb') as f: | |
@@ -61,6 +73,16 @@ def test_write_text(self): | |||
| 61 | 73 | self.assertRaises(TypeError, p.write_text, b'somebytes') | |
| 62 | 74 | self.assertEqual(self.ground.readbytes(p), b'\xe4bcdefg') | |
| 63 | 75 | ||
| 76 | + @unittest.skipIf( | ||
| 77 | + not getattr(sys.flags, 'warn_default_encoding', 0), | ||
| 78 | + "Requires warn_default_encoding", | ||
| 79 | + ) | ||
| 80 | + def test_write_text_encoding_warning(self): | ||
| 81 | + p = self.root / 'fileA' | ||
| 82 | + with self.assertWarns(EncodingWarning) as wc: | ||
| 83 | + p.write_text('abcdefg') | ||
| 84 | + self.assertEqual(wc.filename, __file__) | ||
| 85 | + | ||
| 64 | 86 | def test_write_text_with_newlines(self): | |
| 65 | 87 | # Check that `\n` character change nothing | |
| 66 | 88 | p = self.root / 'fileA' | |
| Back | FazBrowse Home | New Git URL |
0 commit comments