Skip to content
Browse files

Support numba.addressof() (issue #141)

  • Loading branch information...
1 parent 45d5a5b commit 59400c3634b2c15fc8e5c934faeb2315ca458904 @markflorisson markflorisson committed Apr 4, 2013
Showing with 102 additions and 2 deletions.
  1. +3 −0 numba/pipeline.py
  2. +47 −2 numba/special.py
  3. +52 −0 numba/tests/test_addressof.py
View
3 numba/pipeline.py
@@ -545,6 +545,9 @@ def transform(self, ast, env):
func_env.numba_wrapper_func = numbawrapper
func_env.llvm_wrapper_func = lfuncwrapper
+ # Set pointer to function for external code and numba.addressof()
+ numbawrapper.lfunc_pointer = func_env.lfunc_pointer
+
return ast
class ErrorReporting(PipelineStage):
View
49 numba/special.py
@@ -1,16 +1,56 @@
# -*- coding: utf-8 -*-
+
"""
Special compiler-recognized numba functions and attributes.
"""
+
from __future__ import print_function, division, absolute_import
-__all__ = ['NULL', 'typeof', 'python', 'nopython']
+__all__ = ['NULL', 'typeof', 'python', 'nopython', 'addressof']
+
+import ctypes
+
+from numba import error
+
+#------------------------------------------------------------------------
+# Pointers
+#------------------------------------------------------------------------
class NumbaDotNULL(object):
"NULL pointer"
NULL = NumbaDotNULL()
+
+def addressof(obj, propagate=True):
+ """
+ Take the address of a compiled jit function.
+
+ :param obj: the jit function
+ :param write_unraisable: whether to write uncaught exceptions to stderr
+ :param propagate: whether to always propagate exceptions
+
+ :return: ctypes function pointer
+ """
+ from numba import numbawrapper
+
+ if not propagate:
+ raise ValueError("Writing unraisable exception is not yet supported")
+
+ if not isinstance(obj, numbawrapper.numbafunction_type):
+ raise TypeError("Object is not a jit function")
+
+ if obj.lfunc_pointer is None:
+ raise ValueError(
+ "Jit function does not have pointer")
+
+ ctypes_sig = obj.signature.to_ctypes()
+ return ctypes.cast(obj.lfunc_pointer, ctypes_sig)
+
+#------------------------------------------------------------------------
+# Types
+#------------------------------------------------------------------------
+
def typeof(variable):
"""
Get the type of a variable.
@@ -21,6 +61,10 @@ def typeof(variable):
context = NumbaEnvironment.get_environment().context
return context.typemapper.from_python(variable)
+#------------------------------------------------------------------------
+# python/nopython context managers
+#------------------------------------------------------------------------
+
class NoopContext(object):
def __init__(self, name):
@@ -36,4 +80,5 @@ def __repr__(self):
return self.name
python = NoopContext("python")
-nopython = NoopContext("nopython")
+nopython = NoopContext("nopython")
+
View
52 numba/tests/test_addressof.py
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+
+"""
+Test numba.addressof().
+"""
+
+from __future__ import print_function, division, absolute_import
+
+import ctypes
+
+import numba
+from numba import *
+
+
+@jit(int32(int32, int32))
+def func(a, b):
+ return a * b
+
+@autojit
+def error_func():
+ pass
+
+#------------------------------------------------------------------------
+# Tests
+#------------------------------------------------------------------------
+
+def test_addressof(arg):
+ """
+ >>> func = test_addressof(func)
+ >>> assert func.restype == ctypes.c_int32
+ >>> assert func.argtypes == (ctypes.c_int32, ctypes.c_int32)
+ >>> func(5, 2)
+ 10
+ """
+ return numba.addressof(arg)
+
+def test_addressof_error(arg, **kwds):
+ """
+ >>> test_addressof_error(error_func)
+ Traceback (most recent call last):
+ ...
+ TypeError: Object is not a jit function
+
+ >>> test_addressof_error(func, propagate=False)
+ Traceback (most recent call last):
+ ...
+ ValueError: Writing unraisable exception is not yet supported
+ """
+ return numba.addressof(arg, **kwds)
+
+
+numba.testmod()

0 comments on commit 59400c3

Please sign in to comment.
Something went wrong with that request. Please try again.