diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-10-16 20:18:24 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-10-16 20:18:24 (GMT) |
commit | c993315b188b08b6d78248522f6d0ed31d52f939 (patch) | |
tree | 3fd0205ab57b86f588c32a378186c3569ad1d9ed /Objects/stringobject.c | |
parent | dfefc06fe01fdab0171a368c642ecaf352540627 (diff) | |
download | cpython-c993315b188b08b6d78248522f6d0ed31d52f939.zip cpython-c993315b188b08b6d78248522f6d0ed31d52f939.tar.gz cpython-c993315b188b08b6d78248522f6d0ed31d52f939.tar.bz2 |
SF bug [#468061] __str__ ignored in str subclass.
object.c, PyObject_Str: Don't try to optimize anything except exact
string objects here; in particular, let str subclasses go thru tp_str,
same as non-str objects. This allows overrides of tp_str to take
effect.
stringobject.c:
+ string_print (str's tp_print): If the argument isn't an exact string
object, get one from PyObject_Str.
+ string_str (str's tp_str): Make a genuine-string copy of the object if
it's of a proper str subclass type. str() applied to a str subclass
that doesn't override __str__ ends up here.
test_descr.py: New str_of_str_subclass() test.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index aea31ac..0a3155e 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -566,7 +566,18 @@ string_print(PyStringObject *op, FILE *fp, int flags) int i; char c; int quote; + /* XXX Ought to check for interrupts when writing long strings */ + if (! PyString_CheckExact(op)) { + int ret; + /* A str subclass may have its own __str__ method. */ + op = (PyStringObject *) PyObject_Str((PyObject *)op); + if (op == NULL) + return -1; + ret = string_print(op, fp, flags); + Py_DECREF(op); + return ret; + } if (flags & Py_PRINT_RAW) { fwrite(op->ob_sval, 1, (int) op->ob_size, fp); return 0; @@ -651,8 +662,16 @@ string_repr(register PyStringObject *op) static PyObject * string_str(PyObject *s) { - Py_INCREF(s); - return s; + assert(PyString_Check(s)); + if (PyString_CheckExact(s)) { + Py_INCREF(s); + return s; + } + else { + /* Subtype -- return genuine string with the same value. */ + PyStringObject *t = (PyStringObject *) s; + return PyString_FromStringAndSize(t->ob_sval, t->ob_size); + } } static int |