Skip to content

Commit

Permalink
[mypyc] Merge dict get op and generic power op (#9347)
Browse files Browse the repository at this point in the history
This PR merges get op with None as default value for dict, and a power op for generic numbers, which completes the support of generic_ops.py and dict_ops.py
  • Loading branch information
TH3CHARLie authored Aug 25, 2020
1 parent 05a4cab commit a72fca2
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ static void CPy_LogGetAttr(const char *method, PyObject *obj, PyObject *attr) {
CPyTagged CPyObject_Hash(PyObject *o);
PyObject *CPyObject_GetAttr3(PyObject *v, PyObject *name, PyObject *defl);
PyObject *CPyIter_Next(PyObject *iter);
PyObject *CPyNumber_Power(PyObject *base, PyObject *index);


// List operations
Expand All @@ -325,6 +326,7 @@ PyObject *CPySequence_RMultiply(CPyTagged t_size, PyObject *seq);
PyObject *CPyDict_GetItem(PyObject *dict, PyObject *key);
int CPyDict_SetItem(PyObject *dict, PyObject *key, PyObject *value);
PyObject *CPyDict_Get(PyObject *dict, PyObject *key, PyObject *fallback);
PyObject *CPyDict_GetWithNone(PyObject *dict, PyObject *key);
PyObject *CPyDict_Build(Py_ssize_t size, ...);
int CPyDict_Update(PyObject *dict, PyObject *stuff);
int CPyDict_UpdateInDisplay(PyObject *dict, PyObject *stuff);
Expand Down
4 changes: 4 additions & 0 deletions mypyc/lib-rt/dict_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ PyObject *CPyDict_Get(PyObject *dict, PyObject *key, PyObject *fallback) {
return res;
}

PyObject *CPyDict_GetWithNone(PyObject *dict, PyObject *key) {
return CPyDict_Get(dict, key, Py_None);
}

int CPyDict_SetItem(PyObject *dict, PyObject *key, PyObject *value) {
if (PyDict_CheckExact(dict)) {
return PyDict_SetItem(dict, key, value);
Expand Down
5 changes: 5 additions & 0 deletions mypyc/lib-rt/generic_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ PyObject *CPyIter_Next(PyObject *iter)
{
return (*iter->ob_type->tp_iternext)(iter);
}

PyObject *CPyNumber_Power(PyObject *base, PyObject *index)
{
return PyNumber_Power(base, index, Py_None);
}
10 changes: 5 additions & 5 deletions mypyc/primitives/dict_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
)

from mypyc.primitives.registry import (
method_op, simple_emit, c_custom_op, c_method_op, c_function_op, c_binary_op, load_address_op
c_custom_op, c_method_op, c_function_op, c_binary_op, load_address_op
)

# Get the 'dict' type object.
Expand Down Expand Up @@ -77,12 +77,12 @@
error_kind=ERR_MAGIC)

# dict.get(key)
method_op(
c_method_op(
name='get',
arg_types=[dict_rprimitive, object_rprimitive],
result_type=object_rprimitive,
error_kind=ERR_MAGIC,
emit=simple_emit('{dest} = CPyDict_Get({args[0]}, {args[1]}, Py_None);'))
return_type=object_rprimitive,
c_function_name='CPyDict_GetWithNone',
error_kind=ERR_MAGIC)

# Construct an empty dictionary.
dict_new_op = c_custom_op(
Expand Down
14 changes: 7 additions & 7 deletions mypyc/primitives/generic_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
object_rprimitive, int_rprimitive, bool_rprimitive, c_int_rprimitive, pointer_rprimitive
)
from mypyc.primitives.registry import (
binary_op, simple_emit, c_binary_op, c_unary_op, c_method_op, c_function_op, c_custom_op
c_binary_op, c_unary_op, c_method_op, c_function_op, c_custom_op
)


Expand Down Expand Up @@ -72,12 +72,12 @@
error_kind=ERR_MAGIC,
priority=0)

binary_op(op='**',
arg_types=[object_rprimitive, object_rprimitive],
result_type=object_rprimitive,
error_kind=ERR_MAGIC,
emit=simple_emit('{dest} = PyNumber_Power({args[0]}, {args[1]}, Py_None);'),
priority=0)
c_binary_op(name='**',
arg_types=[object_rprimitive, object_rprimitive],
return_type=object_rprimitive,
error_kind=ERR_MAGIC,
c_function_name='CPyNumber_Power',
priority=0)

c_binary_op(
name='in',
Expand Down

0 comments on commit a72fca2

Please sign in to comment.