Author: Carl Friedrich Bolz <[email protected]>
Branch: 
Changeset: r80477:bffa98819224
Date: 2015-10-29 10:26 +0100
http://bitbucket.org/pypy/pypy/changeset/bffa98819224/

Log:    issue #1383 fixed

        don't accept None as encoding in various places

diff --git a/pypy/objspace/std/bytearrayobject.py 
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -199,13 +199,9 @@
 
         if w_source is None:
             w_source = space.wrap('')
-        if w_encoding is None:
-            w_encoding = space.w_None
-        if w_errors is None:
-            w_errors = space.w_None
 
         # Unicode argument
-        if not space.is_w(w_encoding, space.w_None):
+        if w_encoding is not None:
             from pypy.objspace.std.unicodeobject import (
                 _get_encoding_and_errors, encode_object
             )
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -16,8 +16,8 @@
 from pypy.objspace.std.formatting import mod_format
 from pypy.objspace.std.stringmethods import StringMethods
 from pypy.objspace.std.unicodeobject import (
-    _get_encoding_and_errors, decode_object, unicode_from_encoded_object,
-    unicode_from_string)
+    decode_object, unicode_from_encoded_object,
+    unicode_from_string, getdefaultencoding)
 
 
 class W_AbstractBytesObject(W_Root):
@@ -39,13 +39,8 @@
 
     def unicode_w(self, space):
         # Use the default encoding.
-        w_defaultencoding = space.call_function(space.sys.get(
-                                                'getdefaultencoding'))
-        encoding, errors = _get_encoding_and_errors(space, w_defaultencoding,
-                                                    space.w_None)
-        if encoding is None and errors is None:
-            return space.unicode_w(unicode_from_string(space, self))
-        return space.unicode_w(decode_object(space, self, encoding, errors))
+        encoding = getdefaultencoding(space)
+        return space.unicode_w(decode_object(space, self, encoding, None))
 
     def descr_add(self, space, w_other):
         """x.__add__(y) <==> x+y"""
diff --git a/pypy/objspace/std/test/test_unicodeobject.py 
b/pypy/objspace/std/test/test_unicodeobject.py
--- a/pypy/objspace/std/test/test_unicodeobject.py
+++ b/pypy/objspace/std/test/test_unicodeobject.py
@@ -992,3 +992,10 @@
         assert u''.join([s1]) is not s1
         s2 = StrSubclass(u'a')
         assert u''.join([s2]) is not s2
+
+    def test_encoding_and_errors_cant_be_none(self):
+        raises(TypeError, "''.decode(None)")
+        raises(TypeError, "u''.encode(None)")
+        raises(TypeError, "unicode('', encoding=None)")
+        raises(TypeError, 'u"".encode("utf-8", None)')
+
diff --git a/pypy/objspace/std/unicodeobject.py 
b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -427,8 +427,8 @@
 
 
 def _get_encoding_and_errors(space, w_encoding, w_errors):
-    encoding = None if space.is_none(w_encoding) else space.str_w(w_encoding)
-    errors = None if space.is_none(w_errors) else space.str_w(w_errors)
+    encoding = None if w_encoding is None else space.str_w(w_encoding)
+    errors = None if w_errors is None else space.str_w(w_errors)
     return encoding, errors
 
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to