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:
\begin{verbatim}
>>> import linecache
>>> 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}
......@@ -265,7 +265,7 @@ Example:
>>> import locale
>>> loc = locale.setlocale(locale.LC_ALL) # get current 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, 'C') # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc) # restore saved locale
......
......@@ -25,14 +25,14 @@ the spammish repetition'}:
>>> m.update("Nobody inspects")
>>> m.update(" the spammish repetition")
>>> 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}
More condensed:
\begin{verbatim}
>>> 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}
\begin{funcdesc}{new}{\optional{arg}}
......
......@@ -432,7 +432,7 @@ buried deep in nested tuples.
(297,
(298,
(299,
(300, (3, '"""Some documentation.\012"""'))))))))))))))))),
(300, (3, '"""Some documentation.\n"""'))))))))))))))))),
(4, ''))),
(4, ''),
(0, ''))
......@@ -537,7 +537,7 @@ module docstring from the parse tree created previously is easy:
>>> found
1
>>> vars
{'docstring': '"""Some documentation.\012"""'}
{'docstring': '"""Some documentation.\n"""'}
\end{verbatim}
Once specific data can be extracted from a location where it is
......
......@@ -272,11 +272,11 @@ Start element: parent {'id': 'top'}
Start element: child1 {'name': 'paul'}
Character data: 'Text goes here'
End element: child1
Character data: '\012'
Character data: '\n'
Start element: child2 {'name': 'fred'}
Character data: 'More text'
End element: child2
Character data: '\012'
Character data: '\n'
End element: parent
\end{verbatim}
......
......@@ -68,17 +68,17 @@ An example usage:
>>> import rotor
>>> rt = rotor.newrotor('key', 12)
>>> rt.encrypt('bar')
'\2534\363'
'\xab4\xf3'
>>> rt.encryptmore('bar')
'\357\375$'
'\xef\xfd$'
>>> rt.encrypt('bar')
'\2534\363'
>>> rt.decrypt('\2534\363')
'\xab4\xf3'
>>> rt.decrypt('\xab4\xf3')
'bar'
>>> rt.decryptmore('\357\375$')
>>> rt.decryptmore('\xef\xfd$')
'bar'
>>> rt.decrypt('\357\375$')
'l(\315'
>>> rt.decrypt('\xef\xfd$')
'l(\xcd'
>>> del rt
\end{verbatim}
......
......@@ -168,8 +168,8 @@ big-endian machine):
\begin{verbatim}
>>> from struct import *
>>> pack('hhl', 1, 2, 3)
'\000\001\000\002\000\000\000\003'
>>> unpack('hhl', '\000\001\000\002\000\000\000\003')
'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>> calcsize('hhl')
8
......
......@@ -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
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
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
are variable length encodings which permit to store Unicode characters
in 8 or 16 bits. Python uses UTF-8 as default encoding. This becomes
noticeable when printing Unicode strings or writing them to files.
are variable-length encodings which store Unicode characters
in blocks of 8 or 16 bits. To print a Unicode string or write it to a file,
you must convert it to a string with the \method{encode()} method.
\begin{verbatim}
>>> u"äöü"
u'\344\366\374'
>>> str(u"äöü")
>>> u"äöü".encode('UTF-8')
'\303\244\303\266\303\274'
\end{verbatim}
If you have data in a specific encoding and want to produce a
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.
\begin{verbatim}
......@@ -826,14 +826,6 @@ argument.
u'\344\366\374'
\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}}
......
......@@ -24,18 +24,18 @@ Value interpolation too deeply recursive:
Testing for 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: <???>
[line 2]: ' extra-spaces= splat\012'
[line 2]: ' extra-spaces= splat\n'
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: <???>
[line 2]: ':value-without-option-name\012'
[line 2]: ':value-without-option-name\n'
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.
file: <???>, line: 1
'No Section!\012'
'No Section!\n'
Testing query interface...
[]
......
......@@ -6,9 +6,9 @@ Set-Cookie: chips=ahoy;
Set-Cookie: vienna=finger;
chips 'ahoy' '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;";
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: Customer="WILE_E_COYOTE"; Path=/acme;
......
This diff is collapsed.
......@@ -335,8 +335,14 @@ string_print(PyStringObject *op, FILE *fp, int flags)
c = op->ob_sval[i];
if (c == quote || c == '\\')
fprintf(fp, "\\%c", c);
else if (c < ' ' || c >= 0177)
fprintf(fp, "\\%03o", c & 0377);
else if (c == '\t')
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
fputc(c, fp);
}
......@@ -374,10 +380,15 @@ string_repr(register PyStringObject *op)
c = op->ob_sval[i];
if (c == quote || c == '\\')
*p++ = '\\', *p++ = c;
else if (c < ' ' || c >= 0177) {
sprintf(p, "\\%03o", c & 0377);
while (*p != '\0')
p++;
else if (c == '\t')
*p++ = '\\', *p++ = 't';
else if (c == '\n')
*p++ = '\\', *p++ = 'n';
else if (c == '\r')
*p++ = '\\', *p++ = 'r';
else if (c < ' ' || c >= 0x7f) {
sprintf(p, "\\x%02x", c & 0xff);
p += 4;
}
else
*p++ = c;
......
......@@ -1343,7 +1343,7 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
char *p;
char *q;
static const char *hexdigit = "0123456789ABCDEF";
static const char *hexdigit = "0123456789abcdef";
repr = PyString_FromStringAndSize(NULL, 2 + 6*size + 1);
if (repr == NULL)
......@@ -1372,12 +1372,25 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
*p++ = hexdigit[(ch >> 4) & 0xf];
*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) {
*p++ = '\\';
*p++ = hexdigit[(ch >> 6) & 7];
*p++ = hexdigit[(ch >> 3) & 7];
*p++ = hexdigit[ch & 7];
*p++ = 'x';
*p++ = hexdigit[(ch >> 4) & 0xf];
*p++ = hexdigit[ch & 15];
}
/* Copy everything else as-is */
else
......@@ -1498,7 +1511,7 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
char *p;
char *q;
static const char *hexdigit = "0123456789ABCDEF";
static const char *hexdigit = "0123456789abcdef";
repr = PyString_FromStringAndSize(NULL, 6 * size);
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