Commit 37adbd76 authored by Rémi Cardona's avatar Rémi Cardona
Browse files

Use StringIO and BytesIO from io module

Related to #268148.
parent 4cfc2da5d5bd
......@@ -35,7 +35,7 @@ import re
import string
import htmlentitydefs
import codecs
from StringIO import StringIO
from io import BytesIO
try:
import chardet
......@@ -197,12 +197,12 @@ class TransformData(object):
def binary_decode(self):
if self.encoding == 'gzip':
import gzip
stream = gzip.GzipFile(fileobj=StringIO(self.data))
stream = gzip.GzipFile(fileobj=BytesIO(self.data))
self.data = stream.read()
self.encoding = guess_encoding(self.data)
elif self.encoding == 'bzip2':
import bz2
self.data = bz2.decompress(StringIO(self.data)) # StringIO or not?
self.data = bz2.decompress(BytesIO(self.data)) # StringIO or not?
self.encoding = guess_encoding(self.data)
elif self.encoding == 'base64':
import base64
......
......@@ -19,7 +19,7 @@
from logilab.common.testlib import TestCase, unittest_main
import locale
from StringIO import StringIO
from io import BytesIO
from logilab.mtconverter import *
SPECIAL_CHARS = {
......@@ -76,20 +76,20 @@ class HtmlEscapeTC(TestCase):
class GuessEncodingTC(TestCase):
def test_emacs_style_declaration(self):
data = '''# -*- coding: latin1 -*-'''
data = b'''# -*- coding: latin1 -*-'''
self.assertEqual(guess_encoding(data), 'latin1')
def test_emacs_style_declaration_stringIO(self):
data = '''# -*- coding: latin1 -*-'''
self.assertEqual(guess_encoding(StringIO(data)), 'latin1')
data = b'''# -*- coding: latin1 -*-'''
self.assertEqual(guess_encoding(BytesIO(data)), 'latin1')
def test_xml_style_declaration(self):
data = '''<?xml version="1.0" encoding="latin1"?>
data = b'''<?xml version="1.0" encoding="latin1"?>
<root/>'''
self.assertEqual(guess_encoding(data), 'latin1')
def test_html_style_declaration(self):
data = '''<html xmlns="http://www.w3.org/1999/xhtml" xmlns:erudi="http://www.logilab.fr/" xml:lang="fr" lang="fr">
data = b'''<html xmlns="http://www.w3.org/1999/xhtml" xmlns:erudi="http://www.logilab.fr/" xml:lang="fr" lang="fr">
<head>
<base href="http://intranet.logilab.fr/jpl/" /><meta http-equiv="content-type" content="text/html; charset=latin1"/>
</head>
......@@ -99,7 +99,7 @@ class GuessEncodingTC(TestCase):
self.assertEqual(guess_encoding(data), 'latin1')
def test_bad_detection(self):
data = '''class SchemaViewer(object):
data = b'''class SchemaViewer(object):
"""return an ureport layout for some part of a schema"""
def __init__(self, req=None, encoding=None):
'''
......
......@@ -17,7 +17,7 @@
# with logilab-mtconverter. If not, see <http://www.gnu.org/licenses/>.
"""PIL based transformations for images"""
from StringIO import StringIO
from io import BytesIO
from PIL import Image
......@@ -30,13 +30,13 @@ class PILTransform(Transform):
def _convert(self, trdata):
newwidth = trdata.get('width', None)
newheight = trdata.get('height', None)
pilimg = Image.open(StringIO(trdata.data))
pilimg = Image.open(BytesIO(trdata.data))
if self.format in ['jpeg', 'ppm']:
pilimg.draft("RGB", pilimg.size)
pilimg = pilimg.convert("RGB")
if newwidth or newheight:
pilimg.thumbnail((newwidth, newheight), Image.ANTIALIAS)
stream = StringIO()
stream = BytesIO()
pilimg.save(stream, self.format)
return stream.getvalue()
......
......@@ -33,7 +33,7 @@ Original code from active state recipe
"""
import keyword, token, tokenize
from cStringIO import StringIO
from io import StringIO
_KEYWORD = token.NT_OFFSET + 1
_TEXT = token.NT_OFFSET + 2
......
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