Kaydet (Commit) fa004ad3 authored tarafından Ka-Ping Yee's avatar Ka-Ping Yee

Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.

Switch from octal escapes to hex escapes for other nonprintable characters.
üst 726b78ec
...@@ -41,5 +41,5 @@ Example: ...@@ -41,5 +41,5 @@ Example:
\begin{verbatim} \begin{verbatim}
>>> import linecache >>> import linecache
>>> linecache.getline('/etc/passwd', 4) >>> linecache.getline('/etc/passwd', 4)
'sys:x:3:3:sys:/dev:/bin/sh\012' 'sys:x:3:3:sys:/dev:/bin/sh\n'
\end{verbatim} \end{verbatim}
...@@ -265,7 +265,7 @@ Example: ...@@ -265,7 +265,7 @@ Example:
>>> import locale >>> import locale
>>> loc = locale.setlocale(locale.LC_ALL) # get current locale >>> loc = locale.setlocale(locale.LC_ALL) # get current locale
>>> locale.setlocale(locale.LC_ALL, 'de') # use German locale >>> locale.setlocale(locale.LC_ALL, 'de') # use German locale
>>> locale.strcoll('f\344n', 'foo') # compare a string containing an umlaut >>> locale.strcoll('f\xe4n', 'foo') # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale >>> locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale >>> locale.setlocale(locale.LC_ALL, 'C') # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale >>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale
......
...@@ -25,14 +25,14 @@ the spammish repetition'}: ...@@ -25,14 +25,14 @@ the spammish repetition'}:
>>> m.update("Nobody inspects") >>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition") >>> m.update(" the spammish repetition")
>>> m.digest() >>> m.digest()
'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351' '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
\end{verbatim} \end{verbatim}
More condensed: More condensed:
\begin{verbatim} \begin{verbatim}
>>> md5.new("Nobody inspects the spammish repetition").digest() >>> md5.new("Nobody inspects the spammish repetition").digest()
'\273d\234\203\335\036\245\311\331\336\311\241\215\360\377\351' '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
\end{verbatim} \end{verbatim}
\begin{funcdesc}{new}{\optional{arg}} \begin{funcdesc}{new}{\optional{arg}}
......
...@@ -432,7 +432,7 @@ buried deep in nested tuples. ...@@ -432,7 +432,7 @@ buried deep in nested tuples.
(297, (297,
(298, (298,
(299, (299,
(300, (3, '"""Some documentation.\012"""'))))))))))))))))), (300, (3, '"""Some documentation.\n"""'))))))))))))))))),
(4, ''))), (4, ''))),
(4, ''), (4, ''),
(0, '')) (0, ''))
...@@ -537,7 +537,7 @@ module docstring from the parse tree created previously is easy: ...@@ -537,7 +537,7 @@ module docstring from the parse tree created previously is easy:
>>> found >>> found
1 1
>>> vars >>> vars
{'docstring': '"""Some documentation.\012"""'} {'docstring': '"""Some documentation.\n"""'}
\end{verbatim} \end{verbatim}
Once specific data can be extracted from a location where it is Once specific data can be extracted from a location where it is
......
...@@ -272,11 +272,11 @@ Start element: parent {'id': 'top'} ...@@ -272,11 +272,11 @@ Start element: parent {'id': 'top'}
Start element: child1 {'name': 'paul'} Start element: child1 {'name': 'paul'}
Character data: 'Text goes here' Character data: 'Text goes here'
End element: child1 End element: child1
Character data: '\012' Character data: '\n'
Start element: child2 {'name': 'fred'} Start element: child2 {'name': 'fred'}
Character data: 'More text' Character data: 'More text'
End element: child2 End element: child2
Character data: '\012' Character data: '\n'
End element: parent End element: parent
\end{verbatim} \end{verbatim}
......
...@@ -68,17 +68,17 @@ An example usage: ...@@ -68,17 +68,17 @@ An example usage:
>>> import rotor >>> import rotor
>>> rt = rotor.newrotor('key', 12) >>> rt = rotor.newrotor('key', 12)
>>> rt.encrypt('bar') >>> rt.encrypt('bar')
'\2534\363' '\xab4\xf3'
>>> rt.encryptmore('bar') >>> rt.encryptmore('bar')
'\357\375$' '\xef\xfd$'
>>> rt.encrypt('bar') >>> rt.encrypt('bar')
'\2534\363' '\xab4\xf3'
>>> rt.decrypt('\2534\363') >>> rt.decrypt('\xab4\xf3')
'bar' 'bar'
>>> rt.decryptmore('\357\375$') >>> rt.decryptmore('\xef\xfd$')
'bar' 'bar'
>>> rt.decrypt('\357\375$') >>> rt.decrypt('\xef\xfd$')
'l(\315' 'l(\xcd'
>>> del rt >>> del rt
\end{verbatim} \end{verbatim}
......
...@@ -168,8 +168,8 @@ big-endian machine): ...@@ -168,8 +168,8 @@ big-endian machine):
\begin{verbatim} \begin{verbatim}
>>> from struct import * >>> from struct import *
>>> pack('hhl', 1, 2, 3) >>> pack('hhl', 1, 2, 3)
'\000\001\000\002\000\000\000\003' '\x00\x01\x00\x02\x00\x00\x00\x03'
>>> unpack('hhl', '\000\001\000\002\000\000\000\003') >>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3) (1, 2, 3)
>>> calcsize('hhl') >>> calcsize('hhl')
8 8
......
...@@ -801,24 +801,24 @@ Apart from these standard encodings, Python provides a whole set of ...@@ -801,24 +801,24 @@ Apart from these standard encodings, Python provides a whole set of
other ways of creating Unicode strings on the basis of a known other ways of creating Unicode strings on the basis of a known
encoding. encoding.
The builtin \function{unicode()}\bifuncindex{unicode} provides access The built-in function \function{unicode()}\bifuncindex{unicode} provides access
to all registered Unicode codecs (COders and DECoders). Some of the to all registered Unicode codecs (COders and DECoders). Some of the
more well known encodings which these codecs can convert are more well known encodings which these codecs can convert are
\emph{Latin-1}, \emph{ASCII}, \emph{UTF-8} and \emph{UTF-16}. The latter two \emph{Latin-1}, \emph{ASCII}, \emph{UTF-8} and \emph{UTF-16}. The latter two
are variable length encodings which permit to store Unicode characters are variable-length encodings which store Unicode characters
in 8 or 16 bits. Python uses UTF-8 as default encoding. This becomes in blocks of 8 or 16 bits. To print a Unicode string or write it to a file,
noticeable when printing Unicode strings or writing them to files. you must convert it to a string with the \method{encode()} method.
\begin{verbatim} \begin{verbatim}
>>> u"äöü" >>> u"äöü"
u'\344\366\374' u'\344\366\374'
>>> str(u"äöü") >>> u"äöü".encode('UTF-8')
'\303\244\303\266\303\274' '\303\244\303\266\303\274'
\end{verbatim} \end{verbatim}
If you have data in a specific encoding and want to produce a If you have data in a specific encoding and want to produce a
corresponding Unicode string from it, you can use the corresponding Unicode string from it, you can use the
\function{unicode()} builtin with the encoding name as second \function{unicode()} function with the encoding name as second
argument. argument.
\begin{verbatim} \begin{verbatim}
...@@ -826,14 +826,6 @@ argument. ...@@ -826,14 +826,6 @@ argument.
u'\344\366\374' u'\344\366\374'
\end{verbatim} \end{verbatim}
To convert the Unicode string back into a string using the original
encoding, the objects provide an \method{encode()} method.
\begin{verbatim}
>>> u"äöü".encode('UTF-8')
'\303\244\303\266\303\274'
\end{verbatim}
\subsection{Lists \label{lists}} \subsection{Lists \label{lists}}
......
...@@ -24,18 +24,18 @@ Value interpolation too deeply recursive: ...@@ -24,18 +24,18 @@ Value interpolation too deeply recursive:
Testing for parsing errors... Testing for parsing errors...
Caught expected exception: File contains parsing errors: <???> Caught expected exception: File contains parsing errors: <???>
[line 2]: ' extra-spaces: splat\012' [line 2]: ' extra-spaces: splat\n'
Caught expected exception: File contains parsing errors: <???> Caught expected exception: File contains parsing errors: <???>
[line 2]: ' extra-spaces= splat\012' [line 2]: ' extra-spaces= splat\n'
Caught expected exception: File contains parsing errors: <???> Caught expected exception: File contains parsing errors: <???>
[line 2]: 'option-without-value\012' [line 2]: 'option-without-value\n'
Caught expected exception: File contains parsing errors: <???> Caught expected exception: File contains parsing errors: <???>
[line 2]: ':value-without-option-name\012' [line 2]: ':value-without-option-name\n'
Caught expected exception: File contains parsing errors: <???> Caught expected exception: File contains parsing errors: <???>
[line 2]: '=value-without-option-name\012' [line 2]: '=value-without-option-name\n'
Caught expected exception: File contains no section headers. Caught expected exception: File contains no section headers.
file: <???>, line: 1 file: <???>, line: 1
'No Section!\012' 'No Section!\n'
Testing query interface... Testing query interface...
[] []
......
...@@ -6,9 +6,9 @@ Set-Cookie: chips=ahoy; ...@@ -6,9 +6,9 @@ Set-Cookie: chips=ahoy;
Set-Cookie: vienna=finger; Set-Cookie: vienna=finger;
chips 'ahoy' 'ahoy' chips 'ahoy' 'ahoy'
Set-Cookie: chips=ahoy; Set-Cookie: chips=ahoy;
<SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\012;'> <SimpleCookie: keebler='E=mc2; L="Loves"; fudge=\n;'>
Set-Cookie: keebler="E=mc2; L=\"Loves\"; fudge=\012;"; Set-Cookie: keebler="E=mc2; L=\"Loves\"; fudge=\012;";
keebler 'E=mc2; L="Loves"; fudge=\012;' 'E=mc2; L="Loves"; fudge=\012;' keebler 'E=mc2; L="Loves"; fudge=\n;' 'E=mc2; L="Loves"; fudge=\n;'
Set-Cookie: keebler="E=mc2; L=\"Loves\"; fudge=\012;"; Set-Cookie: keebler="E=mc2; L=\"Loves\"; fudge=\012;";
Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme; Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme;
......
This diff is collapsed.
...@@ -335,8 +335,14 @@ string_print(PyStringObject *op, FILE *fp, int flags) ...@@ -335,8 +335,14 @@ string_print(PyStringObject *op, FILE *fp, int flags)
c = op->ob_sval[i]; c = op->ob_sval[i];
if (c == quote || c == '\\') if (c == quote || c == '\\')
fprintf(fp, "\\%c", c); fprintf(fp, "\\%c", c);
else if (c < ' ' || c >= 0177) else if (c == '\t')
fprintf(fp, "\\%03o", c & 0377); fprintf(fp, "\\t");
else if (c == '\n')
fprintf(fp, "\\n");
else if (c == '\r')
fprintf(fp, "\\r");
else if (c < ' ' || c >= 0x7f)
fprintf(fp, "\\x%02x", c & 0xff);
else else
fputc(c, fp); fputc(c, fp);
} }
...@@ -374,10 +380,15 @@ string_repr(register PyStringObject *op) ...@@ -374,10 +380,15 @@ string_repr(register PyStringObject *op)
c = op->ob_sval[i]; c = op->ob_sval[i];
if (c == quote || c == '\\') if (c == quote || c == '\\')
*p++ = '\\', *p++ = c; *p++ = '\\', *p++ = c;
else if (c < ' ' || c >= 0177) { else if (c == '\t')
sprintf(p, "\\%03o", c & 0377); *p++ = '\\', *p++ = 't';
while (*p != '\0') else if (c == '\n')
p++; *p++ = '\\', *p++ = 'n';
else if (c == '\r')
*p++ = '\\', *p++ = 'r';
else if (c < ' ' || c >= 0x7f) {
sprintf(p, "\\x%02x", c & 0xff);
p += 4;
} }
else else
*p++ = c; *p++ = c;
......
...@@ -1343,7 +1343,7 @@ PyObject *unicodeescape_string(const Py_UNICODE *s, ...@@ -1343,7 +1343,7 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
char *p; char *p;
char *q; char *q;
static const char *hexdigit = "0123456789ABCDEF"; static const char *hexdigit = "0123456789abcdef";
repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1); repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1);
if (repr == NULL) if (repr == NULL)
...@@ -1372,12 +1372,25 @@ PyObject *unicodeescape_string(const Py_UNICODE *s, ...@@ -1372,12 +1372,25 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
*p++ = hexdigit[(ch >> 4) & 0xf]; *p++ = hexdigit[(ch >> 4) & 0xf];
*p++ = hexdigit[ch & 15]; *p++ = hexdigit[ch & 15];
} }
/* Map non-printable US ASCII to '\ooo' */ /* Map special whitespace to '\t', \n', '\r' */
else if (ch == '\t') {
*p++ = '\\';
*p++ = 't';
}
else if (ch == '\n') {
*p++ = '\\';
*p++ = 'n';
}
else if (ch == '\r') {
*p++ = '\\';
*p++ = 'r';
}
/* Map non-printable US ASCII to '\xhh' */
else if (ch < ' ' || ch >= 128) { else if (ch < ' ' || ch >= 128) {
*p++ = '\\'; *p++ = '\\';
*p++ = hexdigit[(ch >> 6) & 7]; *p++ = 'x';
*p++ = hexdigit[(ch >> 3) & 7]; *p++ = hexdigit[(ch >> 4) & 0xf];
*p++ = hexdigit[ch & 7]; *p++ = hexdigit[ch & 15];
} }
/* Copy everything else as-is */ /* Copy everything else as-is */
else else
...@@ -1498,7 +1511,7 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, ...@@ -1498,7 +1511,7 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
char *p; char *p;
char *q; char *q;
static const char *hexdigit = "0123456789ABCDEF"; static const char *hexdigit = "0123456789abcdef";
repr = PyString_FromStringAndSize(NULL, 6 * size); repr = PyString_FromStringAndSize(NULL, 6 * size);
if (repr == NULL) if (repr == NULL)
......
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