本文整理汇总了Python中pypy.objspace.std.longobject.W_LongObject类的典型用法代码示例。如果您正苦于以下问题:Python W_LongObject类的具体用法?Python W_LongObject怎么用?Python W_LongObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了W_LongObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pow_ovr
def pow_ovr(space, w_int1, w_int2):
try:
return _impl_pow(space, r_longlong(w_int1.intval), w_int2)
except FailedToImplementArgs:
from pypy.objspace.std import longobject
w_a = W_LongObject.fromint(space, w_int1.intval)
w_b = W_LongObject.fromint(space, w_int2.intval)
return longobject.pow__Long_Long_None(space, w_a, w_b, space.w_None)
示例2: lshift_ovr
def lshift_ovr(space, w_int1, w_int2):
a = r_longlong(w_int1.intval)
try:
return lshift__SmallLong_Int(space, W_SmallLongObject(a), w_int2)
except FailedToImplementArgs:
from pypy.objspace.std import longobject
w_a = W_LongObject.fromint(space, w_int1.intval)
w_b = W_LongObject.fromint(space, w_int2.intval)
return longobject.lshift__Long_Long(space, w_a, w_b)
示例3: _pow_ovf2long
def _pow_ovf2long(space, iv, iw, w_modulus):
if space.is_none(w_modulus) and _recover_with_smalllong(space):
from pypy.objspace.std.smalllongobject import _pow as _pow_small
try:
# XXX: shouldn't have to pass r_longlong(0) here (see
# 4fa4c6b93a84)
return _pow_small(space, r_longlong(iv), iw, r_longlong(0))
except (OverflowError, ValueError):
pass
from pypy.objspace.std.longobject import W_LongObject
w_iv = W_LongObject.fromint(space, iv)
w_iw = W_LongObject.fromint(space, iw)
return w_iv.descr_pow(space, w_iw, w_modulus)
示例4: ovf2long
def ovf2long(space, x, y):
"""Handle overflowing to smalllong or long"""
if _recover_with_smalllong(space):
if ovf2small:
return ovf2small(space, x, y)
# Assume a generic operation without an explicit ovf2small
# handler
from pypy.objspace.std.smalllongobject import W_SmallLongObject
a = r_longlong(x)
b = r_longlong(y)
return W_SmallLongObject(op(a, b))
from pypy.objspace.std.longobject import W_LongObject
w_x = W_LongObject.fromint(space, x)
w_y = W_LongObject.fromint(space, y)
return getattr(w_x, 'descr_' + opname)(space, w_y)
示例5: descr_long
def descr_long(self, space):
try:
return W_LongObject.fromfloat(space, self.floatval)
except OverflowError:
raise oefmt(space.w_OverflowError, "cannot convert float infinity to integer")
except ValueError:
raise oefmt(space.w_ValueError, "cannot convert float NaN to integer")
示例6: wrap
def wrap(self, x):
"Wraps the Python value 'x' into one of the wrapper classes."
# You might notice that this function is rather conspicuously
# not RPython. We can get away with this because the function
# is specialized (see after the function body). Also worth
# noting is that the isinstance's involving integer types
# behave rather differently to how you might expect during
# annotation (see pypy/annotation/builtin.py)
if x is None:
return self.w_None
if isinstance(x, OperationError):
raise TypeError, ("attempt to wrap already wrapped exception: %s"%
(x,))
if isinstance(x, int):
if isinstance(x, bool):
return self.newbool(x)
else:
return self.newint(x)
if isinstance(x, str):
# this hack is temporary: look at the comment in
# test_stdstdobjspace.test_wrap_string
try:
unicode_x = x.decode('ascii')
except UnicodeDecodeError:
# poor man's x.decode('ascii', 'replace'), since it's not
# supported by RPython
if not we_are_translated():
print 'WARNING: space.wrap() called on a non-ascii byte string: %r' % x
lst = []
for ch in x:
ch = ord(ch)
if ch > 127:
lst.append(u'\ufffd')
else:
lst.append(unichr(ch))
unicode_x = u''.join(lst)
return wrapunicode(self, unicode_x)
if isinstance(x, unicode):
return wrapunicode(self, x)
if isinstance(x, float):
return W_FloatObject(x)
if isinstance(x, W_Root):
w_result = x.__spacebind__(self)
#print 'wrapping', x, '->', w_result
return w_result
if isinstance(x, base_int):
if self.config.objspace.std.withsmalllong:
from pypy.objspace.std.smalllongobject import W_SmallLongObject
from rpython.rlib.rarithmetic import r_longlong, r_ulonglong
from rpython.rlib.rarithmetic import longlongmax
if (not isinstance(x, r_ulonglong)
or x <= r_ulonglong(longlongmax)):
return W_SmallLongObject(r_longlong(x))
x = widen(x)
if isinstance(x, int):
return self.newint(x)
else:
return W_LongObject.fromrarith_int(x)
return self._wrap_not_rpython(x)
示例7: newbigint
def newbigint(space, w_longtype, bigint):
"""Turn the bigint into a W_LongObject. If withsmalllong is enabled,
check if the bigint would fit in a smalllong, and return a
W_SmallLongObject instead if it does. Similar to newlong() in
longobject.py, but takes an explicit w_longtype argument.
"""
if (space.config.objspace.std.withsmalllong
and space.is_w(w_longtype, space.w_long)):
try:
z = bigint.tolonglong()
except OverflowError:
pass
else:
from pypy.objspace.std.smalllongobject import W_SmallLongObject
return W_SmallLongObject(z)
from pypy.objspace.std.longobject import W_LongObject
w_obj = space.allocate_instance(W_LongObject, w_longtype)
W_LongObject.__init__(w_obj, bigint)
return w_obj
示例8: long__Float
def long__Float(space, w_floatobj):
try:
return W_LongObject.fromfloat(space, w_floatobj.floatval)
except OverflowError:
if isnan(w_floatobj.floatval):
raise OperationError(
space.w_ValueError,
space.wrap("cannot convert float NaN to integer"))
raise OperationError(space.w_OverflowError,
space.wrap("cannot convert float infinity to long"))
示例9: eq__Float_Long
def eq__Float_Long(space, w_float1, w_long2):
# XXX naive implementation
x = w_float1.floatval
if isinf(x) or math.floor(x) != x:
return space.w_False
try:
w_long1 = W_LongObject.fromfloat(x)
except OverflowError:
return space.w_False
return space.eq(w_long1, w_long2)
示例10: lt__Float_Long
def lt__Float_Long(space, w_float1, w_long2):
# XXX naive implementation
x = w_float1.floatval
if isinf(x):
return space.newbool(x < 0.0)
x_floor = math.floor(x)
try:
w_long1 = W_LongObject.fromfloat(x_floor)
except OverflowError:
return space.newbool(x < 0.0)
return space.lt(w_long1, w_long2)
示例11: wraplong
def wraplong(self, x):
"NOT_RPYTHON"
if self.config.objspace.std.withsmalllong:
from rpython.rlib.rarithmetic import r_longlong
try:
rx = r_longlong(x)
except OverflowError:
pass
else:
from pypy.objspace.std.smalllongobject import \
W_SmallLongObject
return W_SmallLongObject(rx)
return W_LongObject.fromlong(x)
示例12: _hash_float
def _hash_float(space, v):
from pypy.objspace.std.longobject import hash__Long
if isnan(v):
return 0
# This is designed so that Python numbers of different types
# that compare equal hash to the same value; otherwise comparisons
# of mapping keys will turn out weird.
fractpart, intpart = math.modf(v)
if fractpart == 0.0:
# This must return the same hash as an equal int or long.
try:
x = ovfcheck_float_to_int(intpart)
# Fits in a C long == a Python int, so is its own hash.
return x
except OverflowError:
# Convert to long and use its hash.
try:
w_lval = W_LongObject.fromfloat(space, v)
except OverflowError:
# can't convert to long int -- arbitrary
if v < 0:
return -271828
else:
return 314159
return space.int_w(space.hash(w_lval))
# The fractional part is non-zero, so we don't have to worry about
# making this match the hash of some other type.
# Use frexp to get at the bits in the double.
# Since the VAX D double format has 56 mantissa bits, which is the
# most of any double format in use, each of these parts may have as
# many as (but no more than) 56 significant bits.
# So, assuming sizeof(long) >= 4, each part can be broken into two
# longs; frexp and multiplication are used to do that.
# Also, since the Cray double format has 15 exponent bits, which is
# the most of any double format in use, shifting the exponent field
# left by 15 won't overflow a long (again assuming sizeof(long) >= 4).
v, expo = math.frexp(v)
v *= 2147483648.0 # 2**31
hipart = int(v) # take the top 32 bits
v = (v - hipart) * 2147483648.0 # get the next 32 bits
x = intmask(hipart + int(v) + (expo << 15))
return x
示例13: wrap
def wrap(self, x):
"Wraps the Python value 'x' into one of the wrapper classes."
# You might notice that this function is rather conspicuously
# not RPython. We can get away with this because the function
# is specialized (see after the function body). Also worth
# noting is that the isinstance's involving integer types
# behave rather differently to how you might expect during
# annotation (see pypy/annotation/builtin.py)
if x is None:
return self.w_None
if isinstance(x, model.W_Object):
raise TypeError, "attempt to wrap already wrapped object: %s"%(x,)
if isinstance(x, OperationError):
raise TypeError, ("attempt to wrap already wrapped exception: %s"%
(x,))
if isinstance(x, int):
if isinstance(x, bool):
return self.newbool(x)
else:
return self.newint(x)
if isinstance(x, str):
return wrapstr(self, x)
if isinstance(x, unicode):
return wrapunicode(self, x)
if isinstance(x, float):
return W_FloatObject(x)
if isinstance(x, Wrappable):
w_result = x.__spacebind__(self)
#print 'wrapping', x, '->', w_result
return w_result
if isinstance(x, base_int):
if self.config.objspace.std.withsmalllong:
from pypy.objspace.std.smalllongobject import W_SmallLongObject
from pypy.rlib.rarithmetic import r_longlong, r_ulonglong
from pypy.rlib.rarithmetic import longlongmax
if (not isinstance(x, r_ulonglong)
or x <= r_ulonglong(longlongmax)):
return W_SmallLongObject(r_longlong(x))
x = widen(x)
if isinstance(x, int):
return self.newint(x)
else:
return W_LongObject.fromrarith_int(x)
return self._wrap_not_rpython(x)
示例14: float_as_integer_ratio__Float
def float_as_integer_ratio__Float(space, w_float):
value = w_float.floatval
if isinf(value):
w_msg = space.wrap("cannot pass infinity to as_integer_ratio()")
raise OperationError(space.w_OverflowError, w_msg)
elif isnan(value):
w_msg = space.wrap("cannot pass nan to as_integer_ratio()")
raise OperationError(space.w_ValueError, w_msg)
float_part, exp = math.frexp(value)
for i in range(300):
if float_part == math.floor(float_part):
break
float_part *= 2.0
exp -= 1
w_num = W_LongObject.fromfloat(space, float_part)
w_den = space.newlong(1)
w_exp = space.newlong(abs(exp))
w_exp = space.lshift(w_den, w_exp)
if exp > 0:
w_num = space.mul(w_num, w_exp)
else:
w_den = w_exp
# Try to return int.
return space.newtuple([space.int(w_num), space.int(w_den)])
示例15: descr_long
def descr_long(self, space):
# XXX: should try smalllong
from pypy.objspace.std.longobject import W_LongObject
return W_LongObject.fromint(space, self.intval)