Kaydet (Commit) 5a49ade7 authored tarafından Tim Peters's avatar Tim Peters

More on SF bug [#460020] bug or feature: unicode() and subclasses.

Repaired str(i) to return a genuine string when i is an instance of a str
subclass.  New PyString_CheckExact() macro.
üst 8ff70a96
...@@ -52,6 +52,7 @@ typedef struct { ...@@ -52,6 +52,7 @@ typedef struct {
extern DL_IMPORT(PyTypeObject) PyString_Type; extern DL_IMPORT(PyTypeObject) PyString_Type;
#define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type) #define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type)
#define PyString_CheckExact(op) ((op)->ob_type == &PyString_Type)
extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int); extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int);
extern DL_IMPORT(PyObject *) PyString_FromString(const char *); extern DL_IMPORT(PyObject *) PyString_FromString(const char *);
......
...@@ -1440,7 +1440,7 @@ def inherits(): ...@@ -1440,7 +1440,7 @@ def inherits():
verify(u == s) verify(u == s)
s = madstring("12345") s = madstring("12345")
#XXX verify(str(s) == "12345") #XXX verify(str(s) == "12345")
#XXX verify(str(s).__class__ is str) verify(str(s).__class__ is str)
class madunicode(unicode): class madunicode(unicode):
_rev = None _rev = None
......
...@@ -250,10 +250,16 @@ PyObject_Str(PyObject *v) ...@@ -250,10 +250,16 @@ PyObject_Str(PyObject *v)
if (v == NULL) if (v == NULL)
return PyString_FromString("<NULL>"); return PyString_FromString("<NULL>");
if (PyString_Check(v)) { if (PyString_CheckExact(v)) {
Py_INCREF(v); Py_INCREF(v);
return v; return v;
} }
if (PyString_Check(v)) {
/* For a string subtype that's not a string, return a true
string with the same string data. */
PyStringObject *s = (PyStringObject *)v;
return PyString_FromStringAndSize(s->ob_sval, s->ob_size);
}
if (v->ob_type->tp_str == NULL) if (v->ob_type->tp_str == NULL)
return PyObject_Repr(v); return PyObject_Repr(v);
......
...@@ -2711,7 +2711,7 @@ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -2711,7 +2711,7 @@ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
tmp = string_new(&PyString_Type, args, kwds); tmp = string_new(&PyString_Type, args, kwds);
if (tmp == NULL) if (tmp == NULL)
return NULL; return NULL;
assert(PyString_Check(tmp)); assert(PyString_CheckExact(tmp));
new = type->tp_alloc(type, n = PyString_GET_SIZE(tmp)); new = type->tp_alloc(type, n = PyString_GET_SIZE(tmp));
if (new != NULL) if (new != NULL)
memcpy(PyString_AS_STRING(new), PyString_AS_STRING(tmp), n+1); memcpy(PyString_AS_STRING(new), PyString_AS_STRING(tmp), n+1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment