mirror of https://github.com/python/cpython.git
Fix various spots where int/long and str/unicode unification
lead to type checks like isinstance(foo, (str, str)) or isinstance(foo, (int, int)).
This commit is contained in:
parent
5d7a7001d9
commit
aa97f04964
|
@ -59,14 +59,14 @@ def create_string_buffer(init, size=None):
|
||||||
create_string_buffer(anInteger) -> character array
|
create_string_buffer(anInteger) -> character array
|
||||||
create_string_buffer(aString, anInteger) -> character array
|
create_string_buffer(aString, anInteger) -> character array
|
||||||
"""
|
"""
|
||||||
if isinstance(init, (str, str)):
|
if isinstance(init, str):
|
||||||
if size is None:
|
if size is None:
|
||||||
size = len(init)+1
|
size = len(init)+1
|
||||||
buftype = c_char * size
|
buftype = c_char * size
|
||||||
buf = buftype()
|
buf = buftype()
|
||||||
buf.value = init
|
buf.value = init
|
||||||
return buf
|
return buf
|
||||||
elif isinstance(init, (int, int)):
|
elif isinstance(init, int):
|
||||||
buftype = c_char * init
|
buftype = c_char * init
|
||||||
buf = buftype()
|
buf = buftype()
|
||||||
return buf
|
return buf
|
||||||
|
@ -281,14 +281,14 @@ def create_unicode_buffer(init, size=None):
|
||||||
create_unicode_buffer(anInteger) -> character array
|
create_unicode_buffer(anInteger) -> character array
|
||||||
create_unicode_buffer(aString, anInteger) -> character array
|
create_unicode_buffer(aString, anInteger) -> character array
|
||||||
"""
|
"""
|
||||||
if isinstance(init, (str, str)):
|
if isinstance(init, str):
|
||||||
if size is None:
|
if size is None:
|
||||||
size = len(init)+1
|
size = len(init)+1
|
||||||
buftype = c_wchar * size
|
buftype = c_wchar * size
|
||||||
buf = buftype()
|
buf = buftype()
|
||||||
buf.value = init
|
buf.value = init
|
||||||
return buf
|
return buf
|
||||||
elif isinstance(init, (int, int)):
|
elif isinstance(init, int):
|
||||||
buftype = c_wchar * init
|
buftype = c_wchar * init
|
||||||
buf = buftype()
|
buf = buftype()
|
||||||
return buf
|
return buf
|
||||||
|
@ -359,7 +359,7 @@ def __getattr__(self, name):
|
||||||
|
|
||||||
def __getitem__(self, name_or_ordinal):
|
def __getitem__(self, name_or_ordinal):
|
||||||
func = self._FuncPtr((name_or_ordinal, self))
|
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
|
func.__name__ = name_or_ordinal
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
|
@ -133,7 +133,7 @@ def test_longlong_callbacks(self):
|
||||||
f.argtypes = [c_longlong, MyCallback]
|
f.argtypes = [c_longlong, MyCallback]
|
||||||
|
|
||||||
def callback(value):
|
def callback(value):
|
||||||
self.failUnless(isinstance(value, (int, int)))
|
self.failUnless(isinstance(value, int))
|
||||||
return value & 0x7FFFFFFF
|
return value & 0x7FFFFFFF
|
||||||
|
|
||||||
cb = MyCallback(callback)
|
cb = MyCallback(callback)
|
||||||
|
|
|
@ -293,7 +293,7 @@ def test_longlong_callbacks(self):
|
||||||
f.argtypes = [c_longlong, MyCallback]
|
f.argtypes = [c_longlong, MyCallback]
|
||||||
|
|
||||||
def callback(value):
|
def callback(value):
|
||||||
self.failUnless(isinstance(value, (int, int)))
|
self.failUnless(isinstance(value, int))
|
||||||
return value & 0x7FFFFFFF
|
return value & 0x7FFFFFFF
|
||||||
|
|
||||||
cb = MyCallback(callback)
|
cb = MyCallback(callback)
|
||||||
|
|
|
@ -741,32 +741,32 @@ def __cmp__(self, other, context=None):
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, (Decimal, int, int)):
|
if not isinstance(other, (Decimal, int)):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self.__cmp__(other) == 0
|
return self.__cmp__(other) == 0
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
if not isinstance(other, (Decimal, int, int)):
|
if not isinstance(other, (Decimal, int)):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self.__cmp__(other) != 0
|
return self.__cmp__(other) != 0
|
||||||
|
|
||||||
def __lt__(self, other):
|
def __lt__(self, other):
|
||||||
if not isinstance(other, (Decimal, int, int)):
|
if not isinstance(other, (Decimal, int)):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self.__cmp__(other) < 0
|
return self.__cmp__(other) < 0
|
||||||
|
|
||||||
def __le__(self, other):
|
def __le__(self, other):
|
||||||
if not isinstance(other, (Decimal, int, int)):
|
if not isinstance(other, (Decimal, int)):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self.__cmp__(other) <= 0
|
return self.__cmp__(other) <= 0
|
||||||
|
|
||||||
def __gt__(self, other):
|
def __gt__(self, other):
|
||||||
if not isinstance(other, (Decimal, int, int)):
|
if not isinstance(other, (Decimal, int)):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self.__cmp__(other) > 0
|
return self.__cmp__(other) > 0
|
||||||
|
|
||||||
def __ge__(self, other):
|
def __ge__(self, other):
|
||||||
if not isinstance(other, (Decimal, int, int)):
|
if not isinstance(other, (Decimal, int)):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self.__cmp__(other) >= 0
|
return self.__cmp__(other) >= 0
|
||||||
|
|
||||||
|
@ -2993,7 +2993,7 @@ def _convert_other(other):
|
||||||
"""
|
"""
|
||||||
if isinstance(other, Decimal):
|
if isinstance(other, Decimal):
|
||||||
return other
|
return other
|
||||||
if isinstance(other, (int, int)):
|
if isinstance(other, int):
|
||||||
return Decimal(other)
|
return Decimal(other)
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
|
|
|
@ -196,7 +196,7 @@ def _normalize_module(module, depth=2):
|
||||||
"""
|
"""
|
||||||
if inspect.ismodule(module):
|
if inspect.ismodule(module):
|
||||||
return module
|
return module
|
||||||
elif isinstance(module, (str, str)):
|
elif isinstance(module, str):
|
||||||
return __import__(module, globals(), locals(), ["*"])
|
return __import__(module, globals(), locals(), ["*"])
|
||||||
elif module is None:
|
elif module is None:
|
||||||
return sys.modules[sys._getframe(depth).f_globals['__name__']]
|
return sys.modules[sys._getframe(depth).f_globals['__name__']]
|
||||||
|
|
|
@ -99,7 +99,7 @@ def add_data(db, table, values):
|
||||||
assert len(value) == count, value
|
assert len(value) == count, value
|
||||||
for i in range(count):
|
for i in range(count):
|
||||||
field = value[i]
|
field = value[i]
|
||||||
if isinstance(field, (int, int)):
|
if isinstance(field, int):
|
||||||
r.SetInteger(i+1,field)
|
r.SetInteger(i+1,field)
|
||||||
elif isinstance(field, basestring):
|
elif isinstance(field, basestring):
|
||||||
r.SetString(i+1,field)
|
r.SetString(i+1,field)
|
||||||
|
|
|
@ -713,7 +713,7 @@ def AskFileForSave(
|
||||||
raise TypeError, "Cannot pass wanted=FSRef to AskFileForSave"
|
raise TypeError, "Cannot pass wanted=FSRef to AskFileForSave"
|
||||||
if issubclass(tpwanted, Carbon.File.FSSpec):
|
if issubclass(tpwanted, Carbon.File.FSSpec):
|
||||||
return tpwanted(rr.selection[0])
|
return tpwanted(rr.selection[0])
|
||||||
if issubclass(tpwanted, (str, str)):
|
if issubclass(tpwanted, str):
|
||||||
if sys.platform == 'mac':
|
if sys.platform == 'mac':
|
||||||
fullpath = rr.selection[0].as_pathname()
|
fullpath = rr.selection[0].as_pathname()
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -70,7 +70,7 @@ def readPlist(pathOrFile):
|
||||||
usually is a dictionary).
|
usually is a dictionary).
|
||||||
"""
|
"""
|
||||||
didOpen = 0
|
didOpen = 0
|
||||||
if isinstance(pathOrFile, (str, str)):
|
if isinstance(pathOrFile, str):
|
||||||
pathOrFile = open(pathOrFile)
|
pathOrFile = open(pathOrFile)
|
||||||
didOpen = 1
|
didOpen = 1
|
||||||
p = PlistParser()
|
p = PlistParser()
|
||||||
|
@ -85,7 +85,7 @@ def writePlist(rootObject, pathOrFile):
|
||||||
file name or a (writable) file object.
|
file name or a (writable) file object.
|
||||||
"""
|
"""
|
||||||
didOpen = 0
|
didOpen = 0
|
||||||
if isinstance(pathOrFile, (str, str)):
|
if isinstance(pathOrFile, str):
|
||||||
pathOrFile = open(pathOrFile, "w")
|
pathOrFile = open(pathOrFile, "w")
|
||||||
didOpen = 1
|
didOpen = 1
|
||||||
writer = PlistWriter(pathOrFile)
|
writer = PlistWriter(pathOrFile)
|
||||||
|
@ -231,7 +231,7 @@ def __init__(self, file, indentLevel=0, indent="\t", writeHeader=1):
|
||||||
DumbXMLWriter.__init__(self, file, indentLevel, indent)
|
DumbXMLWriter.__init__(self, file, indentLevel, indent)
|
||||||
|
|
||||||
def writeValue(self, value):
|
def writeValue(self, value):
|
||||||
if isinstance(value, (str, str)):
|
if isinstance(value, str):
|
||||||
self.simpleElement("string", value)
|
self.simpleElement("string", value)
|
||||||
elif isinstance(value, bool):
|
elif isinstance(value, bool):
|
||||||
# must switch for bool before int, as bool is a
|
# must switch for bool before int, as bool is a
|
||||||
|
@ -270,7 +270,7 @@ def writeDict(self, d):
|
||||||
self.beginElement("dict")
|
self.beginElement("dict")
|
||||||
items = sorted(d.items())
|
items = sorted(d.items())
|
||||||
for key, value in items:
|
for key, value in items:
|
||||||
if not isinstance(key, (str, str)):
|
if not isinstance(key, str):
|
||||||
raise TypeError("keys must be strings")
|
raise TypeError("keys must be strings")
|
||||||
self.simpleElement("key", key)
|
self.simpleElement("key", key)
|
||||||
self.writeValue(value)
|
self.writeValue(value)
|
||||||
|
|
|
@ -631,7 +631,7 @@ def seed(self, a=None):
|
||||||
import time
|
import time
|
||||||
a = int(time.time() * 256) # use fractional seconds
|
a = int(time.time() * 256) # use fractional seconds
|
||||||
|
|
||||||
if not isinstance(a, (int, int)):
|
if not isinstance(a, int):
|
||||||
a = hash(a)
|
a = hash(a)
|
||||||
|
|
||||||
a, x = divmod(a, 30268)
|
a, x = divmod(a, 30268)
|
||||||
|
|
|
@ -541,7 +541,7 @@ def __init__(self, args, bufsize=0, executable=None,
|
||||||
_cleanup()
|
_cleanup()
|
||||||
|
|
||||||
self._child_created = False
|
self._child_created = False
|
||||||
if not isinstance(bufsize, (int, int)):
|
if not isinstance(bufsize, int):
|
||||||
raise TypeError("bufsize must be an integer")
|
raise TypeError("bufsize must be an integer")
|
||||||
|
|
||||||
if mswindows:
|
if mswindows:
|
||||||
|
|
|
@ -65,7 +65,7 @@ def test_buffer_info(self):
|
||||||
bi = a.buffer_info()
|
bi = a.buffer_info()
|
||||||
self.assert_(isinstance(bi, tuple))
|
self.assert_(isinstance(bi, tuple))
|
||||||
self.assertEqual(len(bi), 2)
|
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.assert_(isinstance(bi[1], int))
|
||||||
self.assertEqual(bi[1], len(a))
|
self.assertEqual(bi[1], len(a))
|
||||||
|
|
||||||
|
|
|
@ -426,7 +426,7 @@ def test_mixed_compares(self):
|
||||||
# represents all Python ints, longs and floats exactly).
|
# represents all Python ints, longs and floats exactly).
|
||||||
class Rat:
|
class Rat:
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
if isinstance(value, (int, int)):
|
if isinstance(value, int):
|
||||||
self.n = value
|
self.n = value
|
||||||
self.d = 1
|
self.d = 1
|
||||||
elif isinstance(value, float):
|
elif isinstance(value, float):
|
||||||
|
|
|
@ -492,12 +492,11 @@ def test_705836():
|
||||||
def test_1229380():
|
def test_1229380():
|
||||||
import sys
|
import sys
|
||||||
for endian in ('', '>', '<'):
|
for endian in ('', '>', '<'):
|
||||||
for cls in (int, int):
|
|
||||||
for fmt in ('B', 'H', 'I', 'L'):
|
for fmt in ('B', 'H', 'I', 'L'):
|
||||||
deprecated_err(struct.pack, endian + fmt, cls(-1))
|
deprecated_err(struct.pack, endian + fmt, -1)
|
||||||
|
|
||||||
deprecated_err(struct.pack, endian + 'B', cls(300))
|
deprecated_err(struct.pack, endian + 'B', 300)
|
||||||
deprecated_err(struct.pack, endian + 'H', cls(70000))
|
deprecated_err(struct.pack, endian + 'H', 70000)
|
||||||
|
|
||||||
deprecated_err(struct.pack, endian + 'I', sys.maxint * 4)
|
deprecated_err(struct.pack, endian + 'I', sys.maxint * 4)
|
||||||
deprecated_err(struct.pack, endian + 'L', sys.maxint * 4)
|
deprecated_err(struct.pack, endian + 'L', sys.maxint * 4)
|
||||||
|
|
|
@ -134,31 +134,27 @@ def test_rfind(self):
|
||||||
|
|
||||||
def test_index(self):
|
def test_index(self):
|
||||||
string_tests.CommonTest.test_index(self)
|
string_tests.CommonTest.test_index(self)
|
||||||
# check mixed argument types
|
self.checkequalnofix(0, 'abcdefghiabc', 'index', '')
|
||||||
for (t1, t2) in ((str, str), (str, str)):
|
self.checkequalnofix(3, 'abcdefghiabc', 'index', 'def')
|
||||||
self.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2(''))
|
self.checkequalnofix(0, 'abcdefghiabc', 'index', 'abc')
|
||||||
self.checkequalnofix(3, t1('abcdefghiabc'), 'index', t2('def'))
|
self.checkequalnofix(9, 'abcdefghiabc', 'index', 'abc', 1)
|
||||||
self.checkequalnofix(0, t1('abcdefghiabc'), 'index', t2('abc'))
|
self.assertRaises(ValueError, 'abcdefghiabc'.index, 'hib')
|
||||||
self.checkequalnofix(9, t1('abcdefghiabc'), 'index', t2('abc'), 1)
|
self.assertRaises(ValueError, 'abcdefghiab'.index, 'abc', 1)
|
||||||
self.assertRaises(ValueError, t1('abcdefghiabc').index, t2('hib'))
|
self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', 8)
|
||||||
self.assertRaises(ValueError, t1('abcdefghiab').index, t2('abc'), 1)
|
self.assertRaises(ValueError, 'abcdefghi'.index, 'ghi', -1)
|
||||||
self.assertRaises(ValueError, t1('abcdefghi').index, t2('ghi'), 8)
|
|
||||||
self.assertRaises(ValueError, t1('abcdefghi').index, t2('ghi'), -1)
|
|
||||||
|
|
||||||
def test_rindex(self):
|
def test_rindex(self):
|
||||||
string_tests.CommonTest.test_rindex(self)
|
string_tests.CommonTest.test_rindex(self)
|
||||||
# check mixed argument types
|
self.checkequalnofix(12, 'abcdefghiabc', 'rindex', '')
|
||||||
for (t1, t2) in ((str, str), (str, str)):
|
self.checkequalnofix(3, 'abcdefghiabc', 'rindex', 'def')
|
||||||
self.checkequalnofix(12, t1('abcdefghiabc'), 'rindex', t2(''))
|
self.checkequalnofix(9, 'abcdefghiabc', 'rindex', 'abc')
|
||||||
self.checkequalnofix(3, t1('abcdefghiabc'), 'rindex', t2('def'))
|
self.checkequalnofix(0, 'abcdefghiabc', 'rindex', 'abc', 0, -1)
|
||||||
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, 'abcdefghiabc'.rindex, 'hib')
|
||||||
self.assertRaises(ValueError, t1('defghiabc').rindex, t2('def'), 1)
|
self.assertRaises(ValueError, 'defghiabc'.rindex, 'def', 1)
|
||||||
self.assertRaises(ValueError, t1('defghiabc').rindex, t2('abc'), 0, -1)
|
self.assertRaises(ValueError, 'defghiabc'.rindex, 'abc', 0, -1)
|
||||||
self.assertRaises(ValueError, t1('abcdefghi').rindex, t2('ghi'), 0, 8)
|
self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, 8)
|
||||||
self.assertRaises(ValueError, t1('abcdefghi').rindex, t2('ghi'), 0, -1)
|
self.assertRaises(ValueError, 'abcdefghi'.rindex, 'ghi', 0, -1)
|
||||||
|
|
||||||
def test_translate(self):
|
def test_translate(self):
|
||||||
self.checkequalnofix('bbbc', 'abababc', 'translate', {ord('a'):None})
|
self.checkequalnofix('bbbc', 'abababc', 'translate', {ord('a'):None})
|
||||||
|
|
Loading…
Reference in New Issue