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 import os.path
from StringIO import StringIO
import unittest
import App import App
from Testing.ZopeTestCase.warnhook import WarningsHook from Testing.ZopeTestCase.warnhook import WarningsHook
from ZPublisher.HTTPRequest import WSGIRequest
from ZPublisher.HTTPResponse import WSGIResponse
class TestImageFile(unittest.TestCase): class TestImageFile(unittest.TestCase):
...@@ -39,3 +44,26 @@ class TestImageFile(unittest.TestCase): ...@@ -39,3 +44,26 @@ class TestImageFile(unittest.TestCase):
prefix = App.__dict__ prefix = App.__dict__
App.ImageFile.ImageFile('www/zopelogo.png', prefix) App.ImageFile.ImageFile('www/zopelogo.png', prefix)
self.assertFalse(self.warningshook.warnings) 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): ...@@ -273,7 +273,10 @@ class ImageTests(FileTests):
self.assertEqual(self.file.tag(), (tag_fmt % ('bar', 'foo'))) self.assertEqual(self.file.tag(), (tag_fmt % ('bar', 'foo')))
def testViewImageOrFile(self): 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): def test_interfaces(self):
from zope.interface.verify import verifyClass from zope.interface.verify import verifyClass
......
import io
from zope.interface import Interface from zope.interface import Interface
from zope.interface import implements from zope.interface import implements
...@@ -36,16 +38,16 @@ class IStreamIterator(IUnboundStreamIterator): ...@@ -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. fixed-sized sequence of bytes.
""" """
implements(IStreamIterator) implements(IStreamIterator)
def __init__(self, name, mode='r', bufsize=-1, streamsize=1 << 16): def __init__(self, name, mode='rb', bufsize=-1, streamsize=1 << 16):
file.__init__(self, name, mode, bufsize) super(filestream_iterator, self).__init__(name, mode)
self.streamsize = streamsize self.streamsize = streamsize
def next(self): def next(self):
...@@ -56,7 +58,7 @@ class filestream_iterator(file): ...@@ -56,7 +58,7 @@ class filestream_iterator(file):
def __len__(self): def __len__(self):
cur_pos = self.tell() cur_pos = self.tell()
self.seek(0, 2) self.seek(0, io.SEEK_END)
size = self.tell() size = self.tell()
self.seek(cur_pos, 0) self.seek(cur_pos, io.SEEK_SET)
return size return size
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
""" """
from contextlib import contextmanager, closing from contextlib import contextmanager, closing
from cStringIO import StringIO from cStringIO import StringIO
from io import IOBase
import sys import sys
from thread import allocate_lock from thread import allocate_lock
...@@ -40,9 +41,9 @@ from ZPublisher import pubevents ...@@ -40,9 +41,9 @@ from ZPublisher import pubevents
from ZPublisher.utils import recordMetaData from ZPublisher.utils import recordMetaData
if sys.version_info >= (3, ): if sys.version_info >= (3, ):
from io import IOBase _FILE_TYPES = (IOBase, )
else: else:
IOBase = file # NOQA _FILE_TYPES = (IOBase, file) # NOQA
_DEFAULT_DEBUG_MODE = False _DEFAULT_DEBUG_MODE = False
_DEFAULT_REALM = None _DEFAULT_REALM = None
...@@ -229,7 +230,7 @@ def publish_module(environ, start_response, ...@@ -229,7 +230,7 @@ def publish_module(environ, start_response,
status, headers = response.finalize() status, headers = response.finalize()
start_response(status, headers) start_response(status, headers)
if (isinstance(response.body, IOBase) or if (isinstance(response.body, _FILE_TYPES) or
IUnboundStreamIterator.providedBy(response.body)): IUnboundStreamIterator.providedBy(response.body)):
result = response.body result = response.body
else: else:
......
...@@ -320,8 +320,9 @@ class TestPublishModule(unittest.TestCase, PlacelessSetup): ...@@ -320,8 +320,9 @@ class TestPublishModule(unittest.TestCase, PlacelessSetup):
self.assertEqual(kw, {}) self.assertEqual(kw, {})
def test_response_body_is_file(self): def test_response_body_is_file(self):
from io import BytesIO
class DummyFile(file): class DummyFile(BytesIO):
def __init__(self): def __init__(self):
pass 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