Skip to content

Commit d2359c5

Browse files
Begin removing Python 2 compatibility code (#392)
This is not comprehensive because there is no coverage report to confirm what code paths are exercised by the test suite. However, it is a start. Co-authored-by: Jessica Tegner <jessica@jessicategner.com>
1 parent b08d432 commit d2359c5

File tree

4 files changed

+16
-44
lines changed

4 files changed

+16
-44
lines changed

pypandoc/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from .handler import logger, _check_log_handler
1717
from .pandoc_download import DEFAULT_TARGET_FOLDER, download_pandoc
18-
from .py3compat import cast_bytes, cast_unicode, string_types, url2path, urlparse
18+
from .py3compat import cast_bytes, cast_unicode, url2path, urlparse
1919

2020
__author__ = u'Juho Vepsäläinen'
2121
__author_email__ = "bebraw@gmail.com"
@@ -395,7 +395,7 @@ def _convert_input(source, format, input_type, to, extra_args=(),
395395

396396
# adds the proper filter syntax for each item in the filters list
397397
if filters is not None:
398-
if isinstance(filters, string_types):
398+
if isinstance(filters, str):
399399
filters = filters.split()
400400
f = ['--lua-filter=' + x if x.endswith(".lua") else '--filter=' + x for x in filters]
401401
args.extend(f)

pypandoc/pandoc_download.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ def _get_pandoc_urls(version="latest"):
6969
'pkg': 'darwin'
7070
}
7171
# parse pandoc_urls from list to dict
72-
# py26 don't like dict comprehension. Use this one instead when py26 support is dropped
73-
pandoc_urls = {ext2platform[url_frag[-3:]]: (f"https://github.com{url_frag}") for url_frag in pandoc_urls_list}
72+
pandoc_urls = {ext2platform[url_frag[-3:]]: f"https://github.com{url_frag}" for url_frag in pandoc_urls_list}
7473
return pandoc_urls, version
7574

7675

pypandoc/py3compat.py

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,37 +41,14 @@ def cast_bytes(s, encoding=None):
4141
return s
4242

4343

44-
if sys.version_info[0] >= 3:
45-
PY3 = True
44+
# from http://stackoverflow.com/questions/11687478/convert-a-filename-to-a-file-url
45+
from urllib.parse import urljoin, urlparse
46+
from urllib.request import pathname2url, url2pathname
4647

47-
string_types = (str,)
48-
unicode_type = str
4948

50-
# from http://stackoverflow.com/questions/11687478/convert-a-filename-to-a-file-url
51-
from urllib.parse import urljoin, urlparse
52-
from urllib.request import pathname2url, url2pathname
49+
def path2url(path): # noqa: E303
50+
return urljoin('file:', pathname2url(path))
5351

5452

55-
def path2url(path): # noqa: E303
56-
return urljoin('file:', pathname2url(path))
57-
58-
59-
def url2path(url): # noqa: E303
60-
return url2pathname(urlparse(url).path)
61-
62-
else:
63-
PY3 = False
64-
65-
string_types = (str, unicode) # noqa: F821
66-
unicode_type = unicode # noqa: F821
67-
68-
from urlparse import urljoin, urlparse
69-
import urllib
70-
71-
72-
def path2url(path): # noqa: E303
73-
return urljoin('file:', urllib.pathname2url(path))
74-
75-
76-
def url2path(url): # noqa: E303
77-
return urllib.url2pathname(urlparse(url).path)
53+
def url2path(url): # noqa: E303
54+
return url2pathname(urlparse(url).path)

tests.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pathlib import Path
1717

1818
import pypandoc
19-
from pypandoc.py3compat import path2url, string_types, unicode_type
19+
from pypandoc.py3compat import path2url
2020

2121

2222
@contextlib.contextmanager
@@ -53,7 +53,7 @@ def closed_tempfile(suffix, text=None, dir_name=None, prefix=None):
5353
# Stolen from pandas
5454
def is_list_like(arg):
5555
return (hasattr(arg, '__iter__') and
56-
not isinstance(arg, string_types))
56+
not isinstance(arg, str))
5757

5858

5959
@contextlib.contextmanager
@@ -129,10 +129,6 @@ def assert_produces_warning(expected_warning=Warning, filter_level="always",
129129

130130
class TestPypandoc(unittest.TestCase):
131131

132-
# Python 2 compatibility
133-
if not hasattr(unittest.TestCase, 'assertRaisesRegex'):
134-
assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
135-
136132
def setUp(self):
137133
if 'HOME' not in os.environ:
138134
# if this is used with older versions of pandoc-citeproc
@@ -155,7 +151,7 @@ def test_get_pandoc_formats(self):
155151
def test_get_pandoc_version(self):
156152
assert "HOME" in os.environ, "No HOME set, this will error..."
157153
version = pypandoc.get_pandoc_version()
158-
self.assertTrue(isinstance(version, pypandoc.string_types))
154+
self.assertTrue(isinstance(version, str))
159155
major = int(version.split(".")[0])
160156
self.assertTrue(major in [0, 1, 2, 3])
161157

@@ -524,12 +520,12 @@ def test_unicode_input(self):
524520
# make sure that pandoc always returns unicode and does not mishandle it
525521
expected = u'üäöîôû{0}'.format(os.linesep)
526522
written = pypandoc.convert_text(u'<p>üäöîôû</p>', 'md', format='html')
527-
self.assertTrue(isinstance(written, unicode_type))
523+
self.assertTrue(isinstance(written, str))
528524
self.assertEqualExceptForNewlineEnd(expected, written)
529525
bytes = u'<p>üäöîôû</p>'.encode("utf-8")
530526
written = pypandoc.convert_text(bytes, 'md', format='html')
531527
self.assertTrue(expected == written)
532-
self.assertTrue(isinstance(written, unicode_type))
528+
self.assertTrue(isinstance(written, str))
533529

534530
# Only use german umlauts in the next test, as iso-8859-15 covers that
535531
expected = u'äüäö{0}'.format(os.linesep)
@@ -550,7 +546,7 @@ def f():
550546
# with the right encoding it should work...
551547
written = pypandoc.convert_text(bytes, 'md', format='html', encoding="iso-8859-15")
552548
self.assertEqualExceptForNewlineEnd(expected, written)
553-
self.assertTrue(isinstance(written, unicode_type))
549+
self.assertTrue(isinstance(written, str))
554550

555551
def test_conversion_from_non_plain_text_file(self):
556552
with closed_tempfile('.docx') as file_name:

0 commit comments

Comments
 (0)