Skip to content

Commit

Permalink
Merge pull request #2187 from jdufresne/open-warning
Browse files Browse the repository at this point in the history
Avoid deprecated 'U' mode when opening files
  • Loading branch information
hugovk committed Jul 2, 2018
2 parents ae9f620 + 1ba1478 commit 4d88e28
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 61 deletions.
56 changes: 6 additions & 50 deletions Tests/test_file_eps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from helper import unittest, PillowTestCase, hopper

from PIL import Image, EpsImagePlugin
from PIL._util import py3
import io

# Our two EPS test files (they are identical except for their bounding boxes)
Expand Down Expand Up @@ -196,41 +195,15 @@ def _test_readline(self, t, ending):
self.assertEqual(t.readline().strip('\r\n'), 'baz', ending)
self.assertEqual(t.readline().strip('\r\n'), 'bif', ending)

def _test_readline_stringio(self, test_string, ending):
# check all the freaking line endings possible
try:
import StringIO
except ImportError:
# don't skip, it skips everything in the parent test
return
t = StringIO.StringIO(test_string)
def _test_readline_io_psfile(self, test_string, ending):
f = io.BytesIO(test_string.encode('latin-1'))
t = EpsImagePlugin.PSFile(f)
self._test_readline(t, ending)

def _test_readline_io(self, test_string, ending):
if py3:
t = io.StringIO(test_string)
else:
t = io.StringIO(unicode(test_string))
self._test_readline(t, ending)

def _test_readline_file_universal(self, test_string, ending):
f = self.tempfile('temp.txt')
with open(f, 'wb') as w:
if py3:
w.write(test_string.encode('UTF-8'))
else:
w.write(test_string)

with open(f, 'rU') as t:
self._test_readline(t, ending)

def _test_readline_file_psfile(self, test_string, ending):
f = self.tempfile('temp.txt')
with open(f, 'wb') as w:
if py3:
w.write(test_string.encode('UTF-8'))
else:
w.write(test_string)
w.write(test_string.encode('latin-1'))

with open(f, 'rb') as r:
t = EpsImagePlugin.PSFile(r)
Expand All @@ -239,29 +212,12 @@ def _test_readline_file_psfile(self, test_string, ending):
def test_readline(self):
# check all the freaking line endings possible from the spec
# test_string = u'something\r\nelse\n\rbaz\rbif\n'
line_endings = ['\r\n', '\n']
not_working_endings = ['\n\r', '\r']
line_endings = ['\r\n', '\n', '\n\r', '\r']
strings = ['something', 'else', 'baz', 'bif']

for ending in line_endings:
s = ending.join(strings)
# Native Python versions will pass these endings.
# self._test_readline_stringio(s, ending)
# self._test_readline_io(s, ending)
# self._test_readline_file_universal(s, ending)

self._test_readline_file_psfile(s, ending)

for ending in not_working_endings:
# these only work with the PSFile, while they're in spec,
# they're not likely to be used
s = ending.join(strings)

# Native Python versions may fail on these endings.
# self._test_readline_stringio(s, ending)
# self._test_readline_io(s, ending)
# self._test_readline_file_universal(s, ending)

self._test_readline_io_psfile(s, ending)
self._test_readline_file_psfile(s, ending)

def test_open_eps(self):
Expand Down
12 changes: 1 addition & 11 deletions src/PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import sys
from . import Image, ImageFile
from ._binary import i32le as i32
from ._util import py3

__version__ = "0.5"

Expand Down Expand Up @@ -206,16 +205,7 @@ def _open(self):

# Rewrap the open file pointer in something that will
# convert line endings and decode to latin-1.
try:
if py3:
# Python3, can use bare open command.
fp = open(self.fp.name, "Ur", encoding='latin-1')
else:
# Python2, no encoding conversion necessary
fp = open(self.fp.name, "Ur")
except:
# Expect this for bytesio/stringio
fp = PSFile(self.fp)
fp = PSFile(self.fp)

# go to offset - start of "%!PS"
fp.seek(offset)
Expand Down

0 comments on commit 4d88e28

Please sign in to comment.