Skip to content

Commit

Permalink
Merge pull request #59 from rouge8/gh-5
Browse files Browse the repository at this point in the history
Don't crash when printing tracebacks under IPython on Python 2
  • Loading branch information
rouge8 committed Mar 25, 2018
2 parents f1a8875 + 05c5dac commit 5f0029e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion transaction/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ def native_(s, encoding='latin-1', errors='strict'): #pragma NO COVER
if PY3: #pragma NO COVER
from io import StringIO
else:
from io import BytesIO as StringIO
from io import BytesIO
# Prevent crashes in IPython when writing tracebacks if a commit fails
# ref: https://github.com/ipython/ipython/issues/9126#issuecomment-174966638
class StringIO(BytesIO):
def write(self, s):
s = native_(s, encoding='utf-8')
super(StringIO, self).write(s)

if PY3: #pragma NO COVER
from collections import MutableMapping
Expand Down
13 changes: 13 additions & 0 deletions transaction/tests/test__transaction.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2001, 2002, 2005 Zope Foundation and Contributors.
Expand Down Expand Up @@ -1649,6 +1650,18 @@ def third():
transaction.abort() # should do nothing
self.assertEqual(list(dm.keys()), ['a'])

def test_gh5(self):
from transaction import _transaction
from transaction._compat import native_

buffer = _transaction._makeTracebackBuffer()

s = u'ąčę'
buffer.write(s)

buffer.seek(0)
self.assertEqual(buffer.read(), native_(s, 'utf-8'))

class Resource(object):
_b = _c = _v = _f = _a = _x = _after = False
def __init__(self, key, error=None):
Expand Down

0 comments on commit 5f0029e

Please sign in to comment.