Skip to content

bpo-36778: Avoid functools in encodings.cp65001 #13110

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Lib/encodings/cp65001.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""

import codecs
import functools

if not hasattr(codecs, 'code_page_encode'):
raise LookupError("cp65001 encoding is only available on Windows")

### Codec APIs

encode = functools.partial(codecs.code_page_encode, 65001)
_decode = functools.partial(codecs.code_page_decode, 65001)
encode = lambda *args, **kw: codecs.code_page_encode(65001, *args, **kw)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please measure how much overhead it adds?

_decode = lambda *args, **kw: codecs.code_page_decode(65001, *args, **kw)

def decode(input, errors='strict'):
return codecs.code_page_decode(65001, input, errors, True)
Expand All @@ -21,13 +20,13 @@ def encode(self, input, final=False):
return encode(input, self.errors)[0]

class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
_buffer_decode = _decode
_buffer_decode = staticmethod(_decode)

class StreamWriter(codecs.StreamWriter):
encode = encode
encode = staticmethod(encode)

class StreamReader(codecs.StreamReader):
decode = _decode
decode = staticmethod(_decode)

### encodings module API

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
encodings.cp65001 (code page 65001, CP_UTF8) implementation now avoids
functools.partial() to reduce the number of imports at startup.