From b3f8ee4705c9c46f6f6249dcb11592f430ce8178 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 6 Nov 2024 10:02:47 +0200 Subject: [PATCH] Address review comments. --- Lib/encodings/__init__.py | 38 +++++---------------------------- Lib/encodings/_win_cp_codecs.py | 36 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 Lib/encodings/_win_cp_codecs.py diff --git a/Lib/encodings/__init__.py b/Lib/encodings/__init__.py index 9fa31f6b199cd1..3e5e0b1882c633 100644 --- a/Lib/encodings/__init__.py +++ b/Lib/encodings/__init__.py @@ -156,7 +156,9 @@ def search_function(encoding): codecs.register(search_function) if sys.platform == 'win32': - def _code_page_search_function(encoding): + from _win_cp_codecs import create_win32_code_page_codec + + def win32_code_page_search_function(encoding): encoding = encoding.lower() if not encoding.startswith('cp'): return None @@ -170,36 +172,6 @@ def _code_page_search_function(encoding): except (OverflowError, OSError): return None - def encode(input, errors='strict'): - return codecs.code_page_encode(cp, input, errors) - - def decode(input, errors='strict'): - return codecs.code_page_decode(cp, input, errors, True) - - class IncrementalEncoder(codecs.IncrementalEncoder): - def encode(self, input, final=False): - return codecs.code_page_encode(cp, input, self.errors)[0] - - class IncrementalDecoder(codecs.BufferedIncrementalDecoder): - def _buffer_decode(self, input, errors, final): - return codecs.code_page_decode(cp, input, errors, final) - - class StreamWriter(codecs.StreamWriter): - def encode(self, input, errors='strict'): - return codecs.code_page_encode(cp, input, errors) - - class StreamReader(codecs.StreamReader): - def decode(self, input, errors, final): - return codecs.code_page_decode(cp, input, errors, final) - - return codecs.CodecInfo( - name=f'cp{cp}', - encode=encode, - decode=decode, - incrementalencoder=IncrementalEncoder, - incrementaldecoder=IncrementalDecoder, - streamreader=StreamReader, - streamwriter=StreamWriter, - ) + return create_win32_code_page_codec(cp) - codecs.register(_code_page_search_function) + codecs.register(win32_code_page_search_function) diff --git a/Lib/encodings/_win_cp_codecs.py b/Lib/encodings/_win_cp_codecs.py new file mode 100644 index 00000000000000..a3e8f17390b047 --- /dev/null +++ b/Lib/encodings/_win_cp_codecs.py @@ -0,0 +1,36 @@ +import codecs + +def create_win32_code_page_codec(cp): + from codecs import code_page_decode, code_page_decode + + def encode(input, errors='strict'): + return code_page_encode(cp, input, errors) + + def decode(input, errors='strict'): + return code_page_decode(cp, input, errors, True) + + class IncrementalEncoder(codecs.IncrementalEncoder): + def encode(self, input, final=False): + return code_page_encode(cp, input, self.errors)[0] + + class IncrementalDecoder(codecs.BufferedIncrementalDecoder): + def _buffer_decode(self, input, errors, final): + return code_page_decode(cp, input, errors, final) + + class StreamWriter(codecs.StreamWriter): + def encode(self, input, errors='strict'): + return code_page_encode(cp, input, errors) + + class StreamReader(codecs.StreamReader): + def decode(self, input, errors, final): + return code_page_decode(cp, input, errors, final) + + return codecs.CodecInfo( + name=f'cp{cp}', + encode=encode, + decode=decode, + incrementalencoder=IncrementalEncoder, + incrementaldecoder=IncrementalDecoder, + streamreader=StreamReader, + streamwriter=StreamWriter, + )