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

Support attribute access from other modules #6334

Merged
merged 7 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions numba/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def get_bound_function(self, builder, obj, ty):
assert self.get_value_type(ty) == obj.type
return obj

def get_getattr(self, typ, attr):
def get_getattr(self, builder, typ, attr):
"""
Get the getattr() implementation for the given type and attribute name.
The return value is a callable with the signature
Expand All @@ -607,7 +607,7 @@ def get_getattr(self, typ, attr):
return None
else:
pyval = getattr(typ.pymod, attr)
llval = self.get_constant(attrty, pyval)
llval = self.get_constant_generic(builder, attrty, pyval)
def imp(context, builder, typ, val, attr):
return impl_ret_borrowed(context, builder, attrty, llval)
return imp
Expand Down
2 changes: 1 addition & 1 deletion numba/core/lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ def lower_expr(self, resty, expr):
self.incref(resty, res)
return res
else:
impl = self.context.get_getattr(ty, expr.attr)
impl = self.context.get_getattr(self.builder, ty, expr.attr)
attrty = self.context.typing_context.resolve_getattr(ty,
expr.attr)

Expand Down
2 changes: 1 addition & 1 deletion numba/core/optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def optional_getattr(context, builder, typ, value, attr):
"""
inner_type = typ.type
val = context.cast(builder, value, typ, inner_type)
imp = context.get_getattr(inner_type, attr)
imp = context.get_getattr(builder, inner_type, attr)
return imp(context, builder, inner_type, val, attr)


Expand Down
2 changes: 1 addition & 1 deletion numba/cpython/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def deferred_getattr(context, builder, typ, value, attr):
"""
inner_type = typ.get()
val = context.cast(builder, value, typ, inner_type)
imp = context.get_getattr(inner_type, attr)
imp = context.get_getattr(builder, inner_type, attr)
return imp(context, builder, inner_type, val, attr)

@lower_cast(types.Any, types.DeferredType)
Expand Down
2 changes: 1 addition & 1 deletion numba/np/arrayobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -2421,7 +2421,7 @@ def record_getitem(context, builder, sig, args):
"""
Record.__getitem__ redirects to getattr()
"""
impl = context.get_getattr(sig.args[0], args[1])
impl = context.get_getattr(builder, sig.args[0], args[1])
return impl(context, builder, sig.args[0], args[0], args[1])


Expand Down
2 changes: 1 addition & 1 deletion numba/np/npyimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def cast(self, val, fromty, toty):
# attempt conversion of the real part to the specified type.
# note that NumPy issues a warning in this kind of conversions
newty = fromty.underlying_float
attr = self.context.get_getattr(fromty, 'real')
attr = self.context.get_getattr(self.builder, fromty, 'real')
val = attr(self.context, self.builder, fromty, val, 'real')
fromty = newty
# let the regular cast do the rest...
Expand Down
8 changes: 7 additions & 1 deletion numba/tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@


import unittest
from numba import njit
from numba.core.compiler import compile_isolated, Flags
from numba.core import types
from numba.tests.support import TestCase


enable_pyobj_flags = Flags()
enable_pyobj_flags.set("enable_pyobject")

Expand All @@ -30,6 +30,12 @@ def delattr_usecase(o):


class TestAttributes(TestCase):
def test_getattr(self, flags=enable_pyobj_flags):
import numba.tests.usecases as uc
@njit
def f():
return uc._GLOBAL_STR
self.assertEquals(f(), "abc")

def test_setattr(self, flags=enable_pyobj_flags):
pyfunc = setattr_usecase
Expand Down
2 changes: 2 additions & 0 deletions numba/tests/usecases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import numpy as np
from numba import jit

_GLOBAL_STR = "abc"

def sum1d(s, e):
c = 0
for i in range(s, e):
Expand Down