1
我想設計一個名爲虛擬天堂的應用程序的機器人,並且爲構建bot而給出的SDK被編譯到共享庫中,因此我必須使用ctypes。Python Ctypes奇怪的行爲
當我使用
import threading
...
from ctypes import CDLL, CFUNCTYPE, c_char_p, c_int, c_void_p
vp = CDLL("libvpsdk.so")
vp.vp_string.restype = c_char_p
vp.vp_int.restype = c_int
...
class bot(threading.Thread):
def initBot(self):
...
instance = vp.vp_create()
...
EventFunc = CFUNCTYPE(None)
event_chat_func = EventFunc(self.event_chat)
vp.vp_event_set(instance, 0, event_chat_func)
...
def event_chat(self):
print "Hello"
...
event_chat被正確調用,並打印 「Hello」
但是當我使用這個
import threading
import chat
...
from ctypes import CDLL, CFUNCTYPE, c_char_p, c_int, c_void_p
vp = CDLL("libvpsdk.so")
vp.vp_string.restype = c_char_p
vp.vp_int.restype = c_int
...
class bot(threading.Thread):
def initBot(self):
...
instance = vp.vp_create()
...
chat.VPSDK(vp, instance)
...
Chat.py:
from ctypes import CFUNCTYPE
...
class VPSDK:
def __init__(self, vp, instance):
EventFunc = CFUNCTYPE(None)
event_chat_func = EventFunc(self.event_chat)
vp.vp_event_set(instance, 0, event_chat_func)
def event_chat(self):
print "Hello"
...
我得到呃ror「非法指令」
我在做什麼錯誤!?我需要使用這個單獨的類,否則我的機器人的其他部分將失去功能。
謝謝,我只是在「chat.py」中創建了全局變量「event_chat_func」,現在它沒有任何問題。 – MetaDark 2010-12-22 14:28:04