question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to assign 0 as NULL to void** local

See original GitHub issue

Reporting a bug

  • I have tried using the latest released version of Numba (most recent is visible in the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
  • I have included a self contained code sample to reproduce the problem. i.e. it’s possible to run as ‘python bug.py’.

Pointers can be set to NULL by assigning 0. This appears not to work with pointers to pointers.

import numba

# Succeeds
@numba.cfunc("void()", locals = {"bar": numba.types.voidptr})
def foo():
  bar = 0

# Fails
@numba.cfunc("void()", locals = {"bar": numba.types.CPointer(numba.types.voidptr)})
def foo():
  bar = 0
TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No conversion from Literal[int](0) to void** for 'bar', defined at None

I would assume that if this works with pointers generally, it shouldn’t fail for the specific case of a pointer to a pointer.

This is actually blocking progress on something for me at the moment, so any comments on workarounds would be appreciated! I need to pass a NULL void** into a ctypes function from inside a cfunc.

This seems to work fine, but I can’t alter the signature of my cfunc to accommodate so it’s not a solution for me.

@numba.cfunc(numba.types.void(numba.types.CPointer(numba.types.voidptr)))
def foo(a):
  return

foo(0)

The pointer types don’t appear to have casting support.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stuartarchibaldcommented, Dec 2, 2020

That intrinsic trick looks very nice, thanks! I just looked up Numba intrinsics after this and I see that casting to a pointer is actually the example given; I wish I’d managed to turn that page up in my googling! I think this pattern will be quite helpful for a few other things I was having difficulty with too.

No problem. If you know roughly what the LLVM is for what you want, then @intrinsic gives you an escape hatch to do that. clang -S -emit-llvm can help with getting the “rough” LLVM from some C code.

1reaction
stuartarchibaldcommented, Dec 2, 2020

Something like this might work:

import numba
from numba.extending import intrinsic

@intrinsic
def void_ptrptr(tyctx, arg):
    sig = numba.types.CPointer(numba.types.voidptr)(numba.types.intp)
    def codegen(cgctx, builder, sig, llargs):
        llvalue, = llargs
        argty, = sig.args
        retty = sig.return_type

        llretty = cgctx.get_value_type(retty)
        retptr = builder.alloca(llretty)
        valbuf = builder.alloca(llvalue.type)
        builder.store(llvalue, valbuf)
        valbuf_val = builder.load(valbuf)
        valbuf_asptr = builder.inttoptr(valbuf_val, llretty)
        builder.store(valbuf_asptr, retptr)
        return builder.load(retptr)
    return sig, codegen

import ctypes
libm = ctypes.CDLL('libm.so')
ct_cos = libm.cos
ct_cos.restype = ctypes.c_double
# this is a lie, but acts as a sink such that `bar` isn't optimised away
ct_cos.argtypes = (ctypes.POINTER(ctypes.c_void_p),)


@numba.cfunc("void()")
def foo():
  bar = void_ptrptr(0)
  ct_cos(bar)

print(foo.inspect_llvm())
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to assign NULL to a pointer - Stack Overflow
I want to call my_function to set my struct to a known state because i'm declaring my struct inside main so it becomes...
Read more >
How to Fix and Avoid NullPointerException in Java - Rollbar
The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified.
Read more >
Null In Java: Understanding the Basics - Upwork
Learn all about how and when to apply null in Java. Through the best examples, understand the common situations and caveats of using...
Read more >
Null in Python: Understanding Python's NoneType Object
As the null in Python, None is not defined to be 0 or any other value. In Python, None is an object and...
Read more >
Strict mode - JavaScript - MDN Web Docs
There are three ways to fail a property assignment: ... Strict mode forbids a 0 -prefixed octal literal or octal escape sequence.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found