mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	Refactor and clean up str.format() code (and helpers) in advance of optimizations.
This commit is contained in:
		
							parent
							
								
									30fadc1799
								
							
						
					
					
						commit
						dc13b79a38
					
				
					 15 changed files with 176 additions and 157 deletions
				
			
		|  | @ -42,8 +42,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |||
| #define PY_SSIZE_T_CLEAN | ||||
| #include "Python.h" | ||||
| 
 | ||||
| #include "formatter_unicode.h" | ||||
| 
 | ||||
| #include "unicodeobject.h" | ||||
| #include "ucnhash.h" | ||||
| 
 | ||||
|  | @ -7863,6 +7861,35 @@ PyDoc_STRVAR(format__doc__, | |||
| \n\ | ||||
| "); | ||||
| 
 | ||||
| static PyObject * | ||||
| unicode__format__(PyObject *self, PyObject *args) | ||||
| { | ||||
|     PyObject *format_spec; | ||||
|     PyObject *result = NULL; | ||||
|     PyObject *tmp = NULL; | ||||
| 
 | ||||
|     /* If 2.x, convert format_spec to the same type as value */ | ||||
|     /* This is to allow things like u''.format('') */ | ||||
|     if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) | ||||
|         goto done; | ||||
|     if (!(PyBytes_Check(format_spec) || PyUnicode_Check(format_spec))) { | ||||
|         PyErr_Format(PyExc_TypeError, "__format__ arg must be str " | ||||
| 		     "or unicode, not %s", Py_TYPE(format_spec)->tp_name); | ||||
|         goto done; | ||||
|     } | ||||
|     tmp = PyObject_Unicode(format_spec); | ||||
|     if (tmp == NULL) | ||||
|         goto done; | ||||
|     format_spec = tmp; | ||||
| 
 | ||||
|     result = _PyUnicode_FormatAdvanced(self, | ||||
|                                        PyUnicode_AS_UNICODE(format_spec), | ||||
|                                        PyUnicode_GET_SIZE(format_spec)); | ||||
| done: | ||||
|     Py_XDECREF(tmp); | ||||
|     return result; | ||||
| } | ||||
| 
 | ||||
| PyDoc_STRVAR(p_format__doc__, | ||||
| "S.__format__(format_spec) -> unicode\n\
 | ||||
| \n\ | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Eric Smith
						Eric Smith