Skip to content

Commit

Permalink
Insist that transaction user and description are text (unicode)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Fulton committed Nov 15, 2016
1 parent b4a6c46 commit 8f507c4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
5 changes: 4 additions & 1 deletion transaction/_manager.py
Expand Up @@ -25,6 +25,7 @@
from transaction.interfaces import TransientError
from transaction.weakset import WeakSet
from transaction._compat import reraise
from transaction._compat import text_type
from transaction._transaction import Transaction


Expand Down Expand Up @@ -174,13 +175,15 @@ def run(self, func=None, tries=3):
doc = func.__doc__
if name != '_':
if doc:
doc = name + '\n\n' + doc
doc = name + u'\n\n' + doc
else:
doc = name

for i in range(1, tries + 1):
txn = self.begin()
if doc:
if not isinstance(doc, text_type):
doc = doc.decode('ascii')
txn.note(doc)

try:
Expand Down
18 changes: 15 additions & 3 deletions transaction/_transaction.py
Expand Up @@ -27,6 +27,7 @@
from transaction._compat import native_
from transaction._compat import bytes_
from transaction._compat import StringIO
from transaction._compat import text_type

_marker = object()

Expand Down Expand Up @@ -134,15 +135,19 @@ def user(self):

@user.setter
def user(self, v):
self._user = v + u'' # + u'' to make sure it's unicode
if not isinstance(v, text_type):
raise TypeError("User must be text (unicodd)")
self._user = v

@property
def description(self):
return self._description

@description.setter
def description(self, v):
self._description = v + u'' # + u'' to make sure it's unicode
if not isinstance(v, text_type):
raise TypeError("Description must be text (unicodd)")
self._description = v

def isDoomed(self):
""" See ITransaction.
Expand Down Expand Up @@ -528,15 +533,22 @@ def abort(self):
def note(self, text):
""" See ITransaction.
"""
if not isinstance(text, text_type):
raise TypeError("Note must be text (unicodd)")

text = text.strip()
if self.description:
self.description += u"\n" + text
else:
self.description = text

def setUser(self, user_name, path="/"):
def setUser(self, user_name, path=u"/"):
""" See ITransaction.
"""
if not isinstance(user_name, text_type):
raise TypeError("User name must be text (unicodd)")
if not isinstance(path, text_type):
raise TypeError("User name must be text (unicodd)")
self.user = u"%s %s" % (path, user_name)

def setExtendedInfo(self, name, value):
Expand Down
22 changes: 14 additions & 8 deletions transaction/tests/test__transaction.py
Expand Up @@ -983,32 +983,38 @@ class DM(object):
def test_note(self):
txn = self._makeOne()
try:
txn.note('This is a note.')
txn.note(u'This is a note.')
self.assertEqual(txn.description, u'This is a note.')
txn.note('Another.')
txn.note(u'Another.')
self.assertEqual(txn.description, u'This is a note.\nAnother.')
finally:
txn.abort()

def test_description_nonascii_bytes(self):
def test_description_bytes(self):
txn = self._makeOne()
with self.assertRaises((UnicodeDecodeError, TypeError)):
txn.description = b'\xc2\x80'
txn.description = b'haha'

def test_setUser_default_path(self):
txn = self._makeOne()
txn.setUser('phreddy')
txn.setUser(u'phreddy')
self.assertEqual(txn.user, u'/ phreddy')

def test_setUser_explicit_path(self):
txn = self._makeOne()
txn.setUser('phreddy', '/bedrock')
txn.setUser(u'phreddy', u'/bedrock')
self.assertEqual(txn.user, u'/bedrock phreddy')

def test_user_nonascii_bytes(self):
def test_user_bytes(self):
txn = self._makeOne()
with self.assertRaises((UnicodeDecodeError, TypeError)):
txn.user = b'\xc2\x80'
txn.user = b'phreddy'
with self.assertRaises((UnicodeDecodeError, TypeError)):
txn.setUser(b'phreddy', u'/bedrock')
with self.assertRaises((UnicodeDecodeError, TypeError)):
txn.setUser(u'phreddy', b'/bedrock')
with self.assertRaises((UnicodeDecodeError, TypeError)):
txn.setUser(b'phreddy')

def test_setExtendedInfo_single(self):
txn = self._makeOne()
Expand Down

0 comments on commit 8f507c4

Please sign in to comment.