Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 720ddb6

Browse files
committed
Use PyString_FromFormat for formatting error messages.
1 parent e0e89f7 commit 720ddb6

1 file changed

Lines changed: 36 additions & 42 deletions

File tree

Python/exceptions.c

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,6 @@ UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
12381238
Py_ssize_t start;
12391239
Py_ssize_t end;
12401240
PyObject *reasonObj = NULL;
1241-
char buffer[1000];
12421241
PyObject *result = NULL;
12431242

12441243
self = arg;
@@ -1260,32 +1259,30 @@ UnicodeEncodeError__str__(PyObject *self, PyObject *arg)
12601259

12611260
if (end==start+1) {
12621261
int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
1263-
char *format;
1262+
char badchar_str[20];
12641263
if (badchar <= 0xff)
1265-
format = "'%.400s' codec can't encode character u'\\x%02x' in position %d: %.400s";
1264+
PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
12661265
else if (badchar <= 0xffff)
1267-
format = "'%.400s' codec can't encode character u'\\u%04x' in position %d: %.400s";
1266+
PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
12681267
else
1269-
format = "'%.400s' codec can't encode character u'\\U%08x' in position %d: %.400s";
1270-
PyOS_snprintf(buffer, sizeof(buffer),
1271-
format,
1268+
PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1269+
result = PyString_FromFormat(
1270+
"'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
12721271
PyString_AS_STRING(encodingObj),
1273-
badchar,
1272+
badchar_str,
12741273
start,
12751274
PyString_AS_STRING(reasonObj)
12761275
);
12771276
}
12781277
else {
1279-
/* XXX %zd? */
1280-
PyOS_snprintf(buffer, sizeof(buffer),
1281-
"'%.400s' codec can't encode characters in position %d-%d: %.400s",
1278+
result = PyString_FromFormat(
1279+
"'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
12821280
PyString_AS_STRING(encodingObj),
1283-
(int)start,
1284-
(int)(end-1),
1281+
start,
1282+
(end-1),
12851283
PyString_AS_STRING(reasonObj)
12861284
);
12871285
}
1288-
result = PyString_FromString(buffer);
12891286

12901287
error:
12911288
Py_XDECREF(reasonObj);
@@ -1324,7 +1321,6 @@ UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
13241321
Py_ssize_t start;
13251322
Py_ssize_t end;
13261323
PyObject *reasonObj = NULL;
1327-
char buffer[1000];
13281324
PyObject *result = NULL;
13291325

13301326
self = arg;
@@ -1345,26 +1341,28 @@ UnicodeDecodeError__str__(PyObject *self, PyObject *arg)
13451341
goto error;
13461342

13471343
if (end==start+1) {
1348-
/* XXX %zd? */
1349-
PyOS_snprintf(buffer, sizeof(buffer),
1350-
"'%.400s' codec can't decode byte 0x%02x in position %d: %.400s",
1344+
/* FromFormat does not support %02x, so format that separately */
1345+
char byte[4];
1346+
PyOS_snprintf(byte, sizeof(byte), "%02x",
1347+
((int)PyString_AS_STRING(objectObj)[start])&0xff);
1348+
result = PyString_FromFormat(
1349+
"'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
13511350
PyString_AS_STRING(encodingObj),
1352-
((int)PyString_AS_STRING(objectObj)[start])&0xff,
1353-
(int)start,
1351+
byte,
1352+
start,
13541353
PyString_AS_STRING(reasonObj)
13551354
);
13561355
}
13571356
else {
1358-
/* XXX %zd? */
1359-
PyOS_snprintf(buffer, sizeof(buffer),
1360-
"'%.400s' codec can't decode bytes in position %d-%d: %.400s",
1357+
result = PyString_FromFormat(
1358+
"'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
13611359
PyString_AS_STRING(encodingObj),
1362-
(int)start,
1363-
(int)(end-1),
1360+
start,
1361+
(end-1),
13641362
PyString_AS_STRING(reasonObj)
13651363
);
13661364
}
1367-
result = PyString_FromString(buffer);
1365+
13681366

13691367
error:
13701368
Py_XDECREF(reasonObj);
@@ -1442,7 +1440,6 @@ UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
14421440
Py_ssize_t start;
14431441
Py_ssize_t end;
14441442
PyObject *reasonObj = NULL;
1445-
char buffer[1000];
14461443
PyObject *result = NULL;
14471444

14481445
self = arg;
@@ -1461,31 +1458,28 @@ UnicodeTranslateError__str__(PyObject *self, PyObject *arg)
14611458

14621459
if (end==start+1) {
14631460
int badchar = (int)PyUnicode_AS_UNICODE(objectObj)[start];
1464-
char *format;
1465-
/* XXX %zd? */
1461+
char badchar_str[20];
14661462
if (badchar <= 0xff)
1467-
format = "can't translate character u'\\x%02x' in position %d: %.400s";
1463+
PyOS_snprintf(badchar_str, sizeof(badchar_str), "x%02x", badchar);
14681464
else if (badchar <= 0xffff)
1469-
format = "can't translate character u'\\u%04x' in position %d: %.400s";
1465+
PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
14701466
else
1471-
format = "can't translate character u'\\U%08x' in position %d: %.400s";
1472-
PyOS_snprintf(buffer, sizeof(buffer),
1473-
format,
1474-
badchar,
1475-
(int)start,
1467+
PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
1468+
result = PyString_FromFormat(
1469+
"can't translate character u'\\%s' in position %zd: %.400s",
1470+
badchar_str,
1471+
start,
14761472
PyString_AS_STRING(reasonObj)
14771473
);
14781474
}
14791475
else {
1480-
/* XXX %zd? */
1481-
PyOS_snprintf(buffer, sizeof(buffer),
1482-
"can't translate characters in position %d-%d: %.400s",
1483-
(int)start,
1484-
(int)(end-1),
1476+
result = PyString_FromFormat(
1477+
"can't translate characters in position %zd-%zd: %.400s",
1478+
start,
1479+
(end-1),
14851480
PyString_AS_STRING(reasonObj)
14861481
);
14871482
}
1488-
result = PyString_FromString(buffer);
14891483

14901484
error:
14911485
Py_XDECREF(reasonObj);

0 commit comments

Comments
 (0)