Skip to content

Commit bcd4c18

Browse files
committed
Python 3 preparation: StringIO.
This generally arranges for StringIO and cStringIO references to use io.StringIO for Python 3 but io.BytesIO for Python 2, consistent with the string representations generally used in Roundup. A special FasterStringIO in the TAL code, which referenced internals of the old Python 2 StringIO module, is cut down so it doesn't actually do anything beyond the StringIO class it inherits from (it would also be reasonable to remove FasterStringIO completely). One place in roundup_server.py clearly needing binary I/O is made to use io.BytesIO unconditionally.
1 parent 4beb2f9 commit bcd4c18

File tree

17 files changed

+43
-52
lines changed

17 files changed

+43
-52
lines changed

frontends/roundup.cgi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ LOG = DevNull()
7171
# Set up the error handler
7272
#
7373
try:
74-
import traceback, StringIO, cgi
74+
import traceback, cgi
75+
from roundup.anypy.strings import StringIO
7576
from roundup.cgi import cgitb
7677
except:
7778
print("Content-Type: text/plain\n")
7879
print(_("Failed to import cgitb!\n\n"))
79-
s = StringIO.StringIO()
80+
s = StringIO()
8081
traceback.print_exc(None, s)
8182
print(s.getvalue())
8283

roundup/anypy/strings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import sys
88
_py3 = sys.version_info[0] > 2
99

10+
import io
11+
if _py3:
12+
StringIO = io.StringIO
13+
else:
14+
StringIO = io.BytesIO
15+
1016
def b2s(b):
1117
"""Convert a UTF-8 encoded bytes object to the internal string format."""
1218
if _py3:

roundup/cgi/PageTemplates/PageTemplate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from roundup.cgi.TAL.TALParser import TALParser
2626
from roundup.cgi.TAL.HTMLTALParser import HTMLTALParser
2727
from roundup.cgi.TAL.TALGenerator import TALGenerator
28-
# Do not use cStringIO here! It's not unicode aware. :(
2928
from roundup.cgi.TAL.TALInterpreter import TALInterpreter, FasterStringIO
3029
from .Expressions import getEngine
3130

roundup/cgi/TAL/TALInterpreter.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import re
2323
from types import ListType
2424
from cgi import escape
25-
# Do not use cStringIO here! It's not unicode aware. :(
26-
from StringIO import StringIO
25+
from roundup.anypy.strings import StringIO
2726
#from DocumentTemplate.DT_Util import ustr
2827
ustr = str
2928

@@ -758,18 +757,7 @@ class FasterStringIO(StringIO):
758757
759758
This let's us have a much faster write() method.
760759
"""
761-
def close(self):
762-
if not self.closed:
763-
self.write = _write_ValueError
764-
StringIO.close(self)
765-
766-
def seek(self, pos, mode=0):
767-
raise RuntimeError("FasterStringIO.seek() not allowed")
768-
769-
def write(self, s):
770-
#assert self.pos == self.len
771-
self.buflist.append(s)
772-
self.len = self.pos = self.pos + len(s)
760+
pass
773761

774762

775763
def _write_ValueError(s):

roundup/cgi/engine_chameleon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os.path
66
import chameleon
77

8-
from roundup.cgi.templating import StringIO, context, TALLoaderBase
8+
from roundup.cgi.templating import context, TALLoaderBase
99
from roundup.anypy.strings import s2u
1010

1111
class Loader(TALLoaderBase):

roundup/cgi/engine_zopetal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def render(self, client, classname, request, **options):
8686
c.update({'options': options})
8787

8888
# and go
89-
output = StringIO.StringIO()
89+
output = StringIO()
9090
TALInterpreter.TALInterpreter(self._v_program, self.macros,
9191
getEngine().getContext(c), output, tal=1, strictinsert=0)()
9292
return output.getvalue()

roundup/cgi/templating.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from roundup import hyperdb, date, support
3030
from roundup import i18n
3131
from roundup.i18n import _
32-
from roundup.anypy.strings import is_us, us2s, s2u, u2s
32+
from roundup.anypy.strings import is_us, us2s, s2u, u2s, StringIO
3333

3434
from .KeywordsExpr import render_keywords_expression_editor
3535

@@ -43,10 +43,6 @@
4343
import cPickle as pickle
4444
except ImportError:
4545
import pickle
46-
try:
47-
import cStringIO as StringIO
48-
except ImportError:
49-
import StringIO
5046
try:
5147
from StructuredText.StructuredText import HTML as StructuredText
5248
except ImportError:
@@ -647,7 +643,7 @@ def csv(self):
647643
""" Return the items of this class as a chunk of CSV text.
648644
"""
649645
props = self.propnames()
650-
s = StringIO.StringIO()
646+
s = StringIO()
651647
writer = csv.writer(s)
652648
writer.writerow(props)
653649
check = self._client.db.security.hasPermission

roundup/mailer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import time, quopri, os, socket, smtplib, re, sys, traceback, email, logging
66

7-
from cStringIO import StringIO
8-
97
from roundup import __version__
108
from roundup.date import get_timezone, Date
119

roundup/mailgw.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class node. Any parts of other types are each stored in separate files
9595
from __future__ import print_function
9696
__docformat__ = 'restructuredtext'
9797

98-
import string, re, os, mimetools, cStringIO, smtplib, socket, binascii, quopri
98+
import string, re, os, mimetools, smtplib, socket, binascii, quopri
9999
import time, random, sys, logging
100100
import codecs
101101
import traceback
@@ -108,6 +108,7 @@ class node. Any parts of other types are each stored in separate files
108108
from roundup.mailer import Mailer, MessageSendError
109109
from roundup.i18n import _
110110
from roundup.hyperdb import iter_roles
111+
from roundup.anypy.strings import StringIO
111112

112113
try:
113114
import pyme, pyme.core, pyme.constants, pyme.constants.sigsum
@@ -223,7 +224,7 @@ def getpart(self):
223224
'''
224225
boundary = self.getparam('boundary')
225226
mid, end = '--'+boundary, '--'+boundary+'--'
226-
s = cStringIO.StringIO()
227+
s = StringIO()
227228
while 1:
228229
line = self.fp.readline()
229230
if not line:
@@ -303,7 +304,7 @@ def getname(self):
303304
# the subject of the actual e-mail embedded here
304305
# we add a '.eml' extension like other email software does it
305306
self.fp.seek(0)
306-
s = cStringIO.StringIO(self.getbody())
307+
s = StringIO(self.getbody())
307308
name = Message(s).getheader('subject')
308309
if name:
309310
name = name + '.eml'
@@ -329,7 +330,7 @@ def getbody(self):
329330
data = binascii.a2b_base64(self.fp.read())
330331
elif encoding == 'quoted-printable':
331332
# the quopri module wants to work with files
332-
decoded = cStringIO.StringIO()
333+
decoded = StringIO()
333334
quopri.decode(self.fp, decoded)
334335
data = decoded.getvalue()
335336
elif encoding == 'uuencoded':
@@ -449,7 +450,7 @@ def extract_content(self, parent_type=None, ignore_alternatives=False,
449450
attachments = []
450451
html_part = False
451452
elif unpack_rfc822 and content_type == 'message/rfc822':
452-
s = cStringIO.StringIO(self.getbody())
453+
s = StringIO(self.getbody())
453454
m = Message(s)
454455
ig = ignore_alternatives and not content
455456
new_content, attachments, html_part = m.extract_content(m.gettype(), ig,
@@ -527,7 +528,7 @@ def decrypt(self, author, may_be_unsigned=False):
527528
# pyme.core.Data implements a seek method with a different signature
528529
# than roundup can handle. So we'll put the data in a container that
529530
# the Message class can work with.
530-
c = cStringIO.StringIO()
531+
c = StringIO()
531532
c.write(plaintext.read())
532533
c.seek(0)
533534
return Message(c)
@@ -1328,7 +1329,7 @@ def do_pipe(self):
13281329
13291330
XXX: we may want to read this into a temporary file instead...
13301331
"""
1331-
s = cStringIO.StringIO()
1332+
s = StringIO()
13321333
s.write(sys.stdin.read())
13331334
s.seek(0)
13341335
self.main(s)
@@ -1424,7 +1425,7 @@ def do_imap(self, server, user='', password='', mailbox='', ssl=0,
14241425
server.store(str(i), '+FLAGS', r'(\Deleted)')
14251426

14261427
# process the message
1427-
s = cStringIO.StringIO(data[0][1])
1428+
s = StringIO(data[0][1])
14281429
s.seek(0)
14291430
self.handle_Message(Message(s))
14301431
server.close()
@@ -1493,7 +1494,7 @@ def _do_pop(self, server, user, password, apop, ssl):
14931494
# [ array of message lines ],
14941495
# number of octets ]
14951496
lines = server.retr(i)[1]
1496-
s = cStringIO.StringIO('\n'.join(lines))
1497+
s = StringIO('\n'.join(lines))
14971498
s.seek(0)
14981499
self.handle_Message(Message(s))
14991500
# delete the message

roundup/roundupdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
__docformat__ = 'restructuredtext'
2222

2323
import re, os, smtplib, socket, time, random
24-
import cStringIO, base64, mimetypes
24+
import base64, mimetypes
2525
import os.path
2626
import logging
2727
from email import Encoders

0 commit comments

Comments
 (0)