PyObjC和返回'out'参数(即NSError **) [英] PyObjC and returning 'out' parameters (i.e. NSError **)

查看:191
本文介绍了PyObjC和返回'out'参数(即NSError **)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个ObjC协议作为PyObjC类的混合。

I'm implementing an ObjC protocol as a mix-in for a PyObjC class.

这个协议定义了一个'out'参数。

This protocol defines an 'out' parameter.

我无法找到任何有关如何实现一个ObjC协议定义这个行为的Python类的好的文档。

I am unable to find any good documentation on how a Python class which implements an ObjC protocol defining this is to behave.

ve找到此邮件列表主题但是建议在那里没有工作。他们说返回一个Python列表,方法的返回值为第一个项目,out参数为第二个。

I've found this mailing list thread but the suggestion in there does not work. They say to return a Python list with the method's return value as the first item and the out parameter as the second.

我试过这个,我得到的是一个当从ObjC(< type'exceptions.ValueError'> ;: depythonifying'char',got'tuple')调用时发生异常。

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

看起来PyObjC严格遵守ObjC协议在解析方法参数,这是很好,但它不帮助我试图修改一个输出参数。

It seems PyObjC strictly adheres to the ObjC protocol in depythonifying method arguments, which is nice but it doesn't help me trying to modify an out parameter.

这是ObjC协议:

#import <Foundation/Foundation.h>

@protocol TestProtocol <NSObject>

- (BOOL)testMethod:(NSError **)error;

@end

这是实现此协议的Python类: p>

This is the Python class implementing this protocol:

from Foundation import *
import objc

NSObject = objc.lookUpClass("NSObject")
TestProtocol = objc.protocolNamed("TestProtocol")

class TestClass(NSObject, TestProtocol):

    def testMethod_(self, error):
        return True, None


$ b $ p问题:如何在Python中返回ObjC out参数?

QUESTION: How do I return an ObjC out parameter in Python?

推荐答案

这个问题很老,所以我不知道你现在是否已经排序,但你还没有回答,我很想知道从Obj-C到Python的参数。有一些事情,你应该肯定尝试/研究:

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:

首先最简单的是:当你使用Python中的实现在Objective-C中创建stub时,必须指定(NSError **) out 参数:

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



我已经成功使用这个函数来调用一个自定义Obj-C方法蟒蛇。我相信Python端没有得到正确的元数据。

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.

还有一个签名 Python方法定义。您可以指定其中一个参数是sig中的out参数。此 PyObjC doc 提供了详细信息。

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^@')

另一件事情是(如果你不想使用Objective-C存根,你可以自己想出来),你可以生成你自己的元数据并将.bridgesupport文件粘贴到您的项目中。唯一的事情(大的事情!),我不知道是如何确保这个文件实际上读取。元数据本身真的很容易编写 - 苹果提供了一个名为gen_bridge_metadata的命令行实用程序,你可以手工完成(看看/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python) /PyObjC/AppKit/PyObjC.bridgesupport)。是手册页 ,以及 man 5 BridgeSupport 也是信息。您应该阅读的另一个Apple文档是:生成框架元数据

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 :我找到了 objc.registerMetaDataForSelector objc.parseBridgeSupport ,这两个都允许您为方法添加元数据,使用Python dicts(前一个函数)或XML格式BridgeSupport手册页(后者)。使用 registerMetaData ... 的示例在pyobjc源中可用: pyobjc / pyobjc-core / PyObjCTest / test_metadata * ,我通过这个pyobjc-dev邮件列表发现主题

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.

这篇关于PyObjC和返回'out'参数(即NSError **)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆