Commit 20e9063b authored by Matt Harbison's avatar Matt Harbison

py3: replace cStringIO with pycompat.bytesio

Fixed up after running `2to3 -w -n -f imports -f imports2 .`.  There are other
imports that need fixing, but not all have a pycompat alias.  So I'm splitting
these up.

TortoiseHgOverlayServer.py and thg replace stdio with byte buffers, which won't
work on py3.  They need to be revisited to see if this is still necessary with
py2exe[1].  In the meantime, I conditionalized a couple things there to prevent
extra stacktraces on py3, since the local variable holding the buffer is used
further down in each case.

[1] https://groups.google.com/d/msg/thg-dev/dZbFjZugORs/sGOnxBpnBgAJ
parent 258a0ea16c6b
......@@ -30,7 +30,6 @@ if hasattr(sys, "frozen"):
import errno
import time
import threading
import cStringIO
import Queue
import traceback
import gc
......@@ -52,7 +51,11 @@ except ImportError as e:
from mercurial import demandimport
demandimport.IGNORES.add('win32com.shell')
demandimport.enable()
from mercurial import error
from mercurial import (
error,
pycompat,
)
from mercurial.windows import posixfile, unlink, rename
from tortoisehg.util.i18n import agettext as _
from tortoisehg.util import hglib, thread2, paths, shlib, version
......@@ -280,9 +283,13 @@ def update_batch(batch):
if roots:
_ui = hglib.loadui()
failedroots = set()
errorstream = cStringIO.StringIO()
_stderr = sys.stderr
sys.stderr = errorstream
# TODO: Figure out if this hack for py2exe is still needed, and possibly
# wrap this to handle unicode on py3
if not pycompat.ispy3:
errorstream = pycompat.bytesio()
sys.stderr = errorstream
try:
# Ensure that all unset dirstate entries can be updated.
time.sleep(2)
......@@ -305,9 +312,10 @@ def update_batch(batch):
if notifypaths:
shlib.shell_notify(list(notifypaths), noassoc=not updated_any)
logger.msg('Shell notified')
errmsg = errorstream.getvalue()
if errmsg:
logger.msg('stderr: %s' % errmsg)
if not pycompat.ispy3:
errmsg = errorstream.getvalue()
if errmsg:
logger.msg('stderr: %s' % errmsg)
finally:
sys.stderr = _stderr
......
......@@ -17,7 +17,11 @@ import os
import sys
from mercurial.i18n import _
from mercurial import registrar, ui
from mercurial import (
pycompat,
registrar,
ui,
)
testedwith = '4.3'
......@@ -107,8 +111,7 @@ def enforceversion():
def cmdview(ui, repo, *pats, **opts):
"""start light interactive history viewer from tortoisehg"""
enforceversion()
import cStringIO
mystderr = cStringIO.StringIO()
mystderr = pycompat.bytesio()
origstderr = sys.stderr
sys.stderr = mystderr
sys.__stdout__ = sys.stdout
......
......@@ -35,7 +35,10 @@ from __future__ import print_function
import struct
import array
from cStringIO import StringIO
from mercurial import (
pycompat,
)
__version__ = "1.1-pythongettext"
......@@ -224,4 +227,4 @@ class Msgfmt:
self.po.close()
def getAsFile(self):
return StringIO(self.get())
return pycompat.bytesio(self.get())
......@@ -8,10 +8,10 @@ import sys
import tempfile
import time
from collections import defaultdict
try:
import cStringIO as StringIO
except ImportError:
import StringIO
from mercurial import (
pycompat,
)
from nose import tools
......@@ -151,8 +151,8 @@ class HgClient(object):
origwd = os.getcwd()
ui = hglib.loadui()
ui.setconfig('ui', 'strict', True)
ui.fout = StringIO.StringIO()
ui.ferr = StringIO.StringIO()
ui.fout = pycompat.bytesio()
ui.ferr = pycompat.bytesio()
args = list(args)
req = dispatch.request(args, ui=ui)
if hasattr(req, 'earlyoptions'):
......
import os, tempfile
from nose.tools import *
from nose.plugins.skip import SkipTest
from StringIO import StringIO
from mercurial import config, error
from mercurial import (
config,
error,
pycompat,
)
from tortoisehg.util import wconfig
import helpers
......@@ -23,7 +27,7 @@ def newwconfig(vals={}):
return wconfig.config(newrconfig(vals))
def written(c):
dest = StringIO()
dest = pycompat.bytesio()
c.write(dest)
return dest.getvalue()
......@@ -221,7 +225,7 @@ def check_write_after_update():
def check_read_write():
c = newwconfig()
s = '[foo]\nbar = baz'
c.read(path='foo', fp=StringIO(s))
c.read(path='foo', fp=pycompat.bytesio(s))
assert_equals(s, written(c).rstrip())
@with_wconfig
......@@ -229,16 +233,16 @@ def check_read_write():
def check_read_write_missing_section_header_error():
c = newwconfig()
s = 'bar = baz' # missing header
c.read(path='foo', fp=StringIO(s))
c.write(StringIO())
c.read(path='foo', fp=pycompat.bytesio(s))
c.write(pycompat.bytesio())
@with_wconfig
@raises(error.ParseError)
def check_read_write_parsing_error():
c = newwconfig()
s = '[foo]\n:bar = baz' # Mercurial can parse it but INIConfig can't
c.read(path='foo', fp=StringIO(s))
c.write(StringIO())
c.read(path='foo', fp=pycompat.bytesio(s))
c.write(pycompat.bytesio())
@with_wconfig
def check_write_after_dict_setitem():
......@@ -262,7 +266,7 @@ def check_write_after_dict_delitem():
def check_read_write_rem():
c = newwconfig()
s = '[foo]\nrem = x'
c.read(path='foo', fp=StringIO(s))
c.read(path='foo', fp=pycompat.bytesio(s))
c.set('foo', 'rem', 'y')
assert_equals('[foo]\nrem = y', written(c).rstrip())
......@@ -270,7 +274,7 @@ def check_read_write_rem():
def check_read_write_suboption():
c = newwconfig()
s = '[foo]\nbar:baz = x'
c.read(path='foo', fp=StringIO(s))
c.read(path='foo', fp=pycompat.bytesio(s))
c.set('foo', 'bar:baz', 'y')
assert_equals('[foo]\nbar:baz = y', written(c).rstrip())
......@@ -278,7 +282,7 @@ def check_read_write_suboption():
def check_read_write_suboption_removal():
c = newwconfig()
s = '[foo]\nbar:baz = x\nbar = y'
c.read(path='foo', fp=StringIO(s))
c.read(path='foo', fp=pycompat.bytesio(s))
del c['foo']['bar:baz']
assert_equals('[foo]\nbar = y', written(c).rstrip())
......
......@@ -111,19 +111,26 @@ if ('THGDEBUG' in os.environ or '--profile' in sys.argv
or getattr(sys, 'frozen', None) != 'windows_exe'):
sys.exit(tortoisehg.hgqt.run.dispatch(argv))
else:
import cStringIO
from mercurial import pycompat
from mercurial.utils import procutil
mystderr = cStringIO.StringIO()
origstderr = sys.stderr
sys.stderr = mystderr
sys.__stdout__ = sys.stdout
sys.__stderr__ = sys.stderr
procutil.stderr = sys.stderr
# TODO: Figure out if this hack for py2exe is still needed, and possibly
# wrap this to handle unicode on py3
if not pycompat.ispy3:
mystderr = pycompat.bytesio()
sys.stderr = mystderr
sys.__stdout__ = sys.stdout
sys.__stderr__ = sys.stderr
procutil.stderr = sys.stderr
ret = 0
try:
ret = tortoisehg.hgqt.run.dispatch(argv)
sys.stderr = origstderr
stderrout = mystderr.getvalue()
if not pycompat.ispy3:
sys.stderr = origstderr
stderrout = mystderr.getvalue()
else:
stderrout = ''
errors = ('Traceback', 'TypeError', 'NameError', 'AttributeError',
'NotImplementedError')
for l in stderrout.splitlines():
......
......@@ -7,7 +7,6 @@
from __future__ import absolute_import
import cStringIO
import os
import re
......@@ -285,7 +284,7 @@ class ChunksWidget(QWidget):
if isinstance(ctx, patchctx):
repo.thgbackup(ctx._path)
fp = util.atomictempfile(ctx._path, 'wb')
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
try:
if ctx._ph.comments:
buf.write('\n'.join(ctx._ph.comments))
......@@ -327,7 +326,7 @@ class ChunksWidget(QWidget):
wf = repo.wvfs(self.currentFile, 'wb', atomictemp=True)
wf.write(self.diffbrowse.origcontents)
wf.close()
fp = cStringIO.StringIO()
fp = pycompat.bytesio()
chunks[0].write(fp)
for c in kchunks:
c.write(fp)
......@@ -394,7 +393,7 @@ class ChunksWidget(QWidget):
else:
# Apply chunks to wfile
repo.thgbackup(repo.wjoin(wfile))
fp = cStringIO.StringIO()
fp = pycompat.bytesio()
for c in chunks:
c.write(fp)
fp.seek(0)
......@@ -451,7 +450,7 @@ class ChunksWidget(QWidget):
else:
return []
else:
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
diffopts = patch.diffopts(repo.ui, {'git':True})
m = matchmod.exact(repo.root, repo.root, [wfile])
for p in patch.diff(repo, ctx.p1().node(), None, match=m,
......@@ -781,12 +780,12 @@ class DiffBrowser(QFrame):
elif isinstance(self._ctx.rev(), str):
chunks = self._ctx._files[filename]
else:
header = patch.parsepatch(cStringIO.StringIO(fd.diff))[0]
header = patch.parsepatch(pycompat.bytesio(fd.diff))[0]
chunks = [header] + header.hunks
utext = []
for chunk in chunks[1:]:
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
chunk.selected = False
chunk.write(buf)
chunk.lines = buf.getvalue().splitlines()
......
......@@ -6,10 +6,18 @@
# GNU General Public License version 2, incorporated herein by reference.
import os, posixpath
import cStringIO
from mercurial import commands, error, match, patch, subrepo, util
from mercurial import copies
from mercurial import (
commands,
copies,
error,
match,
patch,
pycompat,
subrepo,
util,
)
from mercurial.utils import (
dateutil,
)
......@@ -377,7 +385,7 @@ class FileData(_AbstractFileData):
diffopts = patch.difffeatureopts(repo.ui)
diffopts.git = True
m = match.exact(repo.root, repo.root, [wfile])
fp = cStringIO.StringIO()
fp = pycompat.bytesio()
copy = {}
if oldname != wfile:
......@@ -401,7 +409,7 @@ class FileData(_AbstractFileData):
values = []
lines = 0
for chunk in self.changes.hunks:
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
chunk.write(buf)
chunk.excluded = False
val = buf.getvalue()
......
......@@ -7,8 +7,6 @@
from __future__ import absolute_import
import cStringIO
from . import qsci as Qsci
from .qtcore import (
QPoint,
......@@ -30,7 +28,10 @@ from .qtgui import (
QVBoxLayout,
)
from mercurial import patch
from mercurial import (
patch,
pycompat,
)
from ..util import hglib
from ..util.i18n import _
......@@ -131,7 +132,7 @@ class RejectsDialog(QDialog):
editor.setMarginLineNumbers(1, True)
editor.setMarginWidth(1, str(editor.lines())+'X')
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
try:
buf.write('diff -r aaaaaaaaaaaa -r bbbbbbbbbbb %s\n' % path)
buf.write(open(path + '.rej', 'rb').read())
......@@ -196,7 +197,7 @@ class RejectsDialog(QDialog):
def showChunk(self, row):
if row == -1 or self.updating:
return
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
chunk = self.chunks[row]
chunk.write(buf)
chunkstr = buf.getvalue().decode(self._textEncoding(), 'replace')
......
......@@ -9,7 +9,6 @@
from __future__ import absolute_import
import binascii
import cStringIO
import os
import shlex # used by runCustomCommand
import subprocess # used by runCustomCommand
......@@ -2243,7 +2242,7 @@ class RepoWidget(QWidget):
else:
cmd += ['--repository', self.repo.root]
_ui = self.repo.ui.copy()
_ui.ferr = cStringIO.StringIO()
_ui.ferr = pycompat.bytesio()
# avoid circular import of hgqt.run by importing it inplace
from . import run
res = run.dispatch(cmd, u=_ui)
......
......@@ -7,7 +7,6 @@
from __future__ import absolute_import
import cStringIO
import glob
import os
import re
......@@ -1177,7 +1176,7 @@ def parsecmdline(cmdline, cwd):
_ = _gettext # TODO: use unicode version globally
# shlex can't process unicode on Python < 2.7.3
cmdline = cmdline.encode('utf-8')
src = cStringIO.StringIO(cmdline)
src = pycompat.bytesio(cmdline)
lex = shlex.shlex(src, posix=True)
lex.whitespace_split = True
lex.commenters = ''
......
......@@ -9,7 +9,6 @@ from __future__ import print_function
import os
import binascii
import cStringIO
from mercurial import patch, util, error
from mercurial import node
......@@ -183,7 +182,7 @@ class patchctx(object):
if wfile == self._parseErrorFileName:
return '\n\n\nErrors while parsing patch:\n'+str(self._parseerror)
if wfile in self._files:
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
for chunk in self._files[wfile]:
chunk.write(buf)
return buf.getvalue()
......
......@@ -7,7 +7,6 @@
import os
import re
import cStringIO
import ConfigParser
from mercurial import error, util, config as config_mod
......@@ -264,7 +263,7 @@ def readfile(path):
def writefile(config, path):
"""Write the given config obj to the specified file"""
# normalize line endings
buf = cStringIO.StringIO()
buf = pycompat.bytesio()
config.write(buf)
data = '\n'.join(buf.getvalue().splitlines()) + '\n'
......
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