I am in the process of writing a lightweight interface in Objective-C that is capable of executing python scripts and passing data back and forth between Objective-C and Python. I looked at PyObjC and ObjP, and not what I'm looking for (and since I'm developing for iOS <= 6.0.1 PyObjC will not compile to make heavy use of NSMapTable).
So basically, I created a Python type in Objective-C called "ObjC_Class" (declaration, no?), And I want this Python object to be almost similar to the ObjC object. So I decided to redefine the class function __ getattr __ so that I could access arbitrary methods and properties of the ObjC equivalent of this class.
Here is the code:
static PyObject * ObjC_Class_getattro(ObjC_Class *self, PyObject *name)
{
NSString *attrName = [NSString stringWithCString:PyString_AsString(name) encoding:NSUTF8StringEncoding];
NSLog(@"Calling Object: %@", self->object);
if([self->object respondsToSelector:NSSelectorFromString(attrName)])
{
methodName = attrName;
PyMethodDef methd = {[attrName UTF8String],ObjC_Class_msg_send,METH_VARARGS,[attrName UTF8String]};
PyObject* pyName = PyString_FromString(methd.ml_name);
PyObject* pyfoo = PyCFunction_NewEx(&methd,(PyObject*)self,pyName);
Py_DECREF(name);
return pyfoo;
}
else
{
return name;
}
}
, :
# 1
from ObjC import ObjC_Class
new = ObjC_Class('UIView')
new.backgroundColor("asdf", 42, "some_random_string")
:
# 2
from ObjC import ObjC_Class
new = ObjC_Class('UIView')
moreStuff = "some_random_string"
new.backgroundColor("asdf", 42, moreStuff)
, :
error: address doesn't contain a section that points to a section in a object file
, , , , .
ObjC_Class_msg_send:
static PyObject* ObjC_Class_msg_send(PyObject *self, PyObject *args)
{
NSLog(@"Entering...");
NSMutableArray *tmp = [[NSMutableArray alloc] init];
for(int i=0;i<PyTuple_Size(args);i++)
{
[tmp addObject:Py_to_ObjC(PyTuple_GetItem(args, i))];
}
NSLog(@"Object: %@, Method Name: %@, args: %@", ((ObjC_Class*)self)->object, methodName, tmp);
methodName = @"";
return PyString_FromString("Did it actually work!?!?!");
}
, python , , ObjC_Class_msg_send ( , ObjC_Class_getattro ).
(, , ... , , )
-, : My ObjC_Class "object", "id", Objective-C, Python...
: Python 2.6 (.3?),
UPDATE
, fpFunc fpFunc ... , ( + ...):
static PyObject * ObjC_Class_getattro(ObjC_Class *self, PyObject *name)
{
NSString *attrName = [NSString stringWithCString:PyString_AsString(name) encoding:NSUTF8StringEncoding];
NSLog(@"Calling Object: %@", self->object);
if([self->object respondsToSelector:NSSelectorFromString(attrName)])
{
methodName = attrName;
PyMethodDef methd = {[attrName UTF8String],ObjC_Class_msg_send,METH_VARARGS,[attrName UTF8String]};
PyObject* pyName = PyString_FromString(methd.ml_name);
PyObject* pyfoo = PyCFunction_NewEx(&methd,(PyObject*)self,pyName);
Py_DECREF(name);
return pyfoo;
}
else
{
return name;
}
}
... Python ( python , .. № 2 ):
SystemError: Objects/methodobject.c:120: bad argument to internal function
:( ...