From aa97f0496412ed834aada921e29588ed16d68e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20D=C3=B6rwald?= Date: Thu, 3 May 2007 21:05:51 +0000 Subject: Fix various spots where int/long and str/unicode unification lead to type checks like isinstance(foo, (str, str)) or isinstance(foo, (int, int)). --- Lib/ctypes/__init__.py | 10 ++++----- Lib/ctypes/test/test_as_parameter.py | 2 +- Lib/ctypes/test/test_functions.py | 2 +- Lib/decimal.py | 14 ++++++------- Lib/doctest.py | 2 +- Lib/msilib/__init__.py | 2 +- Lib/plat-mac/EasyDialogs.py | 2 +- Lib/plat-mac/plistlib.py | 8 ++++---- Lib/random.py | 2 +- Lib/subprocess.py | 2 +- Lib/test/test_array.py | 2 +- Lib/test/test_long.py | 2 +- Lib/test/test_struct.py | 9 ++++---- Lib/test/test_unicode.py | 40 ++++++++++++++++-------------------- 14 files changed, 47 insertions(+), 52 deletions(-) diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 7609017..b753a6b 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -59,14 +59,14 @@ def create_string_buffer(init, size=None): create_string_buffer(anInteger) -> character array create_string_buffer(aString, anInteger) -> character array """ - if isinstance(init, (str, str)): + if isinstance(init, str): if size is None: size = len(init)+1 buftype = c_char * size buf = buftype() buf.value = init return buf - elif isinstance(init, (int, int)): + elif isinstance(init, int): buftype = c_char * init buf = buftype() return buf @@ -281,14 +281,14 @@ else: create_unicode_buffer(anInteger) -> character array create_unicode_buffer(aString, anInteger) -> character array """ - if isinstance(init, (str, str)): + if isinstance(init, str): if size is None: size = len(init)+1 buftype = c_wchar * size buf = buftype() buf.value = init return buf - elif isinstance(init, (int, int)): + elif isinstance(init, int): buftype = c_wchar * init buf = buftype() return buf @@ -359,7 +359,7 @@ class CDLL(object): def __getitem__(self, name_or_ordinal): func = self._FuncPtr((name_or_ordinal, self)) - if not isinstance(name_or_ordinal, (int, int)): + if not isinstance(name_or_ordinal, int): func.__name__ = name_or_ordinal return func diff --git a/Lib/ctypes/test/test_as_parameter.py b/Lib/ctypes/test/test_as_parameter.py index e21782b..884361c 100644 --- a/Lib/ctypes/test/test_as_parameter.py +++ b/Lib/ctypes/test/test_as_parameter.py @@ -133,7 +133,7 @@ class BasicWrapTestCase(unittest.TestCase): f.argtypes = [c_longlong, MyCallback] def callback(value): - self.failUnless(isinstance(value, (int, int))) + self.failUnless(isinstance(value, int)) return value & 0x7FFFFFFF cb = MyCallback(callback) diff --git a/Lib/ctypes/test/test_functions.py b/Lib/ctypes/test/test_functions.py index bbcbbc1..9e5a6f8 100644 --- a/Lib/ctypes/test/test_functions.py +++ b/Lib/ctypes/test/test_functions.py @@ -293,7 +293,7 @@ class FunctionTestCase(unittest.TestCase): f.argtypes = [c_longlong, MyCallback] def callback(value): - self.failUnless(isinstance(value, (int, int))) + self.failUnless(isinstance(value, int)) return value & 0x7FFFFFFF cb = MyCallback(callback) diff --git a/Lib/decimal.py b/Lib/decimal.py index a7238e1..2611f79 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -741,32 +741,32 @@ class Decimal(object): return 1 def __eq__(self, other): - if not isinstance(other, (Decimal, int, int)): + if not isinstance(other, (Decimal, int)): return NotImplemented return self.__cmp__(other) == 0 def __ne__(self, other): - if not isinstance(other, (Decimal, int, int)): + if not isinstance(other, (Decimal, int)): return NotImplemented return self.__cmp__(other) != 0 def __lt__(self, other): - if not isinstance(other, (Decimal, int, int)): + if not isinstance(other, (Decimal, int)): return NotImplemented return self.__cmp__(other) < 0 def __le__(self, other): - if not isinstance(other, (Decimal, int, int)): + if not isinstance(other, (Decimal, int)): return NotImplemented return self.__cmp__(other) <= 0 def __gt__(self, other): - if not isinstance(other, (Decimal, int, int)): + if not isinstance(other, (Decimal, int)): return NotImplemented return self.__cmp__(other) > 0 def __ge__(self, other): - if not isinstance(other, (Decimal, int, int)): + if not isinstance(other, (Decimal, int)): return NotImplemented return self.__cmp__(other) >= 0 @@ -2993,7 +2993,7 @@ def _convert_other(other): """ if isinstance(other, Decimal): return other - if isinstance(other, (int, int)): + if isinstance(other, int): return Decimal(other) return NotImplemented diff --git a/Lib/doctest.py b/Lib/doctest.py index d237811..cb29fcb 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -196,7 +196,7 @@ def _normalize_module(module, depth=2): """ if inspect.ismodule(module): return module - elif isinstance(module, (str, str)): + elif isinstance(module, str): return __import__(module, globals(), locals(), ["*"]) elif module is None: return sys.modules[sys._getframe(depth).f_globals['__name__']] diff --git a/Lib/msilib/__init__.py b/Lib/msilib/__init__.py index 27b03ea..ad3bf62 100644 --- a/Lib/msilib/__init__.py +++ b/Lib/msilib/__init__.py @@ -99,7 +99,7 @@ def add_data(db, table, values): assert len(value) == count, value for i in range(count): field = value[i] - if isinstance(field, (int, int)): + if isinstance(field, int): r.SetInteger(i+1,field) elif isinstance(field, basestring): r.SetString(i+1,field) diff --git a/Lib/plat-mac/EasyDialogs.py b/Lib/plat-mac/EasyDialogs.py index 6d157f9..dfed24f 100644 --- a/Lib/plat-mac/EasyDialogs.py +++ b/Lib/plat-mac/EasyDialogs.py @@ -713,7 +713,7 @@ def AskFileForSave( raise TypeError, "Cannot pass wanted=FSRef to AskFileForSave" if issubclass(tpwanted, Carbon.File.FSSpec): return tpwanted(rr.selection[0]) - if issubclass(tpwanted, (str, str)): + if issubclass(tpwanted, str): if sys.platform == 'mac': fullpath = rr.selection[0].as_pathname() else: diff --git a/Lib/plat-mac/plistlib.py b/Lib/plat-mac/plistlib.py index b040237..049b50b 100644 --- a/Lib/plat-mac/plistlib.py +++ b/Lib/plat-mac/plistlib.py @@ -70,7 +70,7 @@ def readPlist(pathOrFile): usually is a dictionary). """ didOpen = 0 - if isinstance(pathOrFile, (str, str)): + if isinstance(pathOrFile, str): pathOrFile = open(pathOrFile) didOpen = 1 p = PlistParser() @@ -85,7 +85,7 @@ def writePlist(rootObject, pathOrFile): file name or a (writable) file object. """ didOpen = 0 - if isinstance(pathOrFile, (str, str)): + if isinstance(pathOrFile, str): pathOrFile = open(pathOrFile, "w") didOpen = 1 writer = PlistWriter(pathOrFile) @@ -231,7 +231,7 @@ class PlistWriter(DumbXMLWriter): DumbXMLWriter.__init__(self, file, indentLevel, indent) def writeValue(self, value): - if isinstance(value, (str, str)): + if isinstance(value, str): self.simpleElement("string", value) elif isinstance(value, bool): # must switch for bool before int, as bool is a @@ -270,7 +270,7 @@ class PlistWriter(DumbXMLWriter): self.beginElement("dict") items = sorted(d.items()) for key, value in items: - if not isinstance(key, (str, str)): + if not isinstance(key, str): raise TypeError("keys must be strings") self.simpleElement("key", key) self.writeValue(value) diff --git a/Lib/random.py b/Lib/random.py index 8adcff5..99ae750 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -631,7 +631,7 @@ class WichmannHill(Random): import time a = int(time.time() * 256) # use fractional seconds - if not isinstance(a, (int, int)): + if not isinstance(a, int): a = hash(a) a, x = divmod(a, 30268) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 2aa02ae..363c827 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -541,7 +541,7 @@ class Popen(object): _cleanup() self._child_created = False - if not isinstance(bufsize, (int, int)): + if not isinstance(bufsize, int): raise TypeError("bufsize must be an integer") if mswindows: diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index b845570..5278f23 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -65,7 +65,7 @@ class BaseTest(unittest.TestCase): bi = a.buffer_info() self.assert_(isinstance(bi, tuple)) self.assertEqual(len(bi), 2) - self.assert_(isinstance(bi[0], (int, int))) + self.assert_(isinstance(bi[0], int)) self.assert_(isinstance(bi[1], int)) self.assertEqual(bi[1], len(a)) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 2876f83..1f65202 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -426,7 +426,7 @@ class LongTest(unittest.TestCase): # represents all Python ints, longs and floats exactly). class Rat: def __init__(self, value): - if isinstance(value, (int, int)): + if isinstance(value, int): self.n = value self.d = 1 elif isinstance(value, float): diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index b62d74c..c3df222 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -492,12 +492,11 @@ test_705836() def test_1229380(): import sys for endian in ('', '>', '<'): - for cls in (int, int): - for fmt in ('B', 'H', 'I', 'L'): - deprecated_err(struct.pack, endian + fmt, cls(-1)) + for fmt in ('B', 'H', 'I', 'L'): + deprecated_err(struct.pack, endian + fmt, -1) - deprecated_err(struct.pack, endian + 'B', cls(300)) - deprecated_err(struct.pack, endian + 'H', cls(70000)) + deprecated_err(struct.pack, endian + 'B', 300) + deprecated_err(struct.pack, endian + 'H', 70000) deprecated_err(struct.pack, endian + 'I', sys.maxint * 4) deprecated_err(struct.pack, endian + 'L', sys.maxint * 4) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 3dd92ae..ccfa922 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -134,31 +134,27 @@ class UnicodeTest( def test_index(self): string_tests.CommonTest.test_index(self) - # check mixed argument types - for (t1, t2) in ((str, str), (str, str)): - self.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2('')) - self.checkequalnofix(3, t1('abcdefghiabc'), 'index', t2('def')) - self.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2('abc')) - self.checkequalnofix(9, t1('abcdefghiabc'), 'index', t2('abc'), 1) - self.assertRaises(ValueError, t1('abcdefghiabc').index, t2('hib')) - self.assertRaises(ValueError, t1('abcdefghiab').index, t2('abc'), 1) - self.assertRaises(ValueError, t1('abcdefghi').index, t2('ghi'), 8) - self.assertRaises(ValueError, t1('abcdefghi').index, t2('ghi'), -1) + self.checkequalnofix(0, 'abcdefghiabc', 'index', '') + self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def') + self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc') + self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1) + self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib') + self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1) + self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8) + self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1) def test_rindex(self): string_tests.CommonTest.test_rindex(self) - # check mixed argument types - for (t1, t2) in ((str, str), (str, str)): - self.checkequalnofix(12, t1('abcdefghiabc'), 'rindex', t2('')) - self.checkequalnofix(3, t1('abcdefghiabc'), 'rindex', t2('def')) - self.checkequalnofix(9, t1('abcdefghiabc'), 'rindex', t2('abc')) - self.checkequalnofix(0, t1('abcdefghiabc'), 'rindex', t2('abc'), 0, -1) - - self.assertRaises(ValueError, t1('abcdefghiabc').rindex, t2('hib')) - self.assertRaises(ValueError, t1('defghiabc').rindex, t2('def'), 1) - self.assertRaises(ValueError, t1('defghiabc').rindex, t2('abc'), 0, -1) - self.assertRaises(ValueError, t1('abcdefghi').rindex, t2('ghi'), 0, 8) - self.assertRaises(ValueError, t1('abcdefghi').rindex, t2('ghi'), 0, -1) + self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '') + self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def') + self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc') + self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1) + + self.assertRaises(ValueError, 'abcdefghiabc'.rindex, 'hib') + self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1) + self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1) + self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8) + self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1) def test_translate(self): self.checkequalnofix('bbbc', 'abababc', 'translate', {ord('a'):None}) -- cgit v0.12