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

Fix for #6005 #6072

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 29 additions & 11 deletions numba/core/typing/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,28 @@ def get_impl_key(self, sig):
key = key.im_func
return key

@classmethod
def get_source_code_info(cls, impl):
"""
Gets the source information about function impl.
Returns:

code - # str: source code as a string
firstlineno - # int: the first line number of the function impl
path - #str: the path to file containing impl
stuartarchibald marked this conversation as resolved.
Show resolved Hide resolved

if any of the above are not available something generic is returned
"""
try:
code, firstlineno = inspect.getsourcelines(impl)
except OSError: # missing source, probably a string
code = "None available (built from string?)"
firstlineno = 0
path = inspect.getsourcefile(impl)
if path is None:
path = "<unknown> (built from string?)"
return code, firstlineno, path

@abstractmethod
def get_template_info(self):
"""
Expand Down Expand Up @@ -346,8 +368,8 @@ def unpack_opt(x):
def get_template_info(self):
impl = getattr(self, "generic")
basepath = os.path.dirname(os.path.dirname(numba.__file__))
code, firstlineno = inspect.getsourcelines(impl)
path = inspect.getsourcefile(impl)

code, firstlineno, path = self.get_source_code_info(impl)
sig = str(utils.pysignature(impl))
info = {
'kind': "overload",
Expand Down Expand Up @@ -421,8 +443,7 @@ def unpack_opt(x):
def get_template_info(self):
impl = getattr(self, "generic")
basepath = os.path.dirname(os.path.dirname(numba.__file__))
code, firstlineno = inspect.getsourcelines(impl)
path = inspect.getsourcefile(impl)
code, firstlineno, path = self.get_source_code_info(impl)
sig = str(utils.pysignature(impl))
info = {
'kind': "overload",
Expand Down Expand Up @@ -730,7 +751,7 @@ def get_impl_key(self, sig):

@classmethod
def get_source_info(cls):
"""Return a dictionary with information about the source code of the
"""Return a dictionary with information about the source code of the
implementation.

Returns
Expand All @@ -751,8 +772,7 @@ def get_source_info(cls):
"""
basepath = os.path.dirname(os.path.dirname(numba.__file__))
impl = cls._overload_func
code, firstlineno = inspect.getsourcelines(impl)
path = inspect.getsourcefile(impl)
code, firstlineno, path = cls.get_source_code_info(impl)
sig = str(utils.pysignature(impl))
info = {
'kind': "overload",
Expand All @@ -767,8 +787,7 @@ def get_source_info(cls):
def get_template_info(self):
basepath = os.path.dirname(os.path.dirname(numba.__file__))
impl = self._overload_func
code, firstlineno = inspect.getsourcelines(impl)
path = inspect.getsourcefile(impl)
code, firstlineno, path = self.get_source_code_info(impl)
sig = str(utils.pysignature(impl))
info = {
'kind': "overload",
Expand Down Expand Up @@ -836,8 +855,7 @@ def get_impl_key(self, sig):
def get_template_info(self):
basepath = os.path.dirname(os.path.dirname(numba.__file__))
impl = self._definition_func
code, firstlineno = inspect.getsourcelines(impl)
path = inspect.getsourcefile(impl)
code, firstlineno, path = self.get_source_code_info(impl)
sig = str(utils.pysignature(impl))
info = {
'kind': "intrinsic",
Expand Down
20 changes: 20 additions & 0 deletions numba/tests/test_errorhandling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from numba import jit, njit, typed, int64, types
from numba.core import errors
import numba.core.typing.cffi_utils as cffi_support
from numba.experimental import structref
from numba.extending import (overload, intrinsic, overload_method,
overload_attribute)
from numba.core.compiler import CompilerBase
Expand Down Expand Up @@ -423,6 +424,25 @@ def foo():
excstr = str(raises.exception)
self.assertIn("Type Restricted Function in function 'unknown'", excstr)

def test_missing_source(self):

@structref.register
class ParticleType(types.StructRef):
pass

class Particle(structref.StructRefProxy):
def __new__(cls, pos, mass):
return structref.StructRefProxy.__new__(cls, pos)
# didn't provide the required mass argument ----^

structref.define_proxy(Particle, ParticleType, ["pos", "mass"])

with self.assertRaises(errors.TypingError) as raises:
Particle(pos=1, mass=2)

excstr = str(raises.exception)
self.assertIn("required positional argument: 'mass'", excstr)


class TestDeveloperSpecificErrorMessages(SerialMixin, unittest.TestCase):

Expand Down