Debian Codegraph @

pypy/module/__pypy__/interp_dict.py

raw symbols SCIP
1
2 from pypy.interpreter.error import OperationError, oefmt
3 from pypy.interpreter.gateway import unwrap_spec
4
5 @unwrap_spec(type='text')
6 def newdict(space, type):
7 """ newdict(type)
8
9 Create a normal dict with a special implementation strategy.
10
11 type is a string and can be:
12
13 * "module" - equivalent to some_module.__dict__
14
15 * "instance" - equivalent to an instance dict with a not-changing-much
16 set of keys
17
18 * "kwargs" - keyword args dict equivalent of what you get from **kwargs
19 in a function, optimized for passing around
20
21 * "strdict" - string-key only dict. This one should be chosen automatically
22 """
23 if type == 'module':
24 return space.newdict(module=True)
25 elif type == 'instance':
26 return space.newdict(instance=True)
27 elif type == 'kwargs':
28 return space.newdict(kwargs=True)
29 elif type == 'strdict':
30 return space.newdict(strdict=True)
31 else:
32 raise oefmt(space.w_TypeError, "unknown type of dict %s", type)
33
34 def reversed_dict(space, w_obj):
35 """Enumerate the keys in a dictionary object in reversed order.
36
37 This is a __pypy__ function instead of being simply done by calling
38 reversed(), for CPython compatibility: dictionaries are only ordered
39 on PyPy. You should use the collections.OrderedDict class for cases
40 where ordering is important. That class implements __reversed__ by
41 calling __pypy__.reversed_dict().
42 """
43 from pypy.objspace.std.dictmultiobject import W_DictMultiObject
44 if not isinstance(w_obj, W_DictMultiObject):
45 raise OperationError(space.w_TypeError, space.w_None)
46 return w_obj.descr_reversed(space)
47
48 def dict_popitem_first(space, w_obj):
49 """Interp-level implementation of OrderedDict.popitem(last=False).
50 """
51 from pypy.objspace.std.dictmultiobject import W_DictMultiObject
52 if not isinstance(w_obj, W_DictMultiObject):
53 raise OperationError(space.w_TypeError, space.w_None)
54 return w_obj.nondescr_popitem_first(space)
55
56 def delitem_if_value_is(space, w_obj, w_key, w_value):
57 """Atomic equivalent to: 'if dict.get(key) is value: del dict[key]'.
58
59 SPECIAL USE CASES ONLY! Avoid using on dicts which are specialized,
60 e.g. to int or str keys, because it switches to the object strategy.
61 Also, the 'is' operation is really pointer equality, so avoid using
62 it if 'value' is an immutable object like int or str.
63 """
64 from pypy.objspace.std.dictmultiobject import W_DictMultiObject
65 if not isinstance(w_obj, W_DictMultiObject):
66 raise OperationError(space.w_TypeError, space.w_None)
67 return w_obj.nondescr_delitem_if_value_is(space, w_key, w_value)
68
69 @unwrap_spec(last=bool)
70 def move_to_end(space, w_obj, w_key, last=True):
71 """Move the key in a dictionary object into the first or last position.
72
73 This is a __pypy__ function instead of being simply done by calling
74 dict.move_to_end(), for CPython compatibility: dictionaries are only
75 ordered on PyPy. You should use the collections.OrderedDict class for
76 cases where ordering is important. That class implements the
77 move_to_end() method by calling __pypy__.move_to_end().
78 """
79 from pypy.objspace.std.dictmultiobject import W_DictMultiObject
80 if not isinstance(w_obj, W_DictMultiObject):
81 raise OperationError(space.w_TypeError, space.w_None)
82 return w_obj.nondescr_move_to_end(space, w_key, last)