[pypy-svn] r76089 - in pypy/branch/fast-forward/pypy/module/struct: . test
benjamin at codespeak.net
benjamin at codespeak.net
Sat Jul 10 16:44:09 CEST 2010
Author: benjamin
Date: Sat Jul 10 16:44:07 2010
New Revision: 76089
Modified:
pypy/branch/fast-forward/pypy/module/struct/formatiterator.py
pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
Log:
fun with deprecation warnings
Modified: pypy/branch/fast-forward/pypy/module/struct/formatiterator.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/struct/formatiterator.py (original)
+++ pypy/branch/fast-forward/pypy/module/struct/formatiterator.py Sat Jul 10 16:44:07 2010
@@ -1,10 +1,11 @@
from pypy.interpreter.error import OperationError
+from pypy.rlib.objectmodel import specialize
from pypy.rlib.rstruct.error import StructError
from pypy.rlib.rstruct.standardfmttable import PACK_ACCEPTS_BROKEN_INPUT
-from pypy.rlib.rstruct.formatiterator import FormatIterator, \
- CalcSizeFormatIterator
+from pypy.rlib.rstruct.formatiterator import (FormatIterator,
+ CalcSizeFormatIterator)
class PackFormatIterator(FormatIterator):
@@ -44,44 +45,46 @@
# permissive version - accepts float arguments too
def accept_int_arg(self):
- w_obj = self.accept_obj_arg()
- try:
- return self.space.int_w(w_obj)
- except OperationError, e:
- return self.space.int_w(self._maybe_float(e, w_obj))
+ return self._accept_integral("int_w")
def accept_uint_arg(self):
- w_obj = self.accept_obj_arg()
- try:
- return self.space.uint_w(w_obj)
- except OperationError, e:
- return self.space.uint_w(self._maybe_float(e, w_obj))
+ return self._accept_integral("uint_w")
def accept_longlong_arg(self):
- w_obj = self.accept_obj_arg()
- try:
- return self.space.r_longlong_w(w_obj)
- except OperationError, e:
- return self.space.r_longlong_w(self._maybe_float(e, w_obj))
+ return self._accept_integral("r_longlong_w")
def accept_ulonglong_arg(self):
+ return self._accept_integral("r_ulonglong_w")
+
+ @specialize.arg(1)
+ def _accept_integral(self, meth):
+ space = self.space
w_obj = self.accept_obj_arg()
- try:
- return self.space.r_ulonglong_w(w_obj)
- except OperationError, e:
- return self.space.r_ulonglong_w(self._maybe_float(e, w_obj))
+ if (space.isinstance_w(w_obj, space.w_int) or
+ space.isinstance_w(w_obj, space.w_long)):
+ w_index = w_obj
+ else:
+ w_index = None
+ w_index_method = space.lookup(w_obj, "__index__")
+ if w_index_method is not None:
+ try:
+ w_index = space.index(w_obj)
+ except OperationError, e:
+ if not e.match(space, space.w_TypeError):
+ raise
+ pass
+ if w_index is None:
+ w_index = self._maybe_float(w_obj)
+ return getattr(space, meth)(w_index)
- def _maybe_float(self, e, w_obj):
+ def _maybe_float(self, w_obj):
space = self.space
- if not e.match(space, space.w_TypeError):
- raise e
- if not space.is_true(space.isinstance(w_obj, space.w_float)):
- # CPython silliness. Have a warning, and then raise the error.
+ if space.is_true(space.isinstance(w_obj, space.w_float)):
+ space.warn("struct: integer argument expected, got float",
+ space.w_DeprecationWarning)
+ else:
space.warn("integer argument expected, got non-integer",
space.w_DeprecationWarning)
- raise e
- space.warn("struct: integer argument expected, got float",
- space.w_DeprecationWarning)
return space.int(w_obj) # wrapped float -> wrapped int or long
else:
Modified: pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py (original)
+++ pypy/branch/fast-forward/pypy/module/struct/test/test_struct.py Sat Jul 10 16:44:07 2010
@@ -55,6 +55,13 @@
assert calcsize('=bQ3i') == 1 + 8 + 3*4
+ def test_index(self):
+ class X(object):
+ def __index__(self):
+ return 3
+ assert self.struct.unpack("i", self.struct.pack("i", X()))[0] == 3
+
+
def test_deprecation_warning(self):
import warnings
for code in 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q':
More information about the Pypy-commit
mailing list