PyObjC and returning 'out' parameters (i.e. NSError **)

蓝咒 提交于 2019-11-29 15:30:02

This question's old, so I don't know if you've sorted it out by now, but you haven't answered it, and I was fussing with getting out parameters from Obj-C to Python a short while ago. There are a few things that you should definitely try/look into:

First and simplest is: when you make that stub in Objective-C with the implementation in Python, you've got to specify that the (NSError **) is an out parameter:

@interface MyObject : NSObject
- (BOOL)testMethod:(out NSError **)error;
@end

I've used this successfully to call a custom Obj-C method from Python. I believe the Python side doesn't get the right metadata otherwise.

There's also adding a signature decorator to your Python method definition. You can specify that one of the parameters is an out parameter in the sig. This PyObjC doc gives details.

@objc.signature('@@:io^@')

The other thing is (and you may have figured this out on your own by now) that if you don't want to do the Objective-C stub, you may be able to generate your own metadata and stick a .bridgesupport file into your project. The only thing (the big thing!) that I'm not sure of is how to make sure that this file actually gets read. The metadata itself is really easy to write -- Apple supplies a command-line utility called gen_bridge_metadata, and you could do it by hand (look at /System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC/AppKit/PyObjC.bridgesupport). Theres a man page for that utility, and man 5 BridgeSupport is also informative. Another Apple doc you should read is: Generating Framework Metadata.

UPDATE for future readers: I've found the functions objc.registerMetaDataForSelector and objc.parseBridgeSupport, both of which allow you to add metadata for your methods, using either Python dicts (the former function) or the XML format described in the BridgeSupport man page (the latter). Examples of using registerMetaData... are available in the pyobjc source at: pyobjc/pyobjc-core/PyObjCTest/test_metadata*, which I discovered via this pyobjc-dev mailing list thread.

I've tried this and all I get is an exception when calling from ObjC (: depythonifying 'char', got 'tuple').

PyObjC generally passes the error back as the second element of a tuple.

Something like:

response, error = object.someMethod_error_(foo) # leave off the error
if not response:
    # process error

Saw your update...

You are either going to have to dive into the metadata bridge and figure out how to apply some metadata at runtime to your implementation to make it work.

Or, possibly much easier, is to subclass from a compiled Objective-C stub class.

I.e. try this:

@interface AbstractFoo : NSObject
@end

@implementation AbstractFoo
- (BOOL) myMethod: (int) arg error: (NSError **) anError;
{
    return YES;
}
@end

Then, you should be able to subclass AbstractFoo from the Python side and it might "just work".

Maybe.

(Sorry -- been a while since I've been this deep in PyObjC)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!