Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Merge dict get op and generic power op #9347

Merged
merged 3 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mypyc/lib-rt/dict_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ PyObject *CPyDict_Get(PyObject *dict, PyObject *key, PyObject *fallback) {
return res;
}

PyObject *CPyDict_GetWithNone(PyObject *dict, PyObject *key) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just calling CPyDict_Get in the body with an extra Py_None argument? The extra call should have minimal performance impact, and this would reduce some code repetition?

Declare the function in CPy.h.

PyObject *res = PyDict_GetItemWithError(dict, key);
if (!res) {
if (PyErr_Occurred()) {
return NULL;
}
res = Py_None;
}
Py_INCREF(res);
return res;
}

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare this in CPy.h (it's unfortunate that this isn't caught by our tests).

{
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