summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-12-20 16:37:21 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2015-12-20 16:37:21 +0200
commit3182db356c8e6106b7bdab907739c741c508037a (patch)
tree74503a2d8698fac210897f8a76a796497d0beb91 /Objects
parent5decf0bf325a7a2d85910e2580c10aa03c907efc (diff)
parent5aac3ed79999f6948d11f979ab3e42b9b60e9083 (diff)
downloadcpython-git-3182db356c8e6106b7bdab907739c741c508037a.tar.gz
Issue #25766: Special method __bytes__() now works in str subclasses.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/bytesobject.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index cbdac86aec..3cc7ce34d0 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -3320,11 +3320,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return PyBytes_FromStringAndSize(NULL, 0);
}
- if (PyUnicode_Check(x)) {
+ if (encoding != NULL) {
/* Encode via the codec registry */
- if (encoding == NULL) {
+ if (!PyUnicode_Check(x)) {
PyErr_SetString(PyExc_TypeError,
- "string argument without an encoding");
+ "encoding without a string argument");
return NULL;
}
new = PyUnicode_AsEncodedString(x, encoding, errors);
@@ -3334,10 +3334,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return new;
}
- /* If it's not unicode, there can't be encoding or errors */
- if (encoding != NULL || errors != NULL) {
+ if (errors != NULL) {
PyErr_SetString(PyExc_TypeError,
- "encoding or errors without a string argument");
+ PyUnicode_Check(x) ?
+ "string argument without an encoding" :
+ "errors without a string argument");
return NULL;
}
@@ -3362,6 +3363,11 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
else if (PyErr_Occurred())
return NULL;
+ if (PyUnicode_Check(x)) {
+ PyErr_SetString(PyExc_TypeError,
+ "string argument without an encoding");
+ return NULL;
+ }
/* Is it an integer? */
size = PyNumber_AsSsize_t(x, PyExc_OverflowError);
if (size == -1 && PyErr_Occurred()) {