mirror of
https://github.com/python/cpython.git
synced 2025-10-22 06:32:43 +00:00
SF bug 480215: softspace confused in nested print
This fixes the symptom, but PRINT_ITEM has no way to know what (if anything) PyFile_WriteObject() writes unless the object being printed is a string. When the object isn't a string, this fix retains the guess that softspace should be set after PyFile_WriteObject(). We might want to say that it's the job of filelike-object write methods to leave the file's softspace in the correct state. That would probably be better -- but everyone relies on PRINT_ITEM to guess for them now.
This commit is contained in:
parent
f3f87f743e
commit
8e5fd53be0
2 changed files with 26 additions and 10 deletions
14
Lib/test/test_softspace.py
Normal file
14
Lib/test/test_softspace.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import test_support
|
||||||
|
import StringIO
|
||||||
|
|
||||||
|
# SF bug 480215: softspace confused in nested print
|
||||||
|
f = StringIO.StringIO()
|
||||||
|
class C:
|
||||||
|
def __str__(self):
|
||||||
|
print >> f, 'a'
|
||||||
|
return 'b'
|
||||||
|
|
||||||
|
print >> f, C(), 'c ', 'd\t', 'e'
|
||||||
|
print >> f, 'f', 'g'
|
||||||
|
# In 2.2 & earlier, this printed ' a\nbc d\te\nf g\n'
|
||||||
|
test_support.vereq(f.getvalue(), 'a\nb c d\te\nf g\n')
|
|
@ -1397,7 +1397,7 @@ eval_frame(PyFrameObject *f)
|
||||||
err = -1;
|
err = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (w != NULL && PyFile_SoftSpace(w, 1))
|
if (w != NULL && PyFile_SoftSpace(w, 0))
|
||||||
err = PyFile_WriteString(" ", w);
|
err = PyFile_WriteString(" ", w);
|
||||||
if (err == 0)
|
if (err == 0)
|
||||||
err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
|
err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
|
||||||
|
@ -1406,20 +1406,22 @@ eval_frame(PyFrameObject *f)
|
||||||
if (PyString_Check(v)) {
|
if (PyString_Check(v)) {
|
||||||
char *s = PyString_AS_STRING(v);
|
char *s = PyString_AS_STRING(v);
|
||||||
int len = PyString_GET_SIZE(v);
|
int len = PyString_GET_SIZE(v);
|
||||||
if (len > 0 &&
|
if (len == 0 ||
|
||||||
isspace(Py_CHARMASK(s[len-1])) &&
|
!isspace(Py_CHARMASK(s[len-1])) ||
|
||||||
s[len-1] != ' ')
|
s[len-1] == ' ')
|
||||||
PyFile_SoftSpace(w, 0);
|
PyFile_SoftSpace(w, 1);
|
||||||
}
|
}
|
||||||
#ifdef Py_USING_UNICODE
|
#ifdef Py_USING_UNICODE
|
||||||
else if (PyUnicode_Check(v)) {
|
else if (PyUnicode_Check(v)) {
|
||||||
Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
|
Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
|
||||||
int len = PyUnicode_GET_SIZE(v);
|
int len = PyUnicode_GET_SIZE(v);
|
||||||
if (len > 0 &&
|
if (len == 0 ||
|
||||||
Py_UNICODE_ISSPACE(s[len-1]) &&
|
!Py_UNICODE_ISSPACE(s[len-1]) ||
|
||||||
s[len-1] != ' ')
|
s[len-1] == ' ')
|
||||||
PyFile_SoftSpace(w, 0);
|
PyFile_SoftSpace(w, 1);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
PyFile_SoftSpace(w, 1);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
Py_DECREF(v);
|
Py_DECREF(v);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue