Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 6, 2024
1 parent 7e99e96 commit b3f8ee4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
38 changes: 5 additions & 33 deletions Lib/encodings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
36 changes: 36 additions & 0 deletions Lib/encodings/_win_cp_codecs.py
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit b3f8ee4

Please sign in to comment.