From a72fca2836653a957fef054bc9ff98133a9ef89c Mon Sep 17 00:00:00 2001 From: Xuanda Yang Date: Tue, 25 Aug 2020 18:22:23 +0800 Subject: [PATCH] [mypyc] Merge dict get op and generic power op (#9347) 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 --- mypyc/lib-rt/CPy.h | 2 ++ mypyc/lib-rt/dict_ops.c | 4 ++++ mypyc/lib-rt/generic_ops.c | 5 +++++ mypyc/primitives/dict_ops.py | 10 +++++----- mypyc/primitives/generic_ops.py | 14 +++++++------- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index d512c4632ffc..efa1941e6a6b 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -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 @@ -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); diff --git a/mypyc/lib-rt/dict_ops.c b/mypyc/lib-rt/dict_ops.c index b201313f0c93..52ccc2c94b77 100644 --- a/mypyc/lib-rt/dict_ops.c +++ b/mypyc/lib-rt/dict_ops.c @@ -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); diff --git a/mypyc/lib-rt/generic_ops.c b/mypyc/lib-rt/generic_ops.c index 685e214ca793..c619e56d0c1f 100644 --- a/mypyc/lib-rt/generic_ops.c +++ b/mypyc/lib-rt/generic_ops.c @@ -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); +} diff --git a/mypyc/primitives/dict_ops.py b/mypyc/primitives/dict_ops.py index 031aee813e41..f0d15d272161 100644 --- a/mypyc/primitives/dict_ops.py +++ b/mypyc/primitives/dict_ops.py @@ -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. @@ -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( diff --git a/mypyc/primitives/generic_ops.py b/mypyc/primitives/generic_ops.py index efd715bf216f..752511304e23 100644 --- a/mypyc/primitives/generic_ops.py +++ b/mypyc/primitives/generic_ops.py @@ -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 ) @@ -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',