Author: Armin Rigo <[email protected]>
Branch:
Changeset: r69269:311bb87a8956
Date: 2014-02-22 10:58 +0100
http://bitbucket.org/pypy/pypy/changeset/311bb87a8956/
Log: Use the RPython-level detection of out-of-bound indices. This
should give better code when jitted (lower number of guards).
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
@@ -40,7 +40,11 @@
return len(self.data)
def _getitem_result(self, space, index):
- return space.wrap(ord(self.data[index]))
+ try:
+ character = self.data[index]
+ except IndexError:
+ raise oefmt(space.w_IndexError, "bytearray index out of range")
+ return space.wrap(ord(character))
def _val(self, space):
return space.bufferstr_w(self)
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
@@ -78,15 +78,15 @@
return self._new_from_list(ret)
index = space.getindex_w(w_index, space.w_IndexError, "string index")
- selflen = self._len()
- if index < 0:
- index += selflen
- if index < 0 or index >= selflen:
- raise oefmt(space.w_IndexError, "string index out of range")
return self._getitem_result(space, index)
def _getitem_result(self, space, index):
- return self._new(self._val(space)[index])
+ selfvalue = self._val(space)
+ try:
+ character = selfvalue[index]
+ except IndexError:
+ raise oefmt(space.w_IndexError, "string index out of range")
+ return self._new(character)
def descr_getslice(self, space, w_start, w_stop):
selfvalue = self._val(space)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit