GeezerVison is showing github.com — GH-110109: pathlib ABCs: drop use of `io.text_encoding()` (#113417) · python/cpython@f8b6e17
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/pathlib/__init__.py +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -270,6 +270,24 @@ def open(self, mode='r', buffering=-1, encoding=None, | 270 | 270 | encoding = io.text_encoding(encoding) | 271 | 271 | return io.open(self, mode, buffering, encoding, errors, newline) | 272 | 272 |
| | 273 | + def read_text(self, encoding=None, errors=None, newline=None): | | 274 | + """ | | 275 | + Open the file in text mode, read it, and close the file. | | 276 | + """ | | 277 | + # Call io.text_encoding() here to ensure any warning is raised at an | | 278 | + # appropriate stack level. | | 279 | + encoding = io.text_encoding(encoding) | | 280 | + return _abc.PathBase.read_text(self, encoding, errors, newline) | | 281 | + | | 282 | + def write_text(self, data, encoding=None, errors=None, newline=None): | | 283 | + """ | | 284 | + Open the file in text mode, write to it, and close the file. | | 285 | + """ | | 286 | + # Call io.text_encoding() here to ensure any warning is raised at an | | 287 | + # appropriate stack level. | | 288 | + encoding = io.text_encoding(encoding) | | 289 | + return _abc.PathBase.write_text(self, data, encoding, errors, newline) | | 290 | + | 273 | 291 | def iterdir(self): | 274 | 292 | """Yield path objects of the directory contents. | 275 | 293 |
| |
Collapse file Copy file name to clipboardExpand all lines: Lib/pathlib/_abc.py -3Lines changed: 0 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import functools | 2 | | -import io | 3 | 2 | import ntpath | 4 | 3 | import posixpath | 5 | 4 | import sys | @@ -755,7 +754,6 @@ def read_text(self, encoding=None, errors=None, newline=None): | 755 | 754 | """ | 756 | 755 | Open the file in text mode, read it, and close the file. | 757 | 756 | """ | 758 | | - encoding = io.text_encoding(encoding) | 759 | 757 | with self.open(mode='r', encoding=encoding, errors=errors, newline=newline) as f: | 760 | 758 | return f.read() | 761 | 759 |
| @@ -775,7 +773,6 @@ def write_text(self, data, encoding=None, errors=None, newline=None): | 775 | 773 | if not isinstance(data, str): | 776 | 774 | raise TypeError('data must be str, not %s' % | 777 | 775 | data.__class__.__name__) | 778 | | - encoding = io.text_encoding(encoding) | 779 | 776 | with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f: | 780 | 777 | return f.write(data) | 781 | 778 |
| |
|