mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Ensure that complex() only accepts a string argument as the first arg,
and only if there is no second arg. This closes SF patch #479551.
This commit is contained in:
		
							parent
							
								
									733c8935f9
								
							
						
					
					
						commit
						526c7a0101
					
				
					 3 changed files with 26 additions and 4 deletions
				
			
		|  | @ -159,12 +159,14 @@ def my_import(name): | ||||||
| 
 | 
 | ||||||
| \begin{funcdesc}{complex}{real\optional{, imag}} | \begin{funcdesc}{complex}{real\optional{, imag}} | ||||||
|   Create a complex number with the value \var{real} + \var{imag}*j or |   Create a complex number with the value \var{real} + \var{imag}*j or | ||||||
|   convert a string or number to a complex number. |   convert a string or number to a complex number.  If the first | ||||||
|  |   parameter is a string, it will be interpreted as a complex number | ||||||
|  |   and the function must be called without a second parameter.  The | ||||||
|  |   second parameter can never be a string. | ||||||
|   Each argument may be any numeric type (including complex). |   Each argument may be any numeric type (including complex). | ||||||
|   If \var{imag} is omitted, it defaults to zero and the function |   If \var{imag} is omitted, it defaults to zero and the function | ||||||
|   serves as a numeric conversion function like \function{int()}, |   serves as a numeric conversion function like \function{int()}, | ||||||
|   \function{long()} and \function{float()}; in this case it also |   \function{long()} and \function{float()}. | ||||||
|   accepts a string argument which should be a valid complex number. |  | ||||||
| \end{funcdesc} | \end{funcdesc} | ||||||
| 
 | 
 | ||||||
| \begin{funcdesc}{delattr}{object, name} | \begin{funcdesc}{delattr}{object, name} | ||||||
|  |  | ||||||
|  | @ -122,6 +122,14 @@ def __call__(self): pass | ||||||
| if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)' | if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)' | ||||||
| if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)' | if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)' | ||||||
| if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)' | if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)' | ||||||
|  | if complex("1") != 1+0j: raise TestFailed, 'complex("1")' | ||||||
|  | if complex("1j") != 1j: raise TestFailed, 'complex("1j")' | ||||||
|  | try: complex("1", "1") | ||||||
|  | except TypeError: pass | ||||||
|  | else: raise TestFailed, 'complex("1", "1")' | ||||||
|  | try: complex(1, "1") | ||||||
|  | except TypeError: pass | ||||||
|  | else: raise TestFailed, 'complex(1, "1")' | ||||||
| if complex("  3.14+J  ") != 3.14+1j:  raise TestFailed, 'complex("  3.14+J  )"' | if complex("  3.14+J  ") != 3.14+1j:  raise TestFailed, 'complex("  3.14+J  )"' | ||||||
| if have_unicode: | if have_unicode: | ||||||
|     if complex(unicode("  3.14+J  ")) != 3.14+1j: |     if complex(unicode("  3.14+J  ")) != 3.14+1j: | ||||||
|  |  | ||||||
|  | @ -806,8 +806,20 @@ complex_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||||||
| 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, | 	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OO:complex", kwlist, | ||||||
| 					 &r, &i)) | 					 &r, &i)) | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 	if (PyString_Check(r) || PyUnicode_Check(r)) | 	if (PyString_Check(r) || PyUnicode_Check(r)) { | ||||||
|  | 		if (i != NULL) { | ||||||
|  | 			PyErr_SetString(PyExc_TypeError, | ||||||
|  | 					"complex() can't take second arg" | ||||||
|  | 					" if first is a string"); | ||||||
|  | 			return NULL; | ||||||
|  |                 } | ||||||
| 		return complex_subtype_from_string(type, r); | 		return complex_subtype_from_string(type, r); | ||||||
|  | 	} | ||||||
|  | 	if (i != NULL && (PyString_Check(i) || PyUnicode_Check(i))) { | ||||||
|  | 		PyErr_SetString(PyExc_TypeError, | ||||||
|  | 				"complex() second arg can't be a string"); | ||||||
|  | 		return NULL; | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	nbr = r->ob_type->tp_as_number; | 	nbr = r->ob_type->tp_as_number; | ||||||
| 	if (i != NULL) | 	if (i != NULL) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Fred Drake
						Fred Drake