Skip to content

Commit b819f01

Browse files
Remove objmode fall back in repr implementation.
As title. This replaces the objmode fall back with a generic string referring to an object of the type of the argument.
1 parent 39dd873 commit b819f01

2 files changed

Lines changed: 13 additions & 41 deletions

File tree

‎numba/cpython/builtins.py‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -981,33 +981,17 @@ def impl(obj, name):
981981
return impl
982982

983983

984-
def _get_py_repr_and_warn(obj):
985-
# Gets the repr of an object at runtime whilst issuing a performance
986-
# warning
987-
from numba import typeof
988-
msg = ("There is no '__repr__' specialisation defined for the type "
989-
f"'{typeof(obj)}', as a result the Python interpreter has been "
990-
"called to fetch a repr(), this has a performance penalty. To avoid "
991-
"this penalty either define a '__repr__' for the type or remove the "
992-
"call to str()/repr()!")
993-
warnings.warn(NumbaPerformanceWarning(msg))
994-
return repr(obj)
995-
996-
997984
@overload(repr)
998985
def ol_repr_generic(obj):
999-
from numba import objmode
986+
missing_repr_format = f"<object type:{obj}>"
1000987
def impl(obj):
1001988
attr = '__repr__'
1002989
if hasattr(obj, attr) == True:
1003990
return getattr(obj, attr)()
1004991
else:
1005-
# There's no __str__ or __repr__ defined for this object, go via
1006-
# the interpreter.
1007-
pyrepr = ""
1008-
with objmode(pyrepr='unicode_type'):
1009-
pyrepr = _get_py_repr_and_warn(obj)
1010-
return pyrepr
992+
# There's no __str__ or __repr__ defined for this object, return
993+
# something generic
994+
return missing_repr_format
1011995
return impl
1012996

1013997

‎numba/tests/test_builtins.py‎

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,38 +1615,26 @@ def foo(x):
16151615
self.assertEqual(foo(x), foo.py_func(x))
16161616

16171617
def test_repr_fallback(self):
1618-
# checks objmode str/repr call fallback, there's no overloaded
1619-
# __str__ or __repr__ for the dummy type so it has to use the objmode
1620-
# `repr` call.
1618+
# checks str/repr fallback, there's no overloaded __str__ or __repr__
1619+
# for the dummy type so it has to use generic '<object type:(type)>'
1620+
# string for the `repr` call.
16211621

16221622
Dummy, DummyType = self.make_dummy_type()
1623-
string_repr = "this is the dummy object Python side repr"
1624-
Dummy.__repr__= lambda inst: string_repr
16251623
dummy = Dummy()
1624+
string_repr = f"<object type:{typeof(dummy)}>"
1625+
Dummy.__repr__= lambda inst: string_repr
16261626

16271627
@box(DummyType)
16281628
def box_dummy(typ, obj, c):
16291629
clazobj = c.pyapi.unserialize(c.pyapi.serialize_object(Dummy))
16301630
return c.pyapi.call_function_objargs(clazobj, ())
16311631

1632-
with warnings.catch_warnings(record=True) as w:
1633-
warnings.simplefilter('always', errors.NumbaPerformanceWarning)
1634-
ignore_internal_warnings()
1635-
1636-
@njit
1637-
def foo(x):
1638-
return str(x)
1639-
1640-
self.assertEqual(foo(dummy), foo.py_func(dummy))
1641-
1642-
self.assertEqual(len(w), 1)
1632+
@njit
1633+
def foo(x):
1634+
return str(x)
16431635

1644-
self.assertEqual(w[0].category,
1645-
errors.NumbaPerformanceWarning)
1636+
self.assertEqual(foo(dummy), foo.py_func(dummy))
16461637

1647-
msg = ("There is no '__repr__' specialisation defined for the "
1648-
f"type '{typeof(dummy)}'")
1649-
self.assertIn(msg, str(w[0].message))
16501638

16511639

16521640
if __name__ == '__main__':

0 commit comments

Comments
 (0)