Rev 2313: Change what warnings are raised, and add tests that they are used. in http://bzr.arbash-meinel.com/branches/bzr/0.15-dev/unicode_id_warnings
John Arbash Meinel
john at arbash-meinel.com
Fri Mar 2 16:35:19 GMT 2007
At http://bzr.arbash-meinel.com/branches/bzr/0.15-dev/unicode_id_warnings
------------------------------------------------------------
revno: 2313
revision-id: john at arbash-meinel.com-20070302163503-8z3knxzmkzsqmogw
parent: john at arbash-meinel.com-20070302161948-63n53irxqf5f72dp
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: unicode_id_warnings
timestamp: Fri 2007-03-02 10:35:03 -0600
message:
Change what warnings are raised, and add tests that they are used.
modified:
bzrlib/osutils.py osutils.py-20050309040759-eeaff12fbf77ac86
bzrlib/tests/test_osutils.py test_osutils.py-20051201224856-e48ee24c12182989
-------------- next part --------------
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py 2007-03-02 16:19:48 +0000
+++ b/bzrlib/osutils.py 2007-03-02 16:35:03 +0000
@@ -905,33 +905,49 @@
return unicode_or_utf8_string.encode('utf-8')
-def safe_revision_id(unicode_or_utf8_string):
+_revision_id_warning = ('Unicode revision ids were deprecated in bzr 0.15.'
+ ' Revision id generators should be creating utf8'
+ ' revision ids.')
+
+
+def safe_revision_id(unicode_or_utf8_string, warn=True):
"""Revision ids should now be utf8, but at one point they were unicode.
- This is the same as safe_utf8, except it uses the cached encode functions
- to save a little bit of performance.
+ :param unicode_or_utf8_string: A possibly Unicode revision_id. (can also be
+ utf8 or None).
+ :param warn: Functions that are sanitizing user data can set warn=False
+ :return: None or a utf8 revision id.
"""
if (unicode_or_utf8_string is None
or unicode_or_utf8_string.__class__ == str):
return unicode_or_utf8_string
- symbol_versioning.warn('Unicode revision ids were deprecated in bzr 0.15.'
- ' Revision id generators should be creating utf8'
- ' revision ids.', DeprecationWarning, stacklevel=2)
+ if warn:
+ symbol_versioning.warn(_revision_id_warning, DeprecationWarning,
+ stacklevel=2)
return cache_utf8.encode(unicode_or_utf8_string)
-def safe_file_id(unicode_or_utf8_string):
+_file_id_warning = ('Unicode file ids were deprecated in bzr 0.15. File id'
+ ' generators should be creating utf8 file ids.')
+
+
+def safe_file_id(unicode_or_utf8_string, warn=True):
"""File ids should now be utf8, but at one point they were unicode.
This is the same as safe_utf8, except it uses the cached encode functions
to save a little bit of performance.
+
+ :param unicode_or_utf8_string: A possibly Unicode file_id. (can also be
+ utf8 or None).
+ :param warn: Functions that are sanitizing user data can set warn=False
+ :return: None or a utf8 file id.
"""
if (unicode_or_utf8_string is None
or unicode_or_utf8_string.__class__ == str):
return unicode_or_utf8_string
- symbol_versioning.warn('Unicode file ids were deprecated in bzr 0.15.'
- ' File id generators should be creating utf8'
- ' file ids.', DeprecationWarning, stacklevel=2)
+ if warn:
+ symbol_versioning.warn(_file_id_warning, DeprecationWarning,
+ stacklevel=2)
return cache_utf8.encode(unicode_or_utf8_string)
=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py 2007-02-17 21:17:22 +0000
+++ b/bzrlib/tests/test_osutils.py 2007-03-02 16:35:03 +0000
@@ -284,26 +284,25 @@
class TestSafeRevisionId(TestCase):
def test_from_ascii_string(self):
- f = 'foobar'
- self.assertEqual('foobar', osutils.safe_revision_id(f))
- self.assertIs(osutils.safe_utf8(f), f)
+ self.assertEqual('foobar', osutils.safe_revision_id('foobar'))
def test_from_unicode_string_ascii_contents(self):
- self.assertEqual('bargam', osutils.safe_revision_id(u'bargam'))
+ self.assertEqual('bargam',
+ osutils.safe_revision_id(u'bargam', warn=False))
+
+ def test_from_unicode_deprecated(self):
+ self.assertEqual('bargam',
+ self.callDeprecated([osutils._revision_id_warning],
+ osutils.safe_revision_id, u'bargam'))
def test_from_unicode_string_unicode_contents(self):
self.assertEqual('bargam\xc2\xae',
- osutils.safe_revision_id(u'bargam\xae'))
+ osutils.safe_revision_id(u'bargam\xae', warn=False))
def test_from_utf8_string(self):
self.assertEqual('foo\xc2\xae',
osutils.safe_revision_id('foo\xc2\xae'))
- def test_bad_utf8_string(self):
- # This check may eventually go away
- self.assertRaises(BzrBadParameterNotUnicode,
- osutils.safe_revision_id, '\xbb\xbb')
-
def test_none(self):
"""Currently, None is a valid revision_id"""
self.assertEqual(None, osutils.safe_revision_id(None))
@@ -312,25 +311,24 @@
class TestSafeFileId(TestCase):
def test_from_ascii_string(self):
- f = 'foobar'
- self.assertEqual('foobar', osutils.safe_file_id(f))
+ self.assertEqual('foobar', osutils.safe_file_id('foobar'))
def test_from_unicode_string_ascii_contents(self):
- self.assertEqual('bargam', osutils.safe_file_id(u'bargam'))
+ self.assertEqual('bargam', osutils.safe_file_id(u'bargam', warn=False))
+
+ def test_from_unicode_deprecated(self):
+ self.assertEqual('bargam',
+ self.callDeprecated([osutils._file_id_warning],
+ osutils.safe_file_id, u'bargam'))
def test_from_unicode_string_unicode_contents(self):
self.assertEqual('bargam\xc2\xae',
- osutils.safe_file_id(u'bargam\xae'))
+ osutils.safe_file_id(u'bargam\xae', warn=False))
def test_from_utf8_string(self):
self.assertEqual('foo\xc2\xae',
osutils.safe_file_id('foo\xc2\xae'))
- def test_bad_utf8_string(self):
- # This check may eventually go away
- self.assertRaises(BzrBadParameterNotUnicode,
- osutils.safe_file_id, '\xbb\xbb')
-
def test_none(self):
"""Currently, None is a valid revision_id"""
self.assertEqual(None, osutils.safe_file_id(None))
More information about the bazaar-commits
mailing list