Commit 32e810f1 authored by Hanno Schlichting's avatar Hanno Schlichting

Replace file class by Python 3 compatible io classes.

parent 3c669886
import unittest
import io
import os.path
from StringIO import StringIO
import unittest
import App
from Testing.ZopeTestCase.warnhook import WarningsHook
from ZPublisher.HTTPRequest import WSGIRequest
from ZPublisher.HTTPResponse import WSGIResponse
class TestImageFile(unittest.TestCase):
......@@ -39,3 +44,26 @@ class TestImageFile(unittest.TestCase):
prefix = App.__dict__
App.ImageFile.ImageFile('www/zopelogo.png', prefix)
self.assertFalse(self.warningshook.warnings)
class TestImageFileFunctional(unittest.TestCase):
def test_index_html(self):
env = {
'SERVER_NAME': 'localhost',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',
'REQUEST_METHOD': 'GET',
}
stdin = StringIO()
stdout = StringIO()
response = WSGIResponse(stdout)
request = WSGIRequest(stdin, env, response)
path = os.path.join(os.path.dirname(App.__file__),
'www', 'zopelogo.png')
image = App.ImageFile.ImageFile(path)
result = image.index_html(request, response)
self.assertEqual(stdout.getvalue(), '')
self.assertTrue(isinstance(result, io.FileIO))
self.assertTrue(b''.join(result).startswith(b'\x89PNG\r\n'))
self.assertEqual(len(result), image.size)
......@@ -273,7 +273,10 @@ class ImageTests(FileTests):
self.assertEqual(self.file.tag(), (tag_fmt % ('bar', 'foo')))
def testViewImageOrFile(self):
pass # dtml method,screw it
request = self.app.REQUEST
response = request.RESPONSE
result = self.file.index_html(request, response)
self.assertEqual(result, self.data)
def test_interfaces(self):
from zope.interface.verify import verifyClass
......
import io
from zope.interface import Interface
from zope.interface import implements
......@@ -36,16 +38,16 @@ class IStreamIterator(IUnboundStreamIterator):
"""
class filestream_iterator(file):
class filestream_iterator(io.FileIO):
"""
a file subclass which implements an iterator that returns a
A FileIO subclass which implements an iterator that returns a
fixed-sized sequence of bytes.
"""
implements(IStreamIterator)
def __init__(self, name, mode='r', bufsize=-1, streamsize=1 << 16):
file.__init__(self, name, mode, bufsize)
def __init__(self, name, mode='rb', bufsize=-1, streamsize=1 << 16):
super(filestream_iterator, self).__init__(name, mode)
self.streamsize = streamsize
def next(self):
......@@ -56,7 +58,7 @@ class filestream_iterator(file):
def __len__(self):
cur_pos = self.tell()
self.seek(0, 2)
self.seek(0, io.SEEK_END)
size = self.tell()
self.seek(cur_pos, 0)
self.seek(cur_pos, io.SEEK_SET)
return size
......@@ -14,6 +14,7 @@
"""
from contextlib import contextmanager, closing
from cStringIO import StringIO
from io import IOBase
import sys
from thread import allocate_lock
......@@ -40,9 +41,9 @@ from ZPublisher import pubevents
from ZPublisher.utils import recordMetaData
if sys.version_info >= (3, ):
from io import IOBase
_FILE_TYPES = (IOBase, )
else:
IOBase = file # NOQA
_FILE_TYPES = (IOBase, file) # NOQA
_DEFAULT_DEBUG_MODE = False
_DEFAULT_REALM = None
......@@ -229,7 +230,7 @@ def publish_module(environ, start_response,
status, headers = response.finalize()
start_response(status, headers)
if (isinstance(response.body, IOBase) or
if (isinstance(response.body, _FILE_TYPES) or
IUnboundStreamIterator.providedBy(response.body)):
result = response.body
else:
......
......@@ -320,8 +320,9 @@ class TestPublishModule(unittest.TestCase, PlacelessSetup):
self.assertEqual(kw, {})
def test_response_body_is_file(self):
from io import BytesIO
class DummyFile(file):
class DummyFile(BytesIO):
def __init__(self):
pass
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment