summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGregory P. Smith <greg@mad-scientist.com>2007-10-16 06:31:30 (GMT)
committerGregory P. Smith <greg@mad-scientist.com>2007-10-16 06:31:30 (GMT)
commit60d241f135f10312f5a638846659d7e471f6cac9 (patch)
tree620abe8a5e99620cec51a3476ea96c2459956e70 /Lib
parent3d2fd7f923c35fe61c0f26f2ae150d73a42859b9 (diff)
downloadcpython-60d241f135f10312f5a638846659d7e471f6cac9.zip
cpython-60d241f135f10312f5a638846659d7e471f6cac9.tar.gz
cpython-60d241f135f10312f5a638846659d7e471f6cac9.tar.bz2
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
to be called a buffer). Shares code with stringobject when possible. Adds unit tests with common code that should be usable to test the PEPs mutable buffer() and immutable bytes() types. http://bugs.python.org/issue1261
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/buffer_tests.py206
-rw-r--r--Lib/test/test_bytes.py48
2 files changed, 244 insertions, 10 deletions
diff --git a/Lib/test/buffer_tests.py b/Lib/test/buffer_tests.py
new file mode 100644
index 0000000..01ac3c5
--- /dev/null
+++ b/Lib/test/buffer_tests.py
@@ -0,0 +1,206 @@
+# Tests that work for both str8 (bytes) and bytes (buffer) objects.
+# See PEP 3137.
+
+import struct
+import sys
+
+class MixinBytesBufferCommonTests(object):
+ """Tests that work for both str8 (bytes) and bytes (buffer) objects.
+ See PEP 3137.
+ """
+
+ def marshal(self, x):
+ """Convert x into the appropriate type for these tests."""
+ raise RuntimeError('test class must provide a marshal method')
+
+ def test_islower(self):
+ self.assertFalse(self.marshal(b'').islower())
+ self.assert_(self.marshal(b'a').islower())
+ self.assertFalse(self.marshal(b'A').islower())
+ self.assertFalse(self.marshal(b'\n').islower())
+ self.assert_(self.marshal(b'abc').islower())
+ self.assertFalse(self.marshal(b'aBc').islower())
+ self.assert_(self.marshal(b'abc\n').islower())
+ self.assertRaises(TypeError, self.marshal(b'abc').islower, 42)
+
+ def test_isupper(self):
+ self.assertFalse(self.marshal(b'').isupper())
+ self.assertFalse(self.marshal(b'a').isupper())
+ self.assert_(self.marshal(b'A').isupper())
+ self.assertFalse(self.marshal(b'\n').isupper())
+ self.assert_(self.marshal(b'ABC').isupper())
+ self.assertFalse(self.marshal(b'AbC').isupper())
+ self.assert_(self.marshal(b'ABC\n').isupper())
+ self.assertRaises(TypeError, self.marshal(b'abc').isupper, 42)
+
+ def test_istitle(self):
+ self.assertFalse(self.marshal(b'').istitle())
+ self.assertFalse(self.marshal(b'a').istitle())
+ self.assert_(self.marshal(b'A').istitle())
+ self.assertFalse(self.marshal(b'\n').istitle())
+ self.assert_(self.marshal(b'A Titlecased Line').istitle())
+ self.assert_(self.marshal(b'A\nTitlecased Line').istitle())
+ self.assert_(self.marshal(b'A Titlecased, Line').istitle())
+ self.assertFalse(self.marshal(b'Not a capitalized String').istitle())
+ self.assertFalse(self.marshal(b'Not\ta Titlecase String').istitle())
+ self.assertFalse(self.marshal(b'Not--a Titlecase String').istitle())
+ self.assertFalse(self.marshal(b'NOT').istitle())
+ self.assertRaises(TypeError, self.marshal(b'abc').istitle, 42)
+
+ def test_isspace(self):
+ self.assertFalse(self.marshal(b'').isspace())
+ self.assertFalse(self.marshal(b'a').isspace())
+ self.assert_(self.marshal(b' ').isspace())
+ self.assert_(self.marshal(b'\t').isspace())
+ self.assert_(self.marshal(b'\r').isspace())
+ self.assert_(self.marshal(b'\n').isspace())
+ self.assert_(self.marshal(b' \t\r\n').isspace())
+ self.assertFalse(self.marshal(b' \t\r\na').isspace())
+ self.assertRaises(TypeError, self.marshal(b'abc').isspace, 42)
+
+ def test_isalpha(self):
+ self.assertFalse(self.marshal(b'').isalpha())
+ self.assert_(self.marshal(b'a').isalpha())
+ self.assert_(self.marshal(b'A').isalpha())
+ self.assertFalse(self.marshal(b'\n').isalpha())
+ self.assert_(self.marshal(b'abc').isalpha())
+ self.assertFalse(self.marshal(b'aBc123').isalpha())
+ self.assertFalse(self.marshal(b'abc\n').isalpha())
+ self.assertRaises(TypeError, self.marshal(b'abc').isalpha, 42)
+
+ def test_isalnum(self):
+ self.assertFalse(self.marshal(b'').isalnum())
+ self.assert_(self.marshal(b'a').isalnum())
+ self.assert_(self.marshal(b'A').isalnum())
+ self.assertFalse(self.marshal(b'\n').isalnum())
+ self.assert_(self.marshal(b'123abc456').isalnum())
+ self.assert_(self.marshal(b'a1b3c').isalnum())
+ self.assertFalse(self.marshal(b'aBc000 ').isalnum())
+ self.assertFalse(self.marshal(b'abc\n').isalnum())
+ self.assertRaises(TypeError, self.marshal(b'abc').isalnum, 42)
+
+ def test_isdigit(self):
+ self.assertFalse(self.marshal(b'').isdigit())
+ self.assertFalse(self.marshal(b'a').isdigit())
+ self.assert_(self.marshal(b'0').isdigit())
+ self.assert_(self.marshal(b'0123456789').isdigit())
+ self.assertFalse(self.marshal(b'0123456789a').isdigit())
+
+ self.assertRaises(TypeError, self.marshal(b'abc').isdigit, 42)
+
+ def test_lower(self):
+ self.assertEqual(b'hello', self.marshal(b'HeLLo').lower())
+ self.assertEqual(b'hello', self.marshal(b'hello').lower())
+ self.assertRaises(TypeError, self.marshal(b'hello').lower, 42)
+
+ def test_upper(self):
+ self.assertEqual(b'HELLO', self.marshal(b'HeLLo').upper())
+ self.assertEqual(b'HELLO', self.marshal(b'HELLO').upper())
+ self.assertRaises(TypeError, self.marshal(b'hello').upper, 42)
+
+ def test_capitalize(self):
+ self.assertEqual(b' hello ', self.marshal(b' hello ').capitalize())
+ self.assertEqual(b'Hello ', self.marshal(b'Hello ').capitalize())
+ self.assertEqual(b'Hello ', self.marshal(b'hello ').capitalize())
+ self.assertEqual(b'Aaaa', self.marshal(b'aaaa').capitalize())
+ self.assertEqual(b'Aaaa', self.marshal(b'AaAa').capitalize())
+
+ self.assertRaises(TypeError, self.marshal(b'hello').capitalize, 42)
+
+ def test_ljust(self):
+ self.assertEqual(b'abc ', self.marshal(b'abc').ljust(10))
+ self.assertEqual(b'abc ', self.marshal(b'abc').ljust(6))
+ self.assertEqual(b'abc', self.marshal(b'abc').ljust(3))
+ self.assertEqual(b'abc', self.marshal(b'abc').ljust(2))
+ self.assertEqual(b'abc*******', self.marshal(b'abc').ljust(10, '*'))
+ self.assertRaises(TypeError, self.marshal(b'abc').ljust)
+
+ def test_rjust(self):
+ self.assertEqual(b' abc', self.marshal(b'abc').rjust(10))
+ self.assertEqual(b' abc', self.marshal(b'abc').rjust(6))
+ self.assertEqual(b'abc', self.marshal(b'abc').rjust(3))
+ self.assertEqual(b'abc', self.marshal(b'abc').rjust(2))
+ self.assertEqual(b'*******abc', self.marshal(b'abc').rjust(10, '*'))
+ self.assertRaises(TypeError, self.marshal(b'abc').rjust)
+
+ def test_center(self):
+ self.assertEqual(b' abc ', self.marshal(b'abc').center(10))
+ self.assertEqual(b' abc ', self.marshal(b'abc').center(6))
+ self.assertEqual(b'abc', self.marshal(b'abc').center(3))
+ self.assertEqual(b'abc', self.marshal(b'abc').center(2))
+ self.assertEqual(b'***abc****', self.marshal(b'abc').center(10, '*'))
+ self.assertRaises(TypeError, self.marshal(b'abc').center)
+
+ def test_swapcase(self):
+ self.assertEqual(b'hEllO CoMPuTErS',
+ self.marshal(b'HeLLo cOmpUteRs').swapcase())
+
+ self.assertRaises(TypeError, self.marshal(b'hello').swapcase, 42)
+
+ def test_zfill(self):
+ self.assertEqual(b'123', self.marshal(b'123').zfill(2))
+ self.assertEqual(b'123', self.marshal(b'123').zfill(3))
+ self.assertEqual(b'0123', self.marshal(b'123').zfill(4))
+ self.assertEqual(b'+123', self.marshal(b'+123').zfill(3))
+ self.assertEqual(b'+123', self.marshal(b'+123').zfill(4))
+ self.assertEqual(b'+0123', self.marshal(b'+123').zfill(5))
+ self.assertEqual(b'-123', self.marshal(b'-123').zfill(3))
+ self.assertEqual(b'-123', self.marshal(b'-123').zfill(4))
+ self.assertEqual(b'-0123', self.marshal(b'-123').zfill(5))
+ self.assertEqual(b'000', self.marshal(b'').zfill(3))
+ self.assertEqual(b'34', self.marshal(b'34').zfill(1))
+ self.assertEqual(b'0034', self.marshal(b'34').zfill(4))
+
+ self.assertRaises(TypeError, self.marshal(b'123').zfill)
+
+ def test_expandtabs(self):
+ self.assertEqual(b'abc\rab def\ng hi',
+ self.marshal(b'abc\rab\tdef\ng\thi').expandtabs())
+ self.assertEqual(b'abc\rab def\ng hi',
+ self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(8))
+ self.assertEqual(b'abc\rab def\ng hi',
+ self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(4))
+ self.assertEqual(b'abc\r\nab def\ng hi',
+ self.marshal(b'abc\r\nab\tdef\ng\thi').expandtabs(4))
+ self.assertEqual(b'abc\rab def\ng hi',
+ self.marshal(b'abc\rab\tdef\ng\thi').expandtabs())
+ self.assertEqual(b'abc\rab def\ng hi',
+ self.marshal(b'abc\rab\tdef\ng\thi').expandtabs(8))
+ self.assertEqual(b'abc\r\nab\r\ndef\ng\r\nhi',
+ self.marshal(b'abc\r\nab\r\ndef\ng\r\nhi').expandtabs(4))
+ self.assertEqual(b' a\n b', self.marshal(b' \ta\n\tb').expandtabs(1))
+
+ self.assertRaises(TypeError, self.marshal(b'hello').expandtabs, 42, 42)
+ # This test is only valid when sizeof(int) == sizeof(void*) == 4.
+ if sys.maxint < (1 << 32) and struct.calcsize('P') == 4:
+ self.assertRaises(OverflowError,
+ self.marshal(b'\ta\n\tb').expandtabs, sys.maxint)
+
+ def test_title(self):
+ self.assertEqual(b' Hello ', self.marshal(b' hello ').title())
+ self.assertEqual(b'Hello ', self.marshal(b'hello ').title())
+ self.assertEqual(b'Hello ', self.marshal(b'Hello ').title())
+ self.assertEqual(b'Format This As Title String',
+ self.marshal(b'fOrMaT thIs aS titLe String').title())
+ self.assertEqual(b'Format,This-As*Title;String',
+ self.marshal(b'fOrMaT,thIs-aS*titLe;String').title())
+ self.assertEqual(b'Getint', self.marshal(b'getInt').title())
+ self.assertRaises(TypeError, self.marshal(b'hello').title, 42)
+
+ def test_splitlines(self):
+ self.assertEqual([b'abc', b'def', b'', b'ghi'],
+ self.marshal(b'abc\ndef\n\rghi').splitlines())
+ self.assertEqual([b'abc', b'def', b'', b'ghi'],
+ self.marshal(b'abc\ndef\n\r\nghi').splitlines())
+ self.assertEqual([b'abc', b'def', b'ghi'],
+ self.marshal(b'abc\ndef\r\nghi').splitlines())
+ self.assertEqual([b'abc', b'def', b'ghi'],
+ self.marshal(b'abc\ndef\r\nghi\n').splitlines())
+ self.assertEqual([b'abc', b'def', b'ghi', b''],
+ self.marshal(b'abc\ndef\r\nghi\n\r').splitlines())
+ self.assertEqual([b'', b'abc', b'def', b'ghi', b''],
+ self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines())
+ self.assertEqual([b'\n', b'abc\n', b'def\r\n', b'ghi\n', b'\r'],
+ self.marshal(b'\nabc\ndef\r\nghi\n\r').splitlines(1))
+
+ self.assertRaises(TypeError, self.marshal(b'abc').splitlines, 42, 42)
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
index c51a320..391a660 100644
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -8,6 +8,7 @@ import tempfile
import unittest
import test.test_support
import test.string_tests
+import test.buffer_tests
class BytesTest(unittest.TestCase):
@@ -454,17 +455,18 @@ class BytesTest(unittest.TestCase):
def test_fromhex(self):
self.assertRaises(TypeError, bytes.fromhex)
self.assertRaises(TypeError, bytes.fromhex, 1)
- self.assertEquals(bytes.fromhex(''), bytes())
+ self.assertEquals(bytes.fromhex(b''), bytes())
b = bytes([0x1a, 0x2b, 0x30])
- self.assertEquals(bytes.fromhex('1a2B30'), b)
- self.assertEquals(bytes.fromhex(' 1A 2B 30 '), b)
+ self.assertEquals(bytes.fromhex(b'1a2B30'), b)
+ self.assertEquals(bytes.fromhex(b' 1A 2B 30 '), b)
self.assertEquals(bytes.fromhex(memoryview(b'')), bytes())
self.assertEquals(bytes.fromhex(memoryview(b'0000')), bytes([0, 0]))
- self.assertRaises(ValueError, bytes.fromhex, 'a')
- self.assertRaises(ValueError, bytes.fromhex, 'rt')
- self.assertRaises(ValueError, bytes.fromhex, '1a b cd')
- self.assertRaises(ValueError, bytes.fromhex, '\x00')
- self.assertRaises(ValueError, bytes.fromhex, '12 \x00 34')
+ self.assertRaises(TypeError, bytes.fromhex, '1B')
+ self.assertRaises(ValueError, bytes.fromhex, b'a')
+ self.assertRaises(ValueError, bytes.fromhex, b'rt')
+ self.assertRaises(ValueError, bytes.fromhex, b'1a b cd')
+ self.assertRaises(ValueError, bytes.fromhex, b'\x00')
+ self.assertRaises(ValueError, bytes.fromhex, b'12 \x00 34')
def test_join(self):
self.assertEqual(b"".join([]), bytes())
@@ -504,11 +506,12 @@ class BytesTest(unittest.TestCase):
self.assertEqual(b, b'heo')
self.assertRaises(ValueError, lambda: b.remove(ord('l')))
self.assertRaises(ValueError, lambda: b.remove(400))
- self.assertRaises(ValueError, lambda: b.remove('e'))
+ self.assertRaises(TypeError, lambda: b.remove('e'))
# remove first and last
b.remove(ord('o'))
b.remove(ord('h'))
self.assertEqual(b, b'e')
+ self.assertRaises(TypeError, lambda: b.remove(b'e'))
def test_pop(self):
b = b'world'
@@ -542,6 +545,7 @@ class BytesTest(unittest.TestCase):
b = bytes()
b.append(ord('A'))
self.assertEqual(len(b), 1)
+ self.assertRaises(TypeError, lambda: b.append(b'o'))
def test_insert(self):
b = b'msssspp'
@@ -550,6 +554,7 @@ class BytesTest(unittest.TestCase):
b.insert(-2, ord('i'))
b.insert(1000, ord('i'))
self.assertEqual(b, b'mississippi')
+ self.assertRaises(TypeError, lambda: b.insert(0, b'1'))
def test_startswith(self):
b = b'hello'
@@ -734,6 +739,29 @@ class BytesTest(unittest.TestCase):
# Unfortunately they are all bundled with tests that
# are not appropriate for bytes
+ # I've started porting some of those into buffer_tests.py, we should port
+ # the rest that make sense (the code can be cleaned up to use modern
+ # unittest methods at the same time).
+
+class BufferPEP3137Test(unittest.TestCase,
+ test.buffer_tests.MixinBytesBufferCommonTests):
+ def marshal(self, x):
+ return bytes(x)
+ # TODO this should become:
+ #return buffer(x)
+ # once the bytes -> buffer and str8 -> bytes rename happens
+
+ def test_returns_new_copy(self):
+ val = self.marshal(b'1234')
+ # On immutable types these MAY return a reference to themselves
+ # but on mutable types like buffer they MUST return a new copy.
+ for methname in ('zfill', 'rjust', 'ljust', 'center'):
+ method = getattr(val, methname)
+ newval = method(3)
+ self.assertEqual(val, newval)
+ self.assertTrue(val is not newval,
+ methname+' returned self on a mutable object')
+
class BytesAsStringTest(test.string_tests.BaseTest):
type2test = bytes
@@ -759,7 +787,7 @@ class BytesAsStringTest(test.string_tests.BaseTest):
def test_main():
test.test_support.run_unittest(BytesTest)
test.test_support.run_unittest(BytesAsStringTest)
-
+ test.test_support.run_unittest(BufferPEP3137Test)
if __name__ == "__main__":
##test_main()