Skip to content

bpo-31722: Update codecs.IncrementalDecoder to use __subclasshook__ #16664

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 2 commits 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
16 changes: 15 additions & 1 deletion Lib/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

"""

import abc
import builtins
import sys
import _collections_abc

### Registry and builtin stateless codec functions

Expand Down Expand Up @@ -251,7 +253,7 @@ def getstate(self):
def setstate(self, state):
self.buffer = state or ""

class IncrementalDecoder(object):
class IncrementalDecoder(abc.ABC):
"""
An IncrementalDecoder decodes an input in multiple steps. The input can
be passed piece by piece to the decode() method. The IncrementalDecoder
Expand Down Expand Up @@ -300,6 +302,13 @@ def setstate(self, state):
setstate((b"", 0)) must be equivalent to reset().
"""

@classmethod
def __subclasshook__(cls, C):
if issubclass(C, _io.IncrementalNewlineDecoder):
return _collections_abc._check_methods(C, "decode", "reset",
"getstate", "setstate")
return NotImplemented

class BufferedIncrementalDecoder(IncrementalDecoder):
"""
This subclass of IncrementalDecoder can be used as the baseclass for an
Expand Down Expand Up @@ -1110,6 +1119,11 @@ def make_encoding_map(decoding_map):
if _false:
import encodings

try:
import _io
IncrementalDecoder.register(_io.IncrementalNewlineDecoder)
except ImportError:
pass
### Tests

if __name__ == '__main__':
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4177,6 +4177,9 @@ def test_check_encoding_errors(self):
proc = assert_python_failure('-X', 'dev', '-c', code)
self.assertEqual(proc.rc, 10, proc)

def test_subclass_of_codecs_incremental_decoder(self):
self.assertTrue(self.io.IncrementalNewlineDecoder,
codecs.IncrementalDecoder)

class CMiscIOTest(MiscIOTest):
io = io
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update :class:`codecs.IncrementalDecoder` to use :meth:`__subclasshook__` method.
(Contributed by Dong-hee Na in :issue:`31722`.)