Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r49070:c1972bf3e125
Date: 2011-11-09 16:54 -0800
http://bitbucket.org/pypy/pypy/changeset/c1972bf3e125/
Log: fix str() on bytes, reenable the -b cmd line opt
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
@@ -870,9 +870,9 @@
return space.wrap(len(w_str._value))
def str__String(space, w_str):
- if type(w_str) is W_StringObject:
- return w_str
- return wrapstr(space, w_str._value)
+ if space.sys.get_flag('bytes_warning'):
+ space.warn("str() on a bytes instance", space.w_BytesWarning)
+ return repr__String(space, w_str)
def ord__String(space, w_str):
u_str = w_str._value
diff --git a/pypy/objspace/std/test/test_stringobject.py
b/pypy/objspace/std/test/test_stringobject.py
--- a/pypy/objspace/std/test/test_stringobject.py
+++ b/pypy/objspace/std/test/test_stringobject.py
@@ -618,23 +618,24 @@
assert l == [52, 50]
def test_repr(self):
- assert repr(b"") =="b''"
- assert repr(b"a") =="b'a'"
- assert repr(b"'") =='b"\'"'
- assert repr(b"\'") =="b\"\'\""
- assert repr(b"\"") =='b\'"\''
- assert repr(b"\t") =="b'\\t'"
- assert repr(b"\\") =="b'\\\\'"
- assert repr(b'') =="b''"
- assert repr(b'a') =="b'a'"
- assert repr(b'"') =="b'\"'"
- assert repr(b'\'') =='b"\'"'
- assert repr(b'\"') =="b'\"'"
- assert repr(b'\t') =="b'\\t'"
- assert repr(b'\\') =="b'\\\\'"
- assert repr(b"'''\"") =='b\'\\\'\\\'\\\'"\''
- assert repr(b"\x13") =="b'\\x13'"
- assert repr(b"\x02") =="b'\\x02'"
+ for f in str, repr:
+ assert f(b"") =="b''"
+ assert f(b"a") =="b'a'"
+ assert f(b"'") =='b"\'"'
+ assert f(b"\'") =="b\"\'\""
+ assert f(b"\"") =='b\'"\''
+ assert f(b"\t") =="b'\\t'"
+ assert f(b"\\") =="b'\\\\'"
+ assert f(b'') =="b''"
+ assert f(b'a') =="b'a'"
+ assert f(b'"') =="b'\"'"
+ assert f(b'\'') =='b"\'"'
+ assert f(b'\"') =="b'\"'"
+ assert f(b'\t') =="b'\\t'"
+ assert f(b'\\') =="b'\\\\'"
+ assert f(b"'''\"") =='b\'\\\'\\\'\\\'"\''
+ assert f(b"\x13") =="b'\\x13'"
+ assert f(b"\x02") =="b'\\x02'"
def test_contains(self):
assert b'' in b'abc'
diff --git a/pypy/objspace/std/unicodetype.py b/pypy/objspace/std/unicodetype.py
--- a/pypy/objspace/std/unicodetype.py
+++ b/pypy/objspace/std/unicodetype.py
@@ -280,24 +280,17 @@
def unicode_from_object(space, w_obj):
if space.is_w(space.type(w_obj), space.w_unicode):
return w_obj
- elif space.is_w(space.type(w_obj), space.w_str):
- w_res = w_obj
- else:
- w_unicode_method = space.lookup(w_obj, "__unicode__")
- # obscure workaround: for the next two lines see
- # test_unicode_conversion_with__str__
- if w_unicode_method is None:
- if space.isinstance_w(w_obj, space.w_unicode):
- return space.wrap(space.unicode_w(w_obj))
- w_unicode_method = space.lookup(w_obj, "__str__")
- if w_unicode_method is not None:
- w_res = space.get_and_call_function(w_unicode_method, w_obj)
- else:
- w_res = space.str(w_obj)
- if space.isinstance_w(w_res, space.w_unicode):
- return w_res
- return unicode_from_encoded_object(space, w_res, None, "strict")
+ w_unicode_method = space.lookup(w_obj, "__str__")
+ if w_unicode_method is None:
+ return space.repr(w_obj)
+
+ w_res = space.get_and_call_function(w_unicode_method, w_obj)
+ if not space.isinstance_w(w_res, space.w_unicode):
+ typename = space.type(w_res).getname(space)
+ msg = "__str__ returned non-string (type %.200s)" % typename
+ raise OperationError(space.w_TypeError, space.wrap(msg))
+ return w_res
def descr_new_(space, w_unicodetype, w_string='', w_encoding=None,
w_errors=None):
# NB. the default value of w_obj is really a *wrapped* empty string:
diff --git a/pypy/translator/goal/app_main.py b/pypy/translator/goal/app_main.py
--- a/pypy/translator/goal/app_main.py
+++ b/pypy/translator/goal/app_main.py
@@ -392,6 +392,7 @@
'v': (simple_option, 'verbose'),
'U': (simple_option, 'unicode'),
'u': (simple_option, 'unbuffered'),
+ 'b': (simple_option, 'bytes_warning'),
# more complex options
'Q': (div_option, Ellipsis),
'c': (c_option, Ellipsis),
@@ -411,7 +412,6 @@
'3': (simple_option, 'py3k_warning'),
'B': (simple_option, 'dont_write_bytecode'),
's': (simple_option, 'no_user_site'),
- 'b': (simple_option, 'bytes_warning'),
})
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit