diff options
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/bytesobject.c | 11 | ||||
-rw-r--r-- | Objects/object.c | 39 |
2 files changed, 48 insertions, 2 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index d59e79a..3bda6d9 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -2882,11 +2882,10 @@ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static PyObject * string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { - PyObject *x = NULL, *it; + PyObject *x = NULL; const char *encoding = NULL; const char *errors = NULL; PyObject *new = NULL; - Py_ssize_t i, size; static char *kwlist[] = {"source", "encoding", "errors", 0}; if (type != &PyBytes_Type) @@ -2924,6 +2923,14 @@ string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) "encoding or errors without a string argument"); return NULL; } + return PyObject_Bytes(x); +} + +PyObject * +PyBytes_FromObject(PyObject *x) +{ + PyObject *new, *it; + Py_ssize_t i, size; /* Is it an int? */ size = PyNumber_AsSsize_t(x, PyExc_ValueError); diff --git a/Objects/object.c b/Objects/object.c index 79f8288..206bb88 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -453,6 +453,45 @@ PyObject_ASCII(PyObject *v) return res; } +PyObject * +PyObject_Bytes(PyObject *v) +{ + PyObject *bytesmeth, *result, *func; + static PyObject *bytesstring = NULL; + + if (bytesstring == NULL) { + bytesstring = PyUnicode_InternFromString("__bytes__"); + if (bytesstring == NULL) + return NULL; + } + + if (v == NULL) + return PyBytes_FromString("<NULL>"); + + if (PyBytes_CheckExact(v)) { + Py_INCREF(v); + return v; + } + + /* Doesn't create a reference */ + func = _PyType_Lookup(Py_TYPE(v), bytesstring); + if (func != NULL) { + result = PyObject_CallFunctionObjArgs(func, v, NULL); + if (result == NULL) + return NULL; + if (!PyBytes_Check(result)) { + PyErr_Format(PyExc_TypeError, + "__bytes__ returned non-bytes (type %.200s)", + Py_TYPE(result)->tp_name); + Py_DECREF(result); + return NULL; + } + return result; + } + PyErr_Clear(); + return PyBytes_FromObject(v); +} + /* The new comparison philosophy is: we completely separate three-way comparison from rich comparison. That is, PyObject_Compare() and PyObject_Cmp() *just* use the tp_compare slot. And PyObject_RichCompare() |