| 1 |
""" |
| 2 |
Interp-level implementation of the basic space operations. |
| 3 |
""" |
| 4 |
|
| 5 |
import math |
| 6 |
|
| 7 |
from pypy.interpreter import gateway |
| 8 |
from pypy.interpreter.error import OperationError, oefmt |
| 9 |
from pypy.interpreter.gateway import unwrap_spec, WrappedDefault |
| 10 |
from rpython.rlib.rfloat import isfinite, round_double, round_away |
| 11 |
from rpython.rlib import rfloat, rutf8 |
| 12 |
import __builtin__ |
| 13 |
|
| 14 |
def abs(space, w_val): |
| 15 |
"abs(number) -> number\n\nReturn the absolute value of the argument." |
| 16 |
return space.abs(w_val) |
| 17 |
|
| 18 |
def ascii(space, w_obj): |
| 19 |
""""ascii(object) -> string |
| 20 |
|
| 21 |
As repr(), return a string containing a printable representation of an |
| 22 |
object, but escape the non-ASCII characters in the string returned by |
| 23 |
repr() using \\x, \\u or \\U escapes. This generates a string similar |
| 24 |
to that returned by repr() in Python 2.""" |
| 25 |
from pypy.objspace.std.unicodeobject import ascii_from_object |
| 26 |
return ascii_from_object(space, w_obj) |
| 27 |
|
| 28 |
@unwrap_spec(code=int) |
| 29 |
def chr(space, code): |
| 30 |
"Return a Unicode string of one character with the given ordinal." |
| 31 |
if code < 0 or code > 0x10FFFF: |
| 32 |
raise oefmt(space.w_ValueError, "chr() arg out of range") |
| 33 |
s = rutf8.unichr_as_utf8(code, allow_surrogates=True) |
| 34 |
return space.newutf8(s, 1) |
| 35 |
|
| 36 |
def len(space, w_obj): |
| 37 |
"len(object) -> integer\n\nReturn the number of items of a sequence or mapping." |
| 38 |
return space.len(w_obj) |
| 39 |
|
| 40 |
|
| 41 |
def checkattrname(space, w_name): |
| 42 |
if not space.isinstance_w(w_name, space.w_text): |
| 43 |
raise oefmt(space.w_TypeError, |
| 44 |
"attribute name must be string, not '%T'", w_name) |
| 45 |
return w_name |
| 46 |
|
| 47 |
def delattr(space, w_object, w_name): |
| 48 |
"""Delete a named attribute on an object. |
| 49 |
delattr(x, 'y') is equivalent to ``del x.y''.""" |
| 50 |
w_name = checkattrname(space, w_name) |
| 51 |
space.delattr(w_object, w_name) |
| 52 |
return space.w_None |
| 53 |
|
| 54 |
def getattr(space, w_object, w_name, w_defvalue=None): |
| 55 |
"""Get a named attribute from an object. |
| 56 |
getattr(x, 'y') is equivalent to ``x.y''.""" |
| 57 |
w_name = checkattrname(space, w_name) |
| 58 |
try: |
| 59 |
return space.getattr(w_object, w_name) |
| 60 |
except OperationError as e: |
| 61 |
if w_defvalue is not None: |
| 62 |
if e.match(space, space.w_AttributeError): |
| 63 |
return w_defvalue |
| 64 |
raise |
| 65 |
|
| 66 |
def hasattr(space, w_object, w_name): |
| 67 |
"""Return whether the object has an attribute with the given name. |
| 68 |
(This is done by calling getattr(object, name) and catching exceptions.)""" |
| 69 |
w_name = checkattrname(space, w_name) |
| 70 |
try: |
| 71 |
space.getattr(w_object, w_name) |
| 72 |
except OperationError as e: |
| 73 |
if e.match(space, space.w_AttributeError): |
| 74 |
return space.w_False |
| 75 |
raise |
| 76 |
else: |
| 77 |
return space.w_True |
| 78 |
|
| 79 |
def hash(space, w_object): |
| 80 |
"""Return a hash value for the object. Two objects which compare as |
| 81 |
equal have the same hash value. It is possible, but unlikely, for |
| 82 |
two un-equal objects to have the same hash value.""" |
| 83 |
return space.hash(w_object) |
| 84 |
|
| 85 |
def id(space, w_object): |
| 86 |
"Return the identity of an object: id(x) == id(y) if and only if x is y." |
| 87 |
w_res = space.id(w_object) |
| 88 |
space.audit("builtins.id", [w_res]) |
| 89 |
return w_res |
| 90 |
|
| 91 |
def divmod(space, w_x, w_y): |
| 92 |
"""Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.""" |
| 93 |
return space.divmod(w_x, w_y) |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
def round(space, w_number, w_ndigits=None): |
| 98 |
"""round(number[, ndigits]) -> number |
| 99 |
|
| 100 |
Round a number to a given precision in decimal digits (default 0 digits). |
| 101 |
This returns an int when called with one argument or if ndigits=None, |
| 102 |
otherwise the same type as the number. ndigits may be negative.""" |
| 103 |
round = space.lookup(w_number, '__round__') |
| 104 |
if round is None: |
| 105 |
raise oefmt(space.w_TypeError, |
| 106 |
"type %T doesn't define __round__ method", w_number) |
| 107 |
if space.is_none(w_ndigits): |
| 108 |
return space.get_and_call_function(round, w_number) |
| 109 |
else: |
| 110 |
return space.get_and_call_function(round, w_number, w_ndigits) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
iter_sentinel = gateway.applevel(''' |
| 115 |
# App-level implementation of the iter(callable,sentinel) operation. |
| 116 |
# Uses a class instead of a generator to avoid "generator already executing" |
| 117 |
# when the callable re-enters the iterator (e.g. exhausts it during the call). |
| 118 |
|
| 119 |
class _CallableIterator: |
| 120 |
def __init__(self, callable_, sentinel): |
| 121 |
self._callable = callable_ |
| 122 |
self._sentinel = sentinel |
| 123 |
self._exhausted = False |
| 124 |
|
| 125 |
def __iter__(self): |
| 126 |
return self |
| 127 |
|
| 128 |
def __next__(self): |
| 129 |
if self._exhausted: |
| 130 |
raise StopIteration |
| 131 |
try: |
| 132 |
result = self._callable() |
| 133 |
except StopIteration: |
| 134 |
self._exhausted = True |
| 135 |
raise |
| 136 |
if self._exhausted: |
| 137 |
# callable re-entered and exhausted us; discard result |
| 138 |
raise StopIteration |
| 139 |
if result == self._sentinel: |
| 140 |
self._exhausted = True |
| 141 |
raise StopIteration |
| 142 |
return result |
| 143 |
|
| 144 |
def iter_sentinel(callable_, sentinel): |
| 145 |
if not callable(callable_): |
| 146 |
raise TypeError('iter(v, w): v must be callable') |
| 147 |
return _CallableIterator(callable_, sentinel) |
| 148 |
|
| 149 |
''', filename=__file__).interphook("iter_sentinel") |
| 150 |
|
| 151 |
def iter(space, w_collection_or_callable, w_sentinel=None): |
| 152 |
"""iter(collection) -> iterator over the elements of the collection. |
| 153 |
|
| 154 |
iter(callable, sentinel) -> iterator calling callable() until it returns |
| 155 |
the sentinel. |
| 156 |
""" |
| 157 |
if w_sentinel is None: |
| 158 |
return space.iter(w_collection_or_callable) |
| 159 |
else: |
| 160 |
return iter_sentinel(space, w_collection_or_callable, w_sentinel) |
| 161 |
|
| 162 |
def next(space, w_iterator, w_default=None): |
| 163 |
"""next(iterator[, default]) |
| 164 |
Return the next item from the iterator. If default is given and the iterator |
| 165 |
is exhausted, it is returned instead of raising StopIteration.""" |
| 166 |
try: |
| 167 |
return space.next(w_iterator) |
| 168 |
except OperationError as e: |
| 169 |
if w_default is not None and e.match(space, space.w_StopIteration): |
| 170 |
return w_default |
| 171 |
raise |
| 172 |
|
| 173 |
def ord(space, w_val): |
| 174 |
"""Return the integer ordinal of a character.""" |
| 175 |
return space.ord(w_val) |
| 176 |
|
| 177 |
@unwrap_spec(w_mod=WrappedDefault(None)) |
| 178 |
def pow(space, w_base, w_exp, w_mod): |
| 179 |
"""With two arguments, equivalent to ``base**exp''. |
| 180 |
With three arguments, equivalent to ``(base**exp) % mod''. |
| 181 |
|
| 182 |
Some types, such as ints, are able to use a more efficient algorithm when |
| 183 |
invoked using the three argument form.""" |
| 184 |
return space.pow(w_base, w_exp, w_mod) |
| 185 |
|
| 186 |
def repr(space, w_object): |
| 187 |
"""Return a canonical string representation of the object. |
| 188 |
For simple object types, eval(repr(object)) == object.""" |
| 189 |
return space.repr(w_object) |
| 190 |
|
| 191 |
def setattr(space, w_object, w_name, w_val): |
| 192 |
"""Store a named attribute into an object. |
| 193 |
setattr(x, 'y', z) is equivalent to ``x.y = z''.""" |
| 194 |
w_name = checkattrname(space, w_name) |
| 195 |
space.setattr(w_object, w_name, w_val) |
| 196 |
return space.w_None |
| 197 |
|
| 198 |
def callable(space, w_object): |
| 199 |
"""Check whether the object appears to be callable (i.e., some kind of |
| 200 |
function). Note that classes are callable.""" |
| 201 |
return space.callable(w_object) |
| 202 |
|
| 203 |
@unwrap_spec(w_format_spec = WrappedDefault(u"")) |
| 204 |
def format(space, w_obj, w_format_spec): |
| 205 |
"""Format a obj according to format_spec""" |
| 206 |
return space.format(w_obj, w_format_spec) |