mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	ord: provide better error messages
This commit is contained in:
		
							parent
							
								
									502d2b4615
								
							
						
					
					
						commit
						394b54d01a
					
				
					 1 changed files with 19 additions and 8 deletions
				
			
		|  | @ -1642,21 +1642,32 @@ builtin_ord(self, args) | ||||||
| { | { | ||||||
| 	PyObject *obj; | 	PyObject *obj; | ||||||
| 	long ord; | 	long ord; | ||||||
|  | 	int size; | ||||||
| 
 | 
 | ||||||
| 	if (!PyArg_ParseTuple(args, "O:ord", &obj)) | 	if (!PyArg_ParseTuple(args, "O:ord", &obj)) | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 
 | 
 | ||||||
| 	if (PyString_Check(obj) && PyString_GET_SIZE(obj) == 1) | 	if (PyString_Check(obj)) { | ||||||
|  | 		size = PyString_GET_SIZE(obj); | ||||||
|  | 		if (size == 1) | ||||||
| 			ord = (long)((unsigned char)*PyString_AS_STRING(obj)); | 			ord = (long)((unsigned char)*PyString_AS_STRING(obj)); | ||||||
| 	else if (PyUnicode_Check(obj) && PyUnicode_GET_SIZE(obj) == 1) | 	} else if (PyUnicode_Check(obj)) { | ||||||
|  | 		size = PyUnicode_GET_SIZE(obj); | ||||||
|  | 		if (size == 1) | ||||||
| 			ord = (long)*PyUnicode_AS_UNICODE(obj); | 			ord = (long)*PyUnicode_AS_UNICODE(obj); | ||||||
| 	else { | 	} else { | ||||||
| 		PyErr_SetString(PyExc_TypeError, | 		PyErr_Format(PyExc_TypeError, | ||||||
| 				"expected a string or unicode character"); | 			     "expected string or unicode character, " \ | ||||||
|  | 			     "%.200s found", obj->ob_type->tp_name); | ||||||
| 		return NULL; | 		return NULL; | ||||||
| 	} | 	} | ||||||
| 
 | 	if (size == 1) | ||||||
| 		return PyInt_FromLong(ord); | 		return PyInt_FromLong(ord); | ||||||
|  | 
 | ||||||
|  | 	PyErr_Format(PyExc_TypeError,  | ||||||
|  | 		     "expected a character, length-%d string found", | ||||||
|  | 		     size); | ||||||
|  | 	return NULL; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static char ord_doc[] = | static char ord_doc[] = | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Jeremy Hylton
						Jeremy Hylton