Author: Tyler Wade <[email protected]>
Branch: fix-bytearray-complexity
Changeset: r71880:cfdc8b7369aa
Date: 2014-06-02 10:35 -0500
http://bitbucket.org/pypy/pypy/changeset/cfdc8b7369aa/
Log: Buffer.__getlen__ needs an assert >= 0
diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -13,7 +13,9 @@
raise NotImplementedError
def __len__(self):
- return self.getlength()
+ res = self.getlength()
+ assert res >= 0
+ return res
def as_str(self):
"Returns an interp-level string with the whole content of the buffer."
diff --git a/rpython/rlib/test/test_buffer.py b/rpython/rlib/test/test_buffer.py
--- a/rpython/rlib/test/test_buffer.py
+++ b/rpython/rlib/test/test_buffer.py
@@ -1,4 +1,6 @@
from rpython.rlib.buffer import *
+from rpython.annotator.annrpython import RPythonAnnotator
+from rpython.annotator.model import SomeInteger
def test_string_buffer():
@@ -11,3 +13,22 @@
assert buf.getslice(1, 6, 1, 5) == buf[1:6]
assert buf.getslice(1, 6, 2, 3) == 'el '
assert buf.as_str() == 'hello world'
+
+
+
+def test_len_nonneg():
+ # This test needs a buffer subclass whose getlength() isn't guaranteed to
+ # return a non-neg integer.
+ class DummyBuffer(Buffer):
+ def __init__(self, s):
+ self.size = s
+
+ def getlength(self):
+ return self.size
+ def func(n):
+ buf = DummyBuffer(n)
+ return len(buf)
+
+ a = RPythonAnnotator()
+ s = a.build_types(func, [int])
+ assert s == SomeInteger(nonneg=True)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit