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

Skip to content

Commit a2983c6

Browse files
committed
Merged revisions 84394 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r84394 | antoine.pitrou | 2010-09-01 17:10:12 +0200 (mer., 01 sept. 2010) | 4 lines Issue #7415: PyUnicode_FromEncodedObject() now uses the new buffer API properly. Patch by Stefan Behnel. ........
1 parent b83df8f commit a2983c6

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
@@ -58,6 +58,7 @@ David Beazley
5858
Robin Becker
5959
Neal Becker
6060
Bill Bedford
61+
Stefan Behnel
6162
Reimer Behrends
6263
Ben Bell
6364
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.1.3?
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
- Restore GIL in nis_cat in case of error.
1619

1720
- Issue #9712: Fix tokenize on identifiers that start with non-ascii names.

Objects/unicodeobject.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,53 +1162,52 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,
11621162
const char *encoding,
11631163
const char *errors)
11641164
{
1165-
const char *s = NULL;
1166-
Py_ssize_t len;
1165+
Py_buffer buffer;
11671166
PyObject *v;
11681167

11691168
if (obj == NULL) {
11701169
PyErr_BadInternalCall();
11711170
return NULL;
11721171
}
11731172

1173+
/* Decoding bytes objects is the most common case and should be fast */
1174+
if (PyBytes_Check(obj)) {
1175+
if (PyBytes_GET_SIZE(obj) == 0) {
1176+
Py_INCREF(unicode_empty);
1177+
v = (PyObject *) unicode_empty;
1178+
}
1179+
else {
1180+
v = PyUnicode_Decode(
1181+
PyBytes_AS_STRING(obj), PyBytes_GET_SIZE(obj),
1182+
encoding, errors);
1183+
}
1184+
return v;
1185+
}
1186+
11741187
if (PyUnicode_Check(obj)) {
11751188
PyErr_SetString(PyExc_TypeError,
11761189
"decoding str is not supported");
11771190
return NULL;
11781191
}
11791192

1180-
/* Coerce object */
1181-
if (PyBytes_Check(obj)) {
1182-
s = PyBytes_AS_STRING(obj);
1183-
len = PyBytes_GET_SIZE(obj);
1184-
}
1185-
else if (PyByteArray_Check(obj)) {
1186-
s = PyByteArray_AS_STRING(obj);
1187-
len = PyByteArray_GET_SIZE(obj);
1188-
}
1189-
else if (PyObject_AsCharBuffer(obj, &s, &len)) {
1190-
/* Overwrite the error message with something more useful in
1191-
case of a TypeError. */
1192-
if (PyErr_ExceptionMatches(PyExc_TypeError))
1193-
PyErr_Format(PyExc_TypeError,
1194-
"coercing to str: need string or buffer, "
1195-
"%.80s found",
1196-
Py_TYPE(obj)->tp_name);
1197-
goto onError;
1193+
/* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
1194+
if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {
1195+
PyErr_Format(PyExc_TypeError,
1196+
"coercing to str: need bytes, bytearray "
1197+
"or buffer-like object, %.80s found",
1198+
Py_TYPE(obj)->tp_name);
1199+
return NULL;
11981200
}
11991201

1200-
/* Convert to Unicode */
1201-
if (len == 0) {
1202+
if (buffer.len == 0) {
12021203
Py_INCREF(unicode_empty);
1203-
v = (PyObject *)unicode_empty;
1204+
v = (PyObject *) unicode_empty;
12041205
}
12051206
else
1206-
v = PyUnicode_Decode(s, len, encoding, errors);
1207+
v = PyUnicode_Decode((char*) buffer.buf, buffer.len, encoding, errors);
12071208

1209+
PyBuffer_Release(&buffer);
12081210
return v;
1209-
1210-
onError:
1211-
return NULL;
12121211
}
12131212

12141213
PyObject *PyUnicode_Decode(const char *s,

0 commit comments

Comments
 (0)