Skip to content

Commit 8e517de

Browse files
authored
Correct the declarations of I/O buffers as bytes-based
* Correctly declare the parameter annotations as `BinaryIO`, because in fact it works only with byte-based buffers. * `sys.stdin.read()` is always a string, so remove the Python2-specific check. * Correct the patch in the test which incorrectly sets `sys.stdin` to a bytes buffer which it never is. * `sys.stdout.buffer` always exists, so remove the Python2-specific fallback. * Correct the patch in the test which incorrectly sets `sys.stdout` to a bytes buffer which it never is. And fix a matching mistake because this necessitated `stdout.read().decode()`.
1 parent 2932f18 commit 8e517de

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

docs/changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file.
88
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
99
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). See the [Contributing Guide](contributing.md) for details.
1010

11+
## [unreleased]
12+
13+
### Fixed
14+
15+
* Fix type annotations for `convertFile` - it accepts only bytes-based buffers.
16+
Also remove legacy checks from Python 2 (#1400)
17+
1118
## [3.5.1] -- 2023-10-31
1219

1320
### Fixed

markdown/core.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import sys
2424
import logging
2525
import importlib
26-
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Mapping, Sequence, TextIO
26+
from typing import TYPE_CHECKING, Any, BinaryIO, Callable, ClassVar, Mapping, Sequence
2727
from . import util
2828
from .preprocessors import build_preprocessors
2929
from .blockprocessors import build_block_parser
@@ -387,8 +387,8 @@ def convert(self, source: str) -> str:
387387

388388
def convertFile(
389389
self,
390-
input: str | TextIO | None = None,
391-
output: str | TextIO | None = None,
390+
input: str | BinaryIO | None = None,
391+
output: str | BinaryIO | None = None,
392392
encoding: str | None = None,
393393
) -> Markdown:
394394
"""
@@ -424,8 +424,6 @@ def convertFile(
424424
input_file.close()
425425
else:
426426
text = sys.stdin.read()
427-
if not isinstance(text, str): # pragma: no cover
428-
text = text.decode(encoding)
429427

430428
text = text.lstrip('\ufeff') # remove the byte-order mark
431429

@@ -448,12 +446,7 @@ def convertFile(
448446
else:
449447
# Encode manually and write bytes to stdout.
450448
html = html.encode(encoding, "xmlcharrefreplace")
451-
try:
452-
# Write bytes directly to buffer (Python 3).
453-
sys.stdout.buffer.write(html)
454-
except AttributeError: # pragma: no cover
455-
# Probably Python 2, which works with bytes by default.
456-
sys.stdout.write(html)
449+
sys.stdout.buffer.write(html)
457450

458451
return self
459452

tests/test_apis.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from logging import DEBUG, WARNING, CRITICAL
3434
import yaml
3535
import tempfile
36-
from io import BytesIO
36+
from io import BytesIO, StringIO, TextIOWrapper
3737
import xml.etree.ElementTree as etree
3838
from xml.etree.ElementTree import ProcessingInstruction
3939

@@ -80,8 +80,8 @@ class TestConvertFile(unittest.TestCase):
8080

8181
def setUp(self):
8282
self.saved = sys.stdin, sys.stdout
83-
sys.stdin = BytesIO(bytes('foo', encoding='utf-8'))
84-
sys.stdout = BytesIO()
83+
sys.stdin = StringIO('foo')
84+
sys.stdout = TextIOWrapper(BytesIO())
8585

8686
def tearDown(self):
8787
sys.stdin, sys.stdout = self.saved
@@ -111,7 +111,7 @@ def testFileObjects(self):
111111
def testStdinStdout(self):
112112
markdown.markdownFromFile()
113113
sys.stdout.seek(0)
114-
self.assertEqual(sys.stdout.read().decode('utf-8'), '<p>foo</p>')
114+
self.assertEqual(sys.stdout.read(), '<p>foo</p>')
115115

116116

117117
class TestBlockParser(unittest.TestCase):

0 commit comments

Comments
 (0)