Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2bd942c

Browse files
authored
Merge pull request #10481 from jkseppan/dviread-python3
Remove python 2 compatibility code from dviread
2 parents 4dadbd2 + ea21cc6 commit 2bd942c

File tree

2 files changed

+22
-54
lines changed

2 files changed

+22
-54
lines changed

lib/matplotlib/dviread.py

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@
1717
...
1818
1919
"""
20-
from __future__ import absolute_import, division, print_function
21-
22-
import six
23-
from six.moves import xrange
24-
2520
from collections import namedtuple
26-
from functools import partial, wraps
21+
from functools import lru_cache, partial, wraps
2722
import logging
2823
import numpy as np
2924
import os
@@ -35,15 +30,6 @@
3530
from matplotlib import cbook, rcParams
3631
from matplotlib.compat import subprocess
3732

38-
try:
39-
from functools import lru_cache
40-
except ImportError: # Py2
41-
from backports.functools_lru_cache import lru_cache
42-
43-
if six.PY3:
44-
def ord(x):
45-
return x
46-
4733
_log = logging.getLogger(__name__)
4834

4935
# Dvi is a bytecode format documented in
@@ -172,7 +158,7 @@ def wrapper(self, byte):
172158
if max is None:
173159
table[min] = wrapper
174160
else:
175-
for i in xrange(min, max+1):
161+
for i in range(min, max+1):
176162
assert table[i] is None
177163
table[i] = wrapper
178164
return wrapper
@@ -194,7 +180,7 @@ class Dvi(object):
194180
>>> print(''.join(unichr(t.glyph) for t in page.text))
195181
"""
196182
# dispatch table
197-
_dtable = [None for _ in xrange(256)]
183+
_dtable = [None] * 256
198184
_dispatch = partial(_dispatch, _dtable)
199185

200186
def __init__(self, filename, dpi):
@@ -311,7 +297,7 @@ def _read(self):
311297
False if there were no more pages.
312298
"""
313299
while True:
314-
byte = ord(self.file.read(1)[0])
300+
byte = self.file.read(1)[0]
315301
self._dtable[byte](self, byte)
316302
if byte == 140: # end of page
317303
return True
@@ -325,11 +311,11 @@ def _arg(self, nbytes, signed=False):
325311
Signedness is determined by the *signed* keyword.
326312
"""
327313
str = self.file.read(nbytes)
328-
value = ord(str[0])
314+
value = str[0]
329315
if signed and value >= 0x80:
330316
value = value - 0x100
331317
for i in range(1, nbytes):
332-
value = 0x100*value + ord(str[i])
318+
value = 0x100*value + str[i]
333319
return value
334320

335321
@_dispatch(min=0, max=127, state=_dvistate.inpage)
@@ -445,14 +431,9 @@ def _fnt_num(self, new_f):
445431
@_dispatch(min=239, max=242, args=('ulen1',))
446432
def _xxx(self, datalen):
447433
special = self.file.read(datalen)
448-
if six.PY3:
449-
chr_ = chr
450-
else:
451-
def chr_(x):
452-
return x
453434
_log.debug(
454435
'Dvi._xxx: encountered special: %s',
455-
''.join([chr_(ch) if 32 <= ord(ch) < 127 else '<%02x>' % ord(ch)
436+
''.join([chr(ch) if 32 <= ch < 127 else '<%02x>' % ch
456437
for ch in special]))
457438

458439
@_dispatch(min=243, max=246, args=('olen1', 'u4', 'u4', 'u4', 'u1', 'u1'))
@@ -464,11 +445,7 @@ def _fnt_def_real(self, k, c, s, d, a, l):
464445
fontname = n[-l:].decode('ascii')
465446
tfm = _tfmfile(fontname)
466447
if tfm is None:
467-
if six.PY2:
468-
error_class = OSError
469-
else:
470-
error_class = FileNotFoundError
471-
raise error_class("missing font metrics file: %s" % fontname)
448+
raise FileNotFoundError("missing font metrics file: %s" % fontname)
472449
if c != 0 and tfm.checksum != 0 and c != tfm.checksum:
473450
raise ValueError('tfm checksum mismatch: %s' % n)
474451

@@ -561,7 +538,7 @@ def __init__(self, scale, tfm, texname, vf):
561538
except ValueError:
562539
nchars = 0
563540
self.widths = [(1000*tfm.width.get(char, 0)) >> 20
564-
for char in xrange(nchars)]
541+
for char in range(nchars)]
565542

566543
def __eq__(self, other):
567544
return self.__class__ == other.__class__ and \
@@ -643,7 +620,7 @@ def _read(self):
643620
packet_char, packet_ends = None, None
644621
packet_len, packet_width = None, None
645622
while True:
646-
byte = ord(self.file.read(1)[0])
623+
byte = self.file.read(1)[0]
647624
# If we are in a packet, execute the dvi instructions
648625
if self.state == _dvistate.inpage:
649626
byte_at = self.file.tell()-1
@@ -774,9 +751,9 @@ def __init__(self, filename):
774751
widths, heights, depths = \
775752
[struct.unpack('!%dI' % (len(x)/4), x)
776753
for x in (widths, heights, depths)]
777-
for idx, char in enumerate(xrange(bc, ec+1)):
778-
byte0 = ord(char_info[4*idx])
779-
byte1 = ord(char_info[4*idx+1])
754+
for idx, char in enumerate(range(bc, ec+1)):
755+
byte0 = char_info[4*idx]
756+
byte1 = char_info[4*idx+1]
780757
self.width[char] = _fix2comp(widths[byte0])
781758
self.height[char] = _fix2comp(heights[byte1 >> 4])
782759
self.depth[char] = _fix2comp(depths[byte1 & 0xf])
@@ -836,7 +813,7 @@ class PsfontsMap(object):
836813
def __init__(self, filename):
837814
self._font = {}
838815
self._filename = filename
839-
if six.PY3 and isinstance(filename, bytes):
816+
if isinstance(filename, bytes):
840817
encoding = sys.getfilesystemencoding() or 'utf-8'
841818
self._filename = filename.decode(encoding, errors='replace')
842819
with open(filename, 'rb') as file:
@@ -1024,25 +1001,19 @@ def find_tex_file(filename, format=None):
10241001
The library that :program:`kpsewhich` is part of.
10251002
"""
10261003

1027-
if six.PY3:
1028-
# we expect these to always be ascii encoded, but use utf-8
1029-
# out of caution
1030-
if isinstance(filename, bytes):
1031-
filename = filename.decode('utf-8', errors='replace')
1032-
if isinstance(format, bytes):
1033-
format = format.decode('utf-8', errors='replace')
1004+
# we expect these to always be ascii encoded, but use utf-8
1005+
# out of caution
1006+
if isinstance(filename, bytes):
1007+
filename = filename.decode('utf-8', errors='replace')
1008+
if isinstance(format, bytes):
1009+
format = format.decode('utf-8', errors='replace')
10341010

10351011
cmd = ['kpsewhich']
10361012
if format is not None:
10371013
cmd += ['--format=' + format]
10381014
cmd += [filename]
10391015
_log.debug('find_tex_file(%s): %s', filename, cmd)
1040-
# stderr is unused, but reading it avoids a subprocess optimization
1041-
# that breaks EINTR handling in some Python versions:
1042-
# http://bugs.python.org/issue12493
1043-
# https://github.com/matplotlib/matplotlib/issues/633
1044-
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
1045-
stderr=subprocess.PIPE)
1016+
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE)
10461017
result = pipe.communicate()[0].rstrip()
10471018
_log.debug('find_tex_file result: %s', result)
10481019
return result.decode('ascii')

lib/matplotlib/tests/test_dviread.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from __future__ import absolute_import, division, print_function
2-
3-
import six
41
from matplotlib.testing.decorators import skip_if_command_unavailable
52

63
import matplotlib.dviread as dr
@@ -64,7 +61,7 @@ def test_dviread():
6461
for [a, b, c, d, e] in entry['text']]
6562
with dr.Dvi(os.path.join(dir, 'test.dvi'), None) as dvi:
6663
data = [{'text': [[t.x, t.y,
67-
six.unichr(t.glyph),
64+
chr(t.glyph),
6865
t.font.texname,
6966
round(t.font.size, 2)]
7067
for t in page.text],

0 commit comments

Comments
 (0)