Note that there are some explanatory texts on larger screens.

plurals
  1. POPyObjC and method_exchangeImplementations: crash. correct usage?
    primarykey
    data
    text
    <p>I'm using PyObjC. PyObjC doesn't provide an interface to <code>method_exchangeImplementations</code> so I was trying to use the function via ctypes. I was trying to overwrite <code>windowShouldClose:</code> from some window controller class.</p> <p>My code:</p> <pre><code>import objc BrowserWindowController = objc.lookUpClass("BrowserWindowController") class BrowserWindowController(objc.Category(BrowserWindowController)): def myWindowShouldClose_(self, sender): print "myWindowShouldClose", self, sender return self.myWindowShouldClose_(sender) from ctypes import * capi = pythonapi # id objc_getClass(const char *name) capi.objc_getClass.restype = c_void_p capi.objc_getClass.argtypes = [c_char_p] # SEL sel_registerName(const char *str) capi.sel_registerName.restype = c_void_p capi.sel_registerName.argtypes = [c_char_p] def capi_get_selector(name): return c_void_p(capi.sel_registerName(name)) # Method class_getInstanceMethod(Class aClass, SEL aSelector) # Will also search superclass for implementations. capi.class_getInstanceMethod.restype = c_void_p capi.class_getInstanceMethod.argtypes = [c_void_p, c_void_p] # void method_exchangeImplementations(Method m1, Method m2) capi.method_exchangeImplementations.restype = None capi.method_exchangeImplementations.argtypes = [c_void_p, c_void_p] def hook_into_close(): clazz = capi.objc_getClass("BrowserWindowController") origClose = capi.class_getInstanceMethod(clazz, capi_get_selector("windowShouldClose:")) newClose = capi.class_getInstanceMethod(clazz, capi_get_selector("myWindowShouldClose:")) capi.method_exchangeImplementations(origClose, newClose) </code></pre> <p>This crashes. With some strange backtrace in <code>[NSWindow _close]</code>.</p> <p>Is the code basically right?</p> <p>What is the problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload