| 1 |
from rpython.rlib.rstring import InvalidBaseError |
| 2 |
|
| 3 |
from pypy.interpreter.error import OperationError, oefmt |
| 4 |
from pypy.interpreter import gateway |
| 5 |
|
| 6 |
IDTAG_SHIFT = 4 |
| 7 |
IDTAG_ALT_UID = 2 |
| 8 |
|
| 9 |
IDTAG_INT = 1 |
| 10 |
IDTAG_LONG = 3 |
| 11 |
IDTAG_FLOAT = 5 |
| 12 |
IDTAG_COMPLEX = 7 |
| 13 |
IDTAG_SPECIAL = 11 |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
CMP_OPS = dict(lt='<', le='<=', eq='==', ne='!=', gt='>', ge='>=') |
| 21 |
BINARY_BITWISE_OPS = {'and': '&', 'lshift': '<<', 'or': '|', 'rshift': '>>', |
| 22 |
'xor': '^'} |
| 23 |
BINARY_OPS = dict(add='+', div='/', floordiv='//', mod='%', mul='*', sub='-', |
| 24 |
truediv='/', matmul='@', **BINARY_BITWISE_OPS) |
| 25 |
COMMUTATIVE_OPS = ('add', 'mul', 'and', 'or', 'xor') |
| 26 |
|
| 27 |
|
| 28 |
def negate(f): |
| 29 |
"""Create a function which calls `f` and negates its result. When the |
| 30 |
result is ``space.w_NotImplemented``, ``space.w_NotImplemented`` is |
| 31 |
returned. This is useful for complementing e.g. the __ne__ descriptor if |
| 32 |
your type already defines a __eq__ descriptor. |
| 33 |
""" |
| 34 |
def _negator(self, space, w_other): |
| 35 |
|
| 36 |
tmp = f(self, space, w_other) |
| 37 |
if tmp is space.w_NotImplemented: |
| 38 |
return space.w_NotImplemented |
| 39 |
return space.newbool(tmp is space.w_False) |
| 40 |
_negator.func_name = 'negate-%s' % f.func_name |
| 41 |
return _negator |
| 42 |
|
| 43 |
def get_positive_index(where, length): |
| 44 |
if where < 0: |
| 45 |
where += length |
| 46 |
if where < 0: |
| 47 |
where = 0 |
| 48 |
elif where > length: |
| 49 |
where = length |
| 50 |
assert where >= 0 |
| 51 |
return where |
| 52 |
|
| 53 |
|
| 54 |
def wrap_parsestringerror(space, e, w_source): |
| 55 |
if isinstance(e, InvalidBaseError): |
| 56 |
raise OperationError(space.w_ValueError, space.newtext(e.msg)) |
| 57 |
else: |
| 58 |
raise oefmt(space.w_ValueError, '%s: %R', |
| 59 |
e.msg, w_source) |
| 60 |
|
| 61 |
app = gateway.applevel(r''' |
| 62 |
def _classdir(klass): |
| 63 |
"""__dir__ for type objects |
| 64 |
|
| 65 |
This includes all attributes of klass and all of the base |
| 66 |
classes recursively. |
| 67 |
""" |
| 68 |
names = set() |
| 69 |
ns = getattr(klass, '__dict__', None) |
| 70 |
if ns is not None: |
| 71 |
names.update(ns) |
| 72 |
bases = getattr(klass, '__bases__', None) |
| 73 |
if bases is not None: |
| 74 |
# Note that since we are only interested in the keys, the order |
| 75 |
# we merge classes is unimportant |
| 76 |
for base in bases: |
| 77 |
names.update(_classdir(base)) |
| 78 |
return names |
| 79 |
|
| 80 |
def _objectdir(obj): |
| 81 |
"""__dir__ for generic objects |
| 82 |
|
| 83 |
Returns __dict__, __class__ and recursively up the |
| 84 |
__class__.__bases__ chain. |
| 85 |
""" |
| 86 |
names = set() |
| 87 |
ns = getattr(obj, '__dict__', None) |
| 88 |
if isinstance(ns, dict): |
| 89 |
names.update(ns) |
| 90 |
klass = getattr(obj, '__class__', None) |
| 91 |
if klass is not None: |
| 92 |
names.update(_classdir(klass)) |
| 93 |
return names |
| 94 |
''', filename=__file__) |
| 95 |
|
| 96 |
_classdir = app.interphook('_classdir') |
| 97 |
_objectdir = app.interphook('_objectdir') |
| 98 |
|
| 99 |
def generic_alias_class_getitem(space, w_cls, w_item): |
| 100 |
"See PEP 585" |
| 101 |
w_builtins = space.getbuiltinmodule('builtins') |
| 102 |
w_mod = space.call_method(w_builtins, '__import__', |
| 103 |
space.newtext("_pypy_generic_alias")) |
| 104 |
w_GenericAlias = space.getattr(w_mod, space.newtext("GenericAlias")) |
| 105 |
return space.call_function(w_GenericAlias, w_cls, w_item) |
| 106 |
|
| 107 |
def builtinclass_new_args_check(space, name, w_basetyp, w_subtyp, __args__): |
| 108 |
if space.is_w(w_basetyp, w_subtyp) or space.is_w( |
| 109 |
space.getattr(w_basetyp, space.newtext('__init__')), |
| 110 |
space.getattr(w_subtyp, space.newtext('__init__'))): |
| 111 |
if __args__.arguments_w: |
| 112 |
raise oefmt(space.w_TypeError, "%8() expected at most 1 argument, got %d", name, 1 + len(__args__.arguments_w)) |
| 113 |
if __args__.keyword_names_w: |
| 114 |
raise oefmt(space.w_TypeError, "%8() takes no keyword arguments", name) |