Merge pull request #1365 from radarhere/psdraw

Fixed PSDraw stdout Python 3 compatibility
This commit is contained in:
Hugo van Kemenade 2015-07-31 19:12:47 +03:00
commit e442a72baa
4 changed files with 46 additions and 21 deletions

View File

@ -376,6 +376,7 @@ def _save(im, fp, filename, eps=1):
pass pass
base_fp = fp base_fp = fp
if fp != sys.stdout:
fp = NoCloseStream(fp) fp = NoCloseStream(fp)
if sys.version_info[0] > 2: if sys.version_info[0] > 2:
fp = io.TextIOWrapper(fp, encoding='latin-1') fp = io.TextIOWrapper(fp, encoding='latin-1')

View File

@ -461,6 +461,9 @@ def _save(im, fp, tile, bufsize=0):
# But, it would need at least the image size in most cases. RawEncode is # But, it would need at least the image size in most cases. RawEncode is
# a tricky case. # a tricky case.
bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c
if fp == sys.stdout:
fp.flush()
return
try: try:
fh = fp.fileno() fh = fp.fileno()
fp.flush() fp.flush()

View File

@ -16,11 +16,12 @@
# #
from PIL import EpsImagePlugin from PIL import EpsImagePlugin
import sys
## ##
# Simple Postscript graphics interface. # Simple Postscript graphics interface.
class PSDraw(object): class PSDraw(object):
""" """
Sets up printing to the given file. If **file** is omitted, Sets up printing to the given file. If **file** is omitted,
@ -29,12 +30,11 @@ class PSDraw(object):
def __init__(self, fp=None): def __init__(self, fp=None):
if not fp: if not fp:
import sys
fp = sys.stdout fp = sys.stdout
self.fp = fp self.fp = fp
def _fp_write(self, to_write): def _fp_write(self, to_write):
if bytes is str: if bytes is str or self.fp == sys.stdout:
self.fp.write(to_write) self.fp.write(to_write)
else: else:
self.fp.write(bytes(to_write, 'UTF-8')) self.fp.write(bytes(to_write, 'UTF-8'))
@ -47,7 +47,7 @@ class PSDraw(object):
"/showpage { } def\n" "/showpage { } def\n"
"%%EndComments\n" "%%EndComments\n"
"%%BeginDocument\n") "%%BeginDocument\n")
# self.fp_write(ERROR_PS) # debugging! # self._fp_write(ERROR_PS) # debugging!
self._fp_write(EDROFF_PS) self._fp_write(EDROFF_PS)
self._fp_write(VDI_PS) self._fp_write(VDI_PS)
self._fp_write("%%EndProlog\n") self._fp_write("%%EndProlog\n")

View File

@ -1,25 +1,17 @@
from helper import unittest, PillowTestCase from helper import unittest, PillowTestCase
from PIL import Image, PSDraw
import os
import sys
class TestPsDraw(PillowTestCase): class TestPsDraw(PillowTestCase):
def test_draw_postscript(self): def _create_document(self, ps):
# Based on Pillow tutorial, but there is no textsize:
# http://pillow.readthedocs.org/en/latest/handbook/tutorial.html
# Arrange
from PIL import Image
from PIL import PSDraw
tempfile = self.tempfile('temp.ps')
fp = open(tempfile, "wb")
im = Image.open("Tests/images/hopper.ppm") im = Image.open("Tests/images/hopper.ppm")
title = "hopper" title = "hopper"
box = (1*72, 2*72, 7*72, 10*72) # in points box = (1*72, 2*72, 7*72, 10*72) # in points
# Act
ps = PSDraw.PSDraw(fp)
ps.begin_document(title) ps.begin_document(title)
# draw diagonal lines in a cross # draw diagonal lines in a cross
@ -35,14 +27,43 @@ class TestPsDraw(PillowTestCase):
ps.text((3*72, 4*72), title) ps.text((3*72, 4*72), title)
ps.end_document() ps.end_document()
def test_draw_postscript(self):
# Based on Pillow tutorial, but there is no textsize:
# http://pillow.readthedocs.org/en/latest/handbook/tutorial.html
# Arrange
tempfile = self.tempfile('temp.ps')
fp = open(tempfile, "wb")
# Act
ps = PSDraw.PSDraw(fp)
self._create_document(ps)
fp.close() fp.close()
# Assert # Assert
# Check non-zero file was created # Check non-zero file was created
import os
self.assertTrue(os.path.isfile(tempfile)) self.assertTrue(os.path.isfile(tempfile))
self.assertGreater(os.path.getsize(tempfile), 0) self.assertGreater(os.path.getsize(tempfile), 0)
def test_stdout(self):
# Temporarily redirect stdout
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
ps = PSDraw.PSDraw()
self._create_document(ps)
# Reset stdout
sys.stdout = old_stdout
self.assertNotEqual(mystdout.getvalue(), "")
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()