[pypy-commit] pypy default: 'string-escape' codec always escape single quotes
amauryfa
noreply at buildbot.pypy.org
Mon Aug 29 22:51:55 CEST 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch:
Changeset: r46906:47a43db15b5b
Date: 2011-08-23 22:52 +0200
http://bitbucket.org/pypy/pypy/changeset/47a43db15b5b/
Log: 'string-escape' codec always escape single quotes
diff --git a/pypy/module/_codecs/interp_codecs.py b/pypy/module/_codecs/interp_codecs.py
--- a/pypy/module/_codecs/interp_codecs.py
+++ b/pypy/module/_codecs/interp_codecs.py
@@ -687,11 +687,15 @@
# support for the "string escape" codec
# This is a bytes-to bytes transformation
- at unwrap_spec(errors='str_or_None')
-def escape_encode(space, w_string, errors='strict'):
- w_repr = space.repr(w_string)
- w_result = space.getslice(w_repr, space.wrap(1), space.wrap(-1))
- return space.newtuple([w_result, space.len(w_string)])
+ at unwrap_spec(data=str, errors='str_or_None')
+def escape_encode(space, data, errors='strict'):
+ from pypy.objspace.std.stringobject import string_escape_encode
+ result = string_escape_encode(data, quote="'")
+ start = 1
+ end = len(result) - 1
+ assert end >= 0
+ w_result = space.wrap(result[start:end])
+ return space.newtuple([w_result, space.wrap(len(data))])
@unwrap_spec(data=str, errors='str_or_None')
def escape_decode(space, data, errors='strict'):
diff --git a/pypy/module/_codecs/test/test_codecs.py b/pypy/module/_codecs/test/test_codecs.py
--- a/pypy/module/_codecs/test/test_codecs.py
+++ b/pypy/module/_codecs/test/test_codecs.py
@@ -102,7 +102,6 @@
def test_indexerror(self):
test = "\\" # trailing backslash
-
raises (ValueError, test.decode,'string-escape')
def test_charmap_decode(self):
@@ -292,6 +291,10 @@
assert '\\0f'.decode('string_escape') == chr(0) + 'f'
assert '\\08'.decode('string_escape') == chr(0) + '8'
+ def test_escape_encode(self):
+ assert '"'.encode('string_escape') == '"'
+ assert "'".encode('string_escape') == "\\'"
+
def test_decode_utf8_different_case(self):
constant = u"a"
assert constant.encode("utf-8") == constant.encode("UTF-8")
diff --git a/pypy/objspace/std/stringobject.py b/pypy/objspace/std/stringobject.py
--- a/pypy/objspace/std/stringobject.py
+++ b/pypy/objspace/std/stringobject.py
@@ -913,12 +913,16 @@
def repr__String(space, w_str):
s = w_str._value
- buf = StringBuilder(len(s) + 2)
-
quote = "'"
if quote in s and '"' not in s:
quote = '"'
+ return space.wrap(string_escape_encode(s, quote))
+
+def string_escape_encode(s, quote):
+
+ buf = StringBuilder(len(s) + 2)
+
buf.append(quote)
startslice = 0
@@ -959,7 +963,7 @@
buf.append(quote)
- return space.wrap(buf.build())
+ return buf.build()
DEFAULT_NOOP_TABLE = ''.join([chr(i) for i in range(256)])
More information about the pypy-commit
mailing list