mirror of
https://github.com/python/cpython.git
synced 2025-12-09 18:48:05 +00:00
make bytes(o) respect __bytes__ #2415
This adds two new C-API functions: PyObject_Bytes and PyBytes_FromObject. Reviewer: Barry
This commit is contained in:
parent
a786b026c9
commit
c15a07333e
8 changed files with 83 additions and 2 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue