New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Creating callback python functions for PyObjC


Post Reply New Thread Subscribe

 
Thread Tools
god0fgod

 
god0fgod's Avatar
 
Member Since: Jun 24, 2008
Posts: 196
god0fgod is an unknown at this point
Mac Specs: Macbook White 2.13Ghz 160GB 10.6.4 (Buggy Version :() Snow Leopard

god0fgod is offline
Hello.

I'm trying to use an Objective-C class made in Python to call python functions but Objective-C can't call the method which calls the python function.

Here's the framework code which the Objective-C code:

Code:
//  Created by Matthew Mitchell on 04/07/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface Game : NSObject {
    id current_pyfunc;
}
-(void) addPyFunc: (id) pyfunc;
-(void) callPyFunc;
@end

//
//  scalelib.m
//  Scalelib Cocoa Framework
//
//  Created by Matthew Mitchell on 04/07/2010.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Game.h"


@implementation Game
-(void) addPyFunc: (id) pyfunc{
    current_pyfunc = pyfunc;
}
-(void) callPyFunc{
    [current_pyfunc call]; //Segmentation fault. Method doesn't exist for some reason.
}
@end
ere is the python script which loads the framework and tests the use of callbacks with failure.

Code:
#!/usr/bin/env python2.3
from objc import *
import os,sys
loadBundle("Scalelib Cocoa Framework",globals(),os.path.dirname(sys.argv[0]) + "/Scalelib Cocoa Framework/build/Release/Scalelib Cocoa Framework.framework/")
class PythonCallback(NSObject):
    def setCallback_withArgs_(self, python_function,args): #Python initialisation of class, add the callback function and arguments
        self.python_function = python_function
        self.args = args
        return self
    def call(self): #Used by Objective-C to call python function
        self.python_function(*self.args)
def create_callback(function,args):
    return PythonCallback.alloc().init().setCallback_withArgs_(function,args)
def square(num):
    print num**2
instance = Game.alloc().init()
callback = create_callback(square,[3])
callback.call()
instance.addPyFunc_(create_callback(square,[5]))
instance.callPyFunc()
I get the output:

9
Segmentation fault

The segmentation fault is because the call method made in python doesn't exist apparently. So how to I make it exist for Objective-C?

Even if the code did work it would be useless but I'm only testing things at the moment. Once I have the callbacks working, I'll be able to make my library for python.

Thank you for any help.
QUOTE Thanks

Post Reply New Thread Subscribe


« Most efficient way of repeatedly drawing attributed text to view | freeBSD version influencing mac OS X networking features »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Client-side cookies on Python in Mac OSX Hikaru79 OS X - Development and Darwin 0 07-12-2007 10:25 PM

All times are GMT -4. The time now is 06:47 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?