From bbb0ff735dc5b24a96514c03747085301b5474dc Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Wed, 1 Nov 2023 15:39:39 +0100 Subject: [PATCH 1/2] 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()`. --- docs/changelog.md | 7 +++++++ markdown/core.py | 16 +++++----------- tests/test_apis.py | 8 ++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index ce9b6540c..72d568654 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). See the [Contributing Guide](contributing.md) for details. +## [unreleased] + +### Fixed + +* Fix type annotations for `convertFile` - it accepts only bytes-based buffers. + Also remove legacy checks from Python 2 (#1400) + ## [3.5.1] -- 2023-10-31 ### Fixed diff --git a/markdown/core.py b/markdown/core.py index 6b556b450..d052a86a5 100644 --- a/markdown/core.py +++ b/markdown/core.py @@ -23,7 +23,7 @@ import sys import logging import importlib -from typing import TYPE_CHECKING, Any, Callable, ClassVar, Mapping, Sequence, TextIO +from typing import TYPE_CHECKING, Any, BinaryIO, Callable, ClassVar, Mapping, Sequence from . import util from .preprocessors import build_preprocessors from .blockprocessors import build_block_parser @@ -387,8 +387,8 @@ def convert(self, source: str) -> str: def convertFile( self, - input: str | TextIO | None = None, - output: str | TextIO | None = None, + input: str | BinaryIO | None = None, + output: str | BinaryIO | None = None, encoding: str | None = None, ) -> Markdown: """ @@ -424,8 +424,6 @@ def convertFile( input_file.close() else: text = sys.stdin.read() - if not isinstance(text, str): # pragma: no cover - text = text.decode(encoding) text = text.lstrip('\ufeff') # remove the byte-order mark @@ -448,12 +446,8 @@ def convertFile( else: # Encode manually and write bytes to stdout. html = html.encode(encoding, "xmlcharrefreplace") - try: - # Write bytes directly to buffer (Python 3). - sys.stdout.buffer.write(html) - except AttributeError: # pragma: no cover - # Probably Python 2, which works with bytes by default. - sys.stdout.write(html) + # Write bytes directly to buffer (Python 3). + sys.stdout.buffer.write(html) return self diff --git a/tests/test_apis.py b/tests/test_apis.py index 1305c5476..d613a822f 100644 --- a/tests/test_apis.py +++ b/tests/test_apis.py @@ -33,7 +33,7 @@ from logging import DEBUG, WARNING, CRITICAL import yaml import tempfile -from io import BytesIO +from io import BytesIO, StringIO, TextIOWrapper import xml.etree.ElementTree as etree from xml.etree.ElementTree import ProcessingInstruction @@ -80,8 +80,8 @@ class TestConvertFile(unittest.TestCase): def setUp(self): self.saved = sys.stdin, sys.stdout - sys.stdin = BytesIO(bytes('foo', encoding='utf-8')) - sys.stdout = BytesIO() + sys.stdin = StringIO('foo') + sys.stdout = TextIOWrapper(BytesIO()) def tearDown(self): sys.stdin, sys.stdout = self.saved @@ -111,7 +111,7 @@ def testFileObjects(self): def testStdinStdout(self): markdown.markdownFromFile() sys.stdout.seek(0) - self.assertEqual(sys.stdout.read().decode('utf-8'), '

foo

') + self.assertEqual(sys.stdout.read(), '

foo

') class TestBlockParser(unittest.TestCase): From 9c48e139d3f820d355e104cd6d7ee0d6e54c4721 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Wed, 1 Nov 2023 16:14:06 +0100 Subject: [PATCH 2/2] Delete comment --- markdown/core.py | 1 - 1 file changed, 1 deletion(-) diff --git a/markdown/core.py b/markdown/core.py index d052a86a5..09a3924f9 100644 --- a/markdown/core.py +++ b/markdown/core.py @@ -446,7 +446,6 @@ def convertFile( else: # Encode manually and write bytes to stdout. html = html.encode(encoding, "xmlcharrefreplace") - # Write bytes directly to buffer (Python 3). sys.stdout.buffer.write(html) return self