Mercurial > cpython-lru_cache-weakref
comparison Objects/stringlib/string_format.h @ 61467:f197407bdba2 3.1
Recorded merge of revisions 81032 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
r81032 | antoine.pitrou | 2010-05-09 17:52:27 +0200 (dim., 09 mai 2010) | 9 lines
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
................
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 May 2010 16:14:21 +0000 |
| parents | 2bcac08cfc6d |
| children | d3797e9dfd2c |
comparison
equal
deleted
inserted
replaced
| 61459:f8c19c5f5dd2 | 61467:f197407bdba2 |
|---|---|
| 561 int ok = 0; | 561 int ok = 0; |
| 562 PyObject *result = NULL; | 562 PyObject *result = NULL; |
| 563 PyObject *format_spec_object = NULL; | 563 PyObject *format_spec_object = NULL; |
| 564 PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL; | 564 PyObject *(*formatter)(PyObject *, STRINGLIB_CHAR *, Py_ssize_t) = NULL; |
| 565 STRINGLIB_CHAR* format_spec_start = format_spec->ptr ? | 565 STRINGLIB_CHAR* format_spec_start = format_spec->ptr ? |
| 566 format_spec->ptr : NULL; | 566 format_spec->ptr : NULL; |
| 567 Py_ssize_t format_spec_len = format_spec->ptr ? | 567 Py_ssize_t format_spec_len = format_spec->ptr ? |
| 568 format_spec->end - format_spec->ptr : 0; | 568 format_spec->end - format_spec->ptr : 0; |
| 569 | 569 |
| 570 /* If we know the type exactly, skip the lookup of __format__ and just | 570 /* If we know the type exactly, skip the lookup of __format__ and just |
| 571 call the formatter directly. */ | 571 call the formatter directly. */ |
| 572 if (PyUnicode_CheckExact(fieldobj)) | 572 if (PyUnicode_CheckExact(fieldobj)) |
| 573 formatter = _PyUnicode_FormatAdvanced; | 573 formatter = _PyUnicode_FormatAdvanced; |
| 574 else if (PyLong_CheckExact(fieldobj)) | 574 else if (PyLong_CheckExact(fieldobj)) |
| 575 formatter =_PyLong_FormatAdvanced; | 575 formatter =_PyLong_FormatAdvanced; |
| 576 else if (PyFloat_CheckExact(fieldobj)) | 576 else if (PyFloat_CheckExact(fieldobj)) |
| 577 formatter = _PyFloat_FormatAdvanced; | 577 formatter = _PyFloat_FormatAdvanced; |
| 578 | 578 |
| 579 /* XXX: for 2.6, convert format_spec to the appropriate type | 579 /* XXX: for 2.6, convert format_spec to the appropriate type |
| 580 (unicode, str) */ | 580 (unicode, str) */ |
| 581 | 581 |
| 582 if (formatter) { | 582 if (formatter) { |
| 583 /* we know exactly which formatter will be called when __format__ is | 583 /* we know exactly which formatter will be called when __format__ is |
| 584 looked up, so call it directly, instead. */ | 584 looked up, so call it directly, instead. */ |
| 585 result = formatter(fieldobj, format_spec_start, format_spec_len); | 585 result = formatter(fieldobj, format_spec_start, format_spec_len); |
| 586 } | 586 } |
| 587 else { | 587 else { |
| 588 /* We need to create an object out of the pointers we have, because | 588 /* We need to create an object out of the pointers we have, because |
| 589 __format__ takes a string/unicode object for format_spec. */ | 589 __format__ takes a string/unicode object for format_spec. */ |
| 590 format_spec_object = STRINGLIB_NEW(format_spec_start, | 590 format_spec_object = STRINGLIB_NEW(format_spec_start, |
| 591 format_spec_len); | 591 format_spec_len); |
| 592 if (format_spec_object == NULL) | 592 if (format_spec_object == NULL) |
| 593 goto done; | 593 goto done; |
| 594 | 594 |
| 595 result = PyObject_Format(fieldobj, format_spec_object); | 595 result = PyObject_Format(fieldobj, format_spec_object); |
| 596 } | 596 } |
| 597 if (result == NULL) | 597 if (result == NULL) |
| 598 goto done; | 598 goto done; |
| 599 | 599 |
| 600 #if PY_VERSION_HEX >= 0x03000000 | 600 #if PY_VERSION_HEX >= 0x03000000 |
| 603 assert(PyBytes_Check(result) || PyUnicode_Check(result)); | 603 assert(PyBytes_Check(result) || PyUnicode_Check(result)); |
| 604 | 604 |
| 605 /* Convert result to our type. We could be str, and result could | 605 /* Convert result to our type. We could be str, and result could |
| 606 be unicode */ | 606 be unicode */ |
| 607 { | 607 { |
| 608 PyObject *tmp = STRINGLIB_TOSTR(result); | 608 PyObject *tmp = STRINGLIB_TOSTR(result); |
| 609 if (tmp == NULL) | 609 if (tmp == NULL) |
| 610 goto done; | 610 goto done; |
| 611 Py_DECREF(result); | 611 Py_DECREF(result); |
| 612 result = tmp; | 612 result = tmp; |
| 613 } | 613 } |
| 614 #endif | 614 #endif |
| 615 | 615 |
| 616 ok = output_data(output, | 616 ok = output_data(output, |
| 617 STRINGLIB_STR(result), STRINGLIB_LEN(result)); | 617 STRINGLIB_STR(result), STRINGLIB_LEN(result)); |
| 842 #if PY_VERSION_HEX >= 0x03000000 | 842 #if PY_VERSION_HEX >= 0x03000000 |
| 843 case 'a': | 843 case 'a': |
| 844 return STRINGLIB_TOASCII(obj); | 844 return STRINGLIB_TOASCII(obj); |
| 845 #endif | 845 #endif |
| 846 default: | 846 default: |
| 847 if (conversion > 32 && conversion < 127) { | 847 if (conversion > 32 && conversion < 127) { |
| 848 /* It's the ASCII subrange; casting to char is safe | 848 /* It's the ASCII subrange; casting to char is safe |
| 849 (assuming the execution character set is an ASCII | 849 (assuming the execution character set is an ASCII |
| 850 superset). */ | 850 superset). */ |
| 851 PyErr_Format(PyExc_ValueError, | 851 PyErr_Format(PyExc_ValueError, |
| 852 "Unknown conversion specifier %c", | 852 "Unknown conversion specifier %c", |
| 853 (char)conversion); | 853 (char)conversion); |
| 854 } else | 854 } else |
| 855 PyErr_Format(PyExc_ValueError, | 855 PyErr_Format(PyExc_ValueError, |
| 856 "Unknown conversion specifier \\x%x", | 856 "Unknown conversion specifier \\x%x", |
| 857 (unsigned int)conversion); | 857 (unsigned int)conversion); |
| 858 return NULL; | 858 return NULL; |
| 859 } | 859 } |
| 860 } | 860 } |
| 861 | 861 |
| 862 /* given: | 862 /* given: |
| 1117 if (conversion == '\0') { | 1117 if (conversion == '\0') { |
| 1118 conversion_str = Py_None; | 1118 conversion_str = Py_None; |
| 1119 Py_INCREF(conversion_str); | 1119 Py_INCREF(conversion_str); |
| 1120 } | 1120 } |
| 1121 else | 1121 else |
| 1122 conversion_str = STRINGLIB_NEW(&conversion, 1); | 1122 conversion_str = STRINGLIB_NEW(&conversion, 1); |
| 1123 if (conversion_str == NULL) | 1123 if (conversion_str == NULL) |
| 1124 goto done; | 1124 goto done; |
| 1125 | 1125 |
| 1126 tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str, | 1126 tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str, |
| 1127 conversion_str); | 1127 conversion_str); |
| 1133 return tuple; | 1133 return tuple; |
| 1134 } | 1134 } |
| 1135 } | 1135 } |
| 1136 | 1136 |
| 1137 static PyMethodDef formatteriter_methods[] = { | 1137 static PyMethodDef formatteriter_methods[] = { |
| 1138 {NULL, NULL} /* sentinel */ | 1138 {NULL, NULL} /* sentinel */ |
| 1139 }; | 1139 }; |
| 1140 | 1140 |
| 1141 static PyTypeObject PyFormatterIter_Type = { | 1141 static PyTypeObject PyFormatterIter_Type = { |
| 1142 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 1142 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1143 "formatteriterator", /* tp_name */ | 1143 "formatteriterator", /* tp_name */ |
| 1144 sizeof(formatteriterobject), /* tp_basicsize */ | 1144 sizeof(formatteriterobject), /* tp_basicsize */ |
| 1145 0, /* tp_itemsize */ | 1145 0, /* tp_itemsize */ |
| 1146 /* methods */ | 1146 /* methods */ |
| 1147 (destructor)formatteriter_dealloc, /* tp_dealloc */ | 1147 (destructor)formatteriter_dealloc, /* tp_dealloc */ |
| 1148 0, /* tp_print */ | 1148 0, /* tp_print */ |
| 1149 0, /* tp_getattr */ | 1149 0, /* tp_getattr */ |
| 1150 0, /* tp_setattr */ | 1150 0, /* tp_setattr */ |
| 1151 0, /* tp_reserved */ | 1151 0, /* tp_reserved */ |
| 1152 0, /* tp_repr */ | 1152 0, /* tp_repr */ |
| 1153 0, /* tp_as_number */ | 1153 0, /* tp_as_number */ |
| 1154 0, /* tp_as_sequence */ | 1154 0, /* tp_as_sequence */ |
| 1155 0, /* tp_as_mapping */ | 1155 0, /* tp_as_mapping */ |
| 1156 0, /* tp_hash */ | 1156 0, /* tp_hash */ |
| 1157 0, /* tp_call */ | 1157 0, /* tp_call */ |
| 1158 0, /* tp_str */ | 1158 0, /* tp_str */ |
| 1159 PyObject_GenericGetAttr, /* tp_getattro */ | 1159 PyObject_GenericGetAttr, /* tp_getattro */ |
| 1160 0, /* tp_setattro */ | 1160 0, /* tp_setattro */ |
| 1161 0, /* tp_as_buffer */ | 1161 0, /* tp_as_buffer */ |
| 1162 Py_TPFLAGS_DEFAULT, /* tp_flags */ | 1162 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1163 0, /* tp_doc */ | 1163 0, /* tp_doc */ |
| 1164 0, /* tp_traverse */ | 1164 0, /* tp_traverse */ |
| 1165 0, /* tp_clear */ | 1165 0, /* tp_clear */ |
| 1166 0, /* tp_richcompare */ | 1166 0, /* tp_richcompare */ |
| 1167 0, /* tp_weaklistoffset */ | 1167 0, /* tp_weaklistoffset */ |
| 1168 PyObject_SelfIter, /* tp_iter */ | 1168 PyObject_SelfIter, /* tp_iter */ |
| 1169 (iternextfunc)formatteriter_next, /* tp_iternext */ | 1169 (iternextfunc)formatteriter_next, /* tp_iternext */ |
| 1170 formatteriter_methods, /* tp_methods */ | 1170 formatteriter_methods, /* tp_methods */ |
| 1171 0, | 1171 0, |
| 1172 }; | 1172 }; |
| 1173 | 1173 |
| 1174 /* unicode_formatter_parser is used to implement | 1174 /* unicode_formatter_parser is used to implement |
| 1175 string.Formatter.vformat. it parses a string and returns tuples | 1175 string.Formatter.vformat. it parses a string and returns tuples |
| 1266 return result; | 1266 return result; |
| 1267 } | 1267 } |
| 1268 } | 1268 } |
| 1269 | 1269 |
| 1270 static PyMethodDef fieldnameiter_methods[] = { | 1270 static PyMethodDef fieldnameiter_methods[] = { |
| 1271 {NULL, NULL} /* sentinel */ | 1271 {NULL, NULL} /* sentinel */ |
| 1272 }; | 1272 }; |
| 1273 | 1273 |
| 1274 static PyTypeObject PyFieldNameIter_Type = { | 1274 static PyTypeObject PyFieldNameIter_Type = { |
| 1275 PyVarObject_HEAD_INIT(&PyType_Type, 0) | 1275 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 1276 "fieldnameiterator", /* tp_name */ | 1276 "fieldnameiterator", /* tp_name */ |
| 1277 sizeof(fieldnameiterobject), /* tp_basicsize */ | 1277 sizeof(fieldnameiterobject), /* tp_basicsize */ |
| 1278 0, /* tp_itemsize */ | 1278 0, /* tp_itemsize */ |
| 1279 /* methods */ | 1279 /* methods */ |
| 1280 (destructor)fieldnameiter_dealloc, /* tp_dealloc */ | 1280 (destructor)fieldnameiter_dealloc, /* tp_dealloc */ |
| 1281 0, /* tp_print */ | 1281 0, /* tp_print */ |
| 1282 0, /* tp_getattr */ | 1282 0, /* tp_getattr */ |
| 1283 0, /* tp_setattr */ | 1283 0, /* tp_setattr */ |
| 1284 0, /* tp_reserved */ | 1284 0, /* tp_reserved */ |
| 1285 0, /* tp_repr */ | 1285 0, /* tp_repr */ |
| 1286 0, /* tp_as_number */ | 1286 0, /* tp_as_number */ |
| 1287 0, /* tp_as_sequence */ | 1287 0, /* tp_as_sequence */ |
| 1288 0, /* tp_as_mapping */ | 1288 0, /* tp_as_mapping */ |
| 1289 0, /* tp_hash */ | 1289 0, /* tp_hash */ |
| 1290 0, /* tp_call */ | 1290 0, /* tp_call */ |
| 1291 0, /* tp_str */ | 1291 0, /* tp_str */ |
| 1292 PyObject_GenericGetAttr, /* tp_getattro */ | 1292 PyObject_GenericGetAttr, /* tp_getattro */ |
| 1293 0, /* tp_setattro */ | 1293 0, /* tp_setattro */ |
| 1294 0, /* tp_as_buffer */ | 1294 0, /* tp_as_buffer */ |
| 1295 Py_TPFLAGS_DEFAULT, /* tp_flags */ | 1295 Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1296 0, /* tp_doc */ | 1296 0, /* tp_doc */ |
| 1297 0, /* tp_traverse */ | 1297 0, /* tp_traverse */ |
| 1298 0, /* tp_clear */ | 1298 0, /* tp_clear */ |
| 1299 0, /* tp_richcompare */ | 1299 0, /* tp_richcompare */ |
| 1300 0, /* tp_weaklistoffset */ | 1300 0, /* tp_weaklistoffset */ |
| 1301 PyObject_SelfIter, /* tp_iter */ | 1301 PyObject_SelfIter, /* tp_iter */ |
| 1302 (iternextfunc)fieldnameiter_next, /* tp_iternext */ | 1302 (iternextfunc)fieldnameiter_next, /* tp_iternext */ |
| 1303 fieldnameiter_methods, /* tp_methods */ | 1303 fieldnameiter_methods, /* tp_methods */ |
| 1304 0}; | 1304 0}; |
| 1305 | 1305 |
| 1306 /* unicode_formatter_field_name_split is used to implement | 1306 /* unicode_formatter_field_name_split is used to implement |
| 1307 string.Formatter.vformat. it takes an PEP 3101 "field name", and | 1307 string.Formatter.vformat. it takes an PEP 3101 "field name", and |
| 1308 returns a tuple of (first, rest): "first", the part before the | 1308 returns a tuple of (first, rest): "first", the part before the |
