[pypy-commit] pypy utf8-unicode2: Fix more translated vs untranslated bool issues (apparently its __nonzero__ instead of __bool__ in Python 2)

waedt noreply at buildbot.pypy.org
Sun Aug 10 09:14:59 CEST 2014


Author: Tyler Wade <wayedt at gmail.com>
Branch: utf8-unicode2
Changeset: r72741:c963423d5f20
Date: 2014-08-10 02:14 -0500
http://bitbucket.org/pypy/pypy/changeset/c963423d5f20/

Log:	Fix more translated vs untranslated bool issues (apparently its
	__nonzero__ instead of __bool__ in Python 2)

diff --git a/pypy/interpreter/test/test_utf8.py b/pypy/interpreter/test/test_utf8.py
--- a/pypy/interpreter/test/test_utf8.py
+++ b/pypy/interpreter/test/test_utf8.py
@@ -196,6 +196,10 @@
     assert s.rsplit(' ', 2) == u.rsplit(' ', 2)
     assert s.rsplit('\n') == [s]
 
+def test_untranslated_bool():
+    r = bool(Utf8Str(''))
+    assert r == True
+
 def test_copy_to_new_wcharp():
     s = build_utf8str()
     if sys.maxunicode < 0x10000 and rffi.sizeof(rffi.WCHAR_T) == 4:
diff --git a/pypy/interpreter/utf8.py b/pypy/interpreter/utf8.py
--- a/pypy/interpreter/utf8.py
+++ b/pypy/interpreter/utf8.py
@@ -245,9 +245,9 @@
         assert self._len >= 0
         return self._len
 
-    def __bool__(self):
+    def __nonzero__(self):
         # XXX Make the untranslated behavior the same as the translated behavior
-        raise True
+        return True
 
     def __hash__(self):
         return compute_hash(self.bytes)
diff --git a/pypy/module/_io/interp_stringio.py b/pypy/module/_io/interp_stringio.py
--- a/pypy/module/_io/interp_stringio.py
+++ b/pypy/module/_io/interp_stringio.py
@@ -43,7 +43,8 @@
             self.readnl = newline
         self.readuniversal = newline is None or len(newline) == 0
         self.readtranslate = newline is None
-        if newline and utf8ord(newline) == ord("\r"):
+        if (newline is not None and len(newline) > 0 and
+            utf8ord(newline) == ord("\r")):
             self.writenl = newline
         if self.readuniversal:
             self.w_decoder = space.call_function(
diff --git a/pypy/module/_io/interp_textio.py b/pypy/module/_io/interp_textio.py
--- a/pypy/module/_io/interp_textio.py
+++ b/pypy/module/_io/interp_textio.py
@@ -382,11 +382,11 @@
 
         self.line_buffering = line_buffering
 
-        self.readuniversal = not newline # null or empty
+        self.readuniversal = newline is None or len(newline) == 0
         self.readtranslate = newline is None
         self.readnl = newline
 
-        self.writetranslate = (newline is None or len(newline) == 0)
+        self.writetranslate = newline is None or len(newline) == 0
         if not self.readuniversal:
             self.writenl = self.readnl
             if utf8.EQ(self.writenl, Utf8Str('\n')):
@@ -646,7 +646,7 @@
         while True:
             # First, get some data if necessary
             has_data = True
-            while not self.decoded_chars:
+            while self.decoded_chars is None or len(self.decoded_chars) == 0:
                 try:
                     if not self._read_chunk(space):
                         has_data = False
@@ -935,7 +935,7 @@
         w_pos = space.call_method(self.w_buffer, "tell")
 
         if self.w_decoder is None or self.snapshot is None:
-            assert not self.decoded_chars
+            assert self.decoded_chars is None or len(self.decoded_chars) == 0
             return w_pos
 
         cookie = PositionCookie(space.bigint_w(w_pos))
diff --git a/pypy/module/_io/test/test_textio.py b/pypy/module/_io/test/test_textio.py
--- a/pypy/module/_io/test/test_textio.py
+++ b/pypy/module/_io/test/test_textio.py
@@ -132,6 +132,14 @@
         t.read(4)
         assert t.tell() == 4
 
+        r = _io.BytesIO("abc")
+        t = _io.TextIOWrapper(r)
+        assert t.read(2) ==  "ab"
+        assert t.read(1) ==  "c"
+        assert t.read(1) ==  ""
+        assert t.read() ==  ""
+        assert t.tell() ==  3
+
     def test_destructor(self):
         import _io
         l = []
diff --git a/pypy/objspace/std/stringmethods.py b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -194,7 +194,7 @@
     @unwrap_spec(tabsize=int)
     def descr_expandtabs(self, space, tabsize=8):
         value = self._val(space)
-        if not value:
+        if value is None or len(value) == 0:
             return self._empty()
 
         if self._use_rstr_ops(space, self):
@@ -222,7 +222,7 @@
         """calculates distance behind the token to the next tabstop"""
 
         distance = tabsize
-        if token:
+        if token is not None and len(token) != 0:
             distance = 0
             offset = len(token)
 


More information about the pypy-commit mailing list