Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
bpo-43510: Implement PEP 597 opt-in EncodingWarning.#19481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
a3c014b050bd1b939f4a03c997774016278c5c556cd9a08c2772648e1a8e30520966cd2b80f42760308ca95dff231fb411096a0a399fc9386fdbcbc3f362bc674feffd9d850f16463ea412d633ee883d16a15e2a5d474b4c17016f03f971c9d26b7a60e74cfa505b5fcbe22e2a9f9f043bea88fd260a4c049a269018ba643a9623e737059e6a6221154c7dc65b2830b14f2a6e06e2a3227d49d280f46446ad0e7f73b27f1c149d654eb7655e3bce76c089fd7File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -40,6 +40,29 @@ | ||
| _CHECK_ERRORS = _IOBASE_EMITS_UNRAISABLE | ||
| def text_encoding(encoding, stacklevel=2): | ||
| """ | ||
| A helper function to choose the text encoding. | ||
| When encoding is not None, just return it. | ||
| Otherwise, return the default text encoding (i.e. "locale"). | ||
| This function emits an EncodingWarning if *encoding* is None and | ||
| sys.flags.warn_default_encoding is true. | ||
| This can be used in APIs with an encoding=None parameter | ||
| that pass it to TextIOWrapper or open. | ||
| However, please consider using encoding="utf-8" for new APIs. | ||
| """ | ||
| if encoding is None: | ||
| encoding = "locale" | ||
| if sys.flags.warn_default_encoding: | ||
| import warnings | ||
| warnings.warn("'encoding' argument not specified.", | ||
| EncodingWarning, stacklevel + 1) | ||
| return encoding | ||
| def open(file, mode="r", buffering=-1, encoding=None, errors=None, | ||
| newline=None, closefd=True, opener=None): | ||
| @@ -248,6 +271,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None, | ||
| result = buffer | ||
| if binary: | ||
| return result | ||
| encoding = text_encoding(encoding) | ||
| text = TextIOWrapper(buffer, encoding, errors, newline, line_buffering) | ||
| result = text | ||
| text.mode = mode | ||
| @@ -2004,19 +2028,22 @@ class TextIOWrapper(TextIOBase): | ||
| def __init__(self, buffer, encoding=None, errors=None, newline=None, | ||
| line_buffering=False, write_through=False): | ||
| self._check_newline(newline) | ||
| encoding = text_encoding(encoding) | ||
| if encoding == "locale": | ||
| try: | ||
| encoding = os.device_encoding(buffer.fileno()) or "locale" | ||
| except (AttributeError, UnsupportedOperation): | ||
| pass | ||
| if encoding == "locale": | ||
| try: | ||
| import locale | ||
| except ImportError: | ||
| # Importing locale may fail if Python is being built | ||
| encoding = "utf-8" | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I saw what you did there! :-D Mention it in the final commit message (I didn't read your 24 commit messages, GitHub UI isn't convenient for that :-( ). | ||
| else: | ||
| encoding = locale.getpreferredencoding(False) | ||
methane marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if not isinstance(encoding, str): | ||
| raise ValueError("invalid encoding: %r" % encoding) | ||
Uh oh!
There was an error while loading.Please reload this page.