Skip to content

Commit

Permalink
Merge pull request #4921 from esc/typed_dict/fix_no_jit
Browse files Browse the repository at this point in the history
fix creating dict with JIT disabled
  • Loading branch information
seibert committed Dec 19, 2019
2 parents 72fc6d7 + cd57168 commit 67b8fdb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
5 changes: 3 additions & 2 deletions numba/dictobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ class Status(IntEnum):


def new_dict(key, value):
"""Construct a new dict. (Not implemented in the interpreter yet)
"""Construct a new dict.
Parameters
----------
key, value : TypeRef
Key type and value type of the new dict.
"""
raise NotImplementedError
# With JIT disabled, ignore all arguments and return a Python dict.
return dict()


@register_model(DictType)
Expand Down
25 changes: 24 additions & 1 deletion numba/tests/test_dictobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from numba.typedobjectutils import _sentry_safe_cast
from numba.utils import IS_PY3
from numba.errors import TypingError
from .support import TestCase, MemoryLeakMixin, unittest
from .support import (TestCase, MemoryLeakMixin, unittest, override_config,
forbid_codegen)

skip_py2 = unittest.skipUnless(IS_PY3, reason='not supported in py2')

Expand Down Expand Up @@ -1585,3 +1586,25 @@ def foo(x):
d = foo(Bag(a=100))
self.assertEqual(d[0].a, 100)
self.assertEqual(d[1].a, 101)


class TestNoJit(TestCase):
"""Exercise dictionary creation with JIT disabled. """

def test_dict_create_no_jit_using_new_dict(self):
with override_config('DISABLE_JIT', True):
with forbid_codegen():
d = dictobject.new_dict(int32, float32)
self.assertEqual(type(d), dict)

def test_dict_create_no_jit_using_Dict(self):
with override_config('DISABLE_JIT', True):
with forbid_codegen():
d = Dict()
self.assertEqual(type(d), dict)

def test_dict_create_no_jit_using_empty(self):
with override_config('DISABLE_JIT', True):
with forbid_codegen():
d = Dict.empty(types.int32, types.float32)
self.assertEqual(type(d), dict)
13 changes: 12 additions & 1 deletion numba/typed/typeddict.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Python wrapper that connects CPython interpreter to the numba dictobject.
"""
from numba import config
from numba.six import MutableMapping
from numba.types import DictType, TypeRef
from numba.targets.imputils import numba_typeref_ctor
Expand Down Expand Up @@ -80,12 +81,22 @@ class Dict(MutableMapping):
Implements the MutableMapping interface.
"""

def __new__(cls, dcttype=None, meminfo=None):
if config.DISABLE_JIT:
return dict.__new__(dict)
else:
return object.__new__(cls)

@classmethod
def empty(cls, key_type, value_type):
"""Create a new empty Dict with *key_type* and *value_type*
as the types for the keys and values of the dictionary respectively.
"""
return cls(dcttype=DictType(key_type, value_type))
if config.DISABLE_JIT:
return dict()
else:
return cls(dcttype=DictType(key_type, value_type))

def __init__(self, **kwargs):
"""
Expand Down

0 comments on commit 67b8fdb

Please sign in to comment.