mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	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
					
				
					 14 changed files with 46 additions and 51 deletions
				
			
		|  | @ -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 @@ def create_unicode_buffer(init, size=None): | |||
|         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 @@ def __getattr__(self, name): | |||
| 
 | ||||
|     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 | ||||
| 
 | ||||
|  |  | |||
|  | @ -133,7 +133,7 @@ def test_longlong_callbacks(self): | |||
|         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) | ||||
|  |  | |||
|  | @ -293,7 +293,7 @@ def test_longlong_callbacks(self): | |||
|         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) | ||||
|  |  | |||
|  | @ -741,32 +741,32 @@ def __cmp__(self, other, context=None): | |||
|         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 | ||||
| 
 | ||||
|  |  | |||
|  | @ -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__']] | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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: | ||||
|  |  | |||
|  | @ -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 @@ def __init__(self, file, indentLevel=0, indent="\t", writeHeader=1): | |||
|         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 @@ def writeDict(self, d): | |||
|         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) | ||||
|  |  | |||
|  | @ -631,7 +631,7 @@ def seed(self, a=None): | |||
|                 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) | ||||
|  |  | |||
|  | @ -541,7 +541,7 @@ def __init__(self, args, bufsize=0, executable=None, | |||
|         _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: | ||||
|  |  | |||
|  | @ -65,7 +65,7 @@ def test_buffer_info(self): | |||
|         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)) | ||||
| 
 | ||||
|  |  | |||
|  | @ -426,7 +426,7 @@ def test_mixed_compares(self): | |||
|         # 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): | ||||
|  |  | |||
|  | @ -492,12 +492,11 @@ def 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) | ||||
|  |  | |||
|  | @ -134,31 +134,27 @@ def test_rfind(self): | |||
| 
 | ||||
|     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.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, 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.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}) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Walter Dörwald
						Walter Dörwald