Re: [Pyobjc-dev] Question on Bridging/Integration
Brought to you by:
ronaldoussoren
From: Ronald O. <ron...@ma...> - 2012-05-23 06:04:05
|
On 21 May, 2012, at 12:54, Vincent wrote: > Hi Ronald, > > thanks for your quick answer! > > The former actually sounds like exactly what I'd need. > Just one more question: Given that my python script is stored inside an NSString property, > what actions/functions would would I need to run before being able to instantiate one of the python clases? > After all the Python would have to have been run before I'd be be able to instantiate one of its subclasses, no? You have to use the Python C API to initialize the interpreter and load a script file that creates the Python class. As an alternative you could create a plugin API and use py2app to create a plugin bundle, as described in <http://pyobjc.sourceforge.net/documentation/pyobjc-core/tutorial_embed/index.html>. > > Would something like this be the right way? > > #import <Python.h> > > … > > @protocol PythonClassProtocol > @optional > - (BOOL)shallRejectObject:(NSObject *)object; > @end > > … > > Py_Initialize(); > PyRun_SimpleString([self.script UTF8String]) > Py_Finalize(); > > id<PythonClassProtocol> pythonObject = [[NSClassFromString(@"PythonClass") alloc] init]; > > for (id<PythonClassProtocol> object in self.objects) { > if ([pythonObject shallRejectObject:object]) { > [self rejectObject:object]; > } > } This should work, but you shouldn't call Py_Finalize. > > Oh and what if the user (me) runs the Python script once, edits it and runs it again? Reloading scripts is a problem when the script defines a Objective-C class because redefining ObjC classes is something the ObjC runtime doesn't like. You could use the Python C API to create a new module for the script and use PyObject_GetAttrString(mod, "functionName") to access the callback functions. This makes it hard to forward ObjC objects to the callback function though. What probably works it another layer of indirection: create two python scripts, one of them contains the class definition with not only the -shallRejectObject: method, but also a method for loading a script. That way you can use the implict conversion of ObjC objects to Python provided by PyObjC, and use the Python class to forward the method calls to Python functions in the script: class PythonClass (NSObject, objc.protocolNamed("PythonClassProtocol")): def loadScript_(self, scriptText): self.symbols = {} exec(scriptText, self.symbols) def shallRejectObject_(self, object): return self.symbols["shallRejectObject")(object) This code is completely untested. > Any conflicts to be expected, given that a class of that name (with potentially different logic, of which I'd obviously want to use the new one) has been created before? > If the latter was the case would I have to extract my filtering routine (not just the filtering method, but the entire code as seen above) into > its own NSTask being re-launched on every execution and talk to my own app via some kind of bridge of my own (Distributed Objects, what not)? That would also work, but has a lot more overhead due to the interprocess communication. Ronald > > Thanks a bunch, > Vincent > > On May 21, 2012, at 8:02 AM, Ronald Oussoren wrote: > >> Using Python from ObjC like this is suboptimal at the moment, mostly because I don't use PyObjC in that way. I'd define an stub class with the right interface in Objective-C with a subclass in Python that implements that interface. That way you can fetch the Python class using objc_lookUpClass (or the Foundation wrapper around that function), create an instance and then call the methods without getting compiler warnings. >> >> To expose instances you have to define an API to fetch those instances. >> >> Ronald > > ------------------------------------------------------------------------------ > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/_______________________________________________ > Pyobjc-dev mailing list > Pyo...@li... > https://lists.sourceforge.net/lists/listinfo/pyobjc-dev |