Skip to content

Commit

Permalink
Fix for #6005
Browse files Browse the repository at this point in the history
Adds logic to handle missing source/source file.

Closes #6005
  • Loading branch information
stuartarchibald committed Aug 4, 2020
1 parent 6bb2930 commit dc16d2c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
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
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

0 comments on commit dc16d2c

Please sign in to comment.