Forum Archive

objc_util ObjCBlock. __call__ method makes reference to unassigned variable (self.func)

Cethric

@omz I was trying to implement a block function for GLKTextureLoader.textureWithContentsOfFile_options_queue_completionHandler_ however it keeps crashing every time I test it (this could be more my fault in code than anything else but.) and in an attempt to test each part individually I noticed that in the ObjCBlock class the __call__ method references a unassigned value.

def ___call__(self, *args):
    return self.func(*args)

However self.func is never actually set.

omz

Thanks, didn't notice that. The __call__ implementation is basically just there for completion's sake because one might expect an ObjCBlock object to be callable...

Cethric

Welcome and ok.
Will do a bit more research into why the crash is occurring then.
Current code

class CBlock(ObjCBlock):
    def __init__(self, func, restype=None, argtypes=None):
        super(CBlock, self).__init__(func, restype=None, argtypes=None)
        self.func = func

type_encodings['@?'] = CBlock

default_opt = {
    'GLKTextureLoaderApplyPremultiplication': 0,
    'GLKTextureLoaderGenerateMipmaps': 0,
    'GLKTextureLoaderOriginBottomLeft': 0,
    'GLKTextureLoaderGrayscaleAsAlpha': 0,
    'GLKTextureLoaderSRGB': 0
}

DEFAULTS = ns(default_opt)


class GLKTextureLoader:
    def __init__(self, sharegroup=None):
        if sharegroup is None:
            sharegroup = currentContext().sharegroup
            print sharegroup
        tl = ObjCClass('GLKTextureLoader')
        self._loader = tl.alloc().initWithSharegroup_(sharegroup._sharegroup)

    def textureFromFile(self, path):
        callback = CBlock(GLKTextureLoaderCallback, restype=None, argtypes=[ctypes.c_void_p, ctypes.c_void_p])
        print dir(self._loader)
        print DEFAULTS
        # self._loader.textureWithContentsOfFile_options_queue_completionHandler_(path, DEFAULTS, None, callback)

Note currentContext() is just a wrapper for EAGLContext.currentContext() where EAGLContext has not been allocated or initialised.