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

Skip to content

Commit b0fa831

Browse files
committed
Issue #7415: PyUnicode_FromEncodedObject() now uses the new buffer API
properly. Patch by Stefan Behnel.
1 parent f68c2a7 commit b0fa831

3 files changed

Lines changed: 30 additions & 27 deletions

File tree

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ David Beazley
6262
Robin Becker
6363
Neal Becker
6464
Bill Bedford
65+
Stefan Behnel
6566
Reimer Behrends
6667
Ben Bell
6768
Thomas Bellman

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.2 Alpha 2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #7415: PyUnicode_FromEncodedObject() now uses the new buffer API
16+
properly. Patch by Stefan Behnel.
17+
1518
- Issue #5553: The Py_LOCAL_INLINE macro now results in inlining on
1619
most platforms. Previously, it online inlined when using Microsoft
1720
Visual C.

Objects/unicodeobject.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,53 +1234,52 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,
12341234
const char *encoding,
12351235
const char *errors)
12361236
{
1237-
const char *s = NULL;
1238-
Py_ssize_t len;
1237+
Py_buffer buffer;
12391238
PyObject *v;
12401239

12411240
if (obj == NULL) {
12421241
PyErr_BadInternalCall();
12431242
return NULL;
12441243
}
12451244

1245+
/* Decoding bytes objects is the most common case and should be fast */
1246+
if (PyBytes_Check(obj)) {
1247+
if (PyBytes_GET_SIZE(obj) == 0) {
1248+
Py_INCREF(unicode_empty);
1249+
v = (PyObject *) unicode_empty;
1250+
}
1251+
else {
1252+
v = PyUnicode_Decode(
1253+
PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj),
1254+
encoding, errors);
1255+
}
1256+
return v;
1257+
}
1258+
12461259
if (PyUnicode_Check(obj)) {
12471260
PyErr_SetString(PyExc_TypeError,
12481261
"decoding str is not supported");
12491262
return NULL;
12501263
}
12511264

1252-
/* Coerce object */
1253-
if (PyBytes_Check(obj)) {
1254-
s = PyBytes_AS_STRING(obj);
1255-
len = PyBytes_GET_SIZE(obj);
1256-
}
1257-
else if (PyByteArray_Check(obj)) {
1258-
s = PyByteArray_AS_STRING(obj);
1259-
len = PyByteArray_GET_SIZE(obj);
1260-
}
1261-
else if (PyObject_AsCharBuffer(obj, &s, &len)) {
1262-
/* Overwrite the error message with something more useful in
1263-
case of a TypeError. */
1264-
if (PyErr_ExceptionMatches(PyExc_TypeError))
1265-
PyErr_Format(PyExc_TypeError,
1266-
"coercing to str: need bytes, bytearray or char buffer, "
1267-
"%.80s found",
1268-
Py_TYPE(obj)->tp_name);
1269-
goto onError;
1265+
/* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
1266+
if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {
1267+
PyErr_Format(PyExc_TypeError,
1268+
"coercing to str: need bytes, bytearray "
1269+
"or buffer-like object, %.80s found",
1270+
Py_TYPE(obj)->tp_name);
1271+
return NULL;
12701272
}
12711273

1272-
/* Convert to Unicode */
1273-
if (len == 0) {
1274+
if (buffer.len == 0) {
12741275
Py_INCREF(unicode_empty);
1275-
v = (PyObject *)unicode_empty;
1276+
v = (PyObject *) unicode_empty;
12761277
}
12771278
else
1278-
v = PyUnicode_Decode(s, len, encoding, errors);
1279+
v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors);
12791280

1281+
PyBuffer_Release(&buffer);
12801282
return v;
1281-
1282-
onError:
1283-
return NULL;
12841283
}
12851284

12861285
/* Convert encoding to lower case and replace '_' with '-' in order to

0 commit comments

Comments
 (0)