我有一個Python供電的DSL,我通過exec()
執行。此DSL包含通過CFFI的本地函數調用。如何解決這個tkinter/exec()/ cffi組合中的堆棧溢出問題?
調用一個只有2 C調用深度的本機函數時,堆棧溢出(所以你使它不可執行!)崩潰,在每個C函數中只有少數uint16_t
被分配到棧中。 Python應用程序是一個tkinter
GUI,它通過定時器(master.after(1000, self.tick)
)事件調用DSL,這可能會佔用堆棧本身的很大一部分。
這裏沒有遞歸調用。
OS X 10.12.3,巨蟒3.6.0rc1(v3.6.0rc1:29a273eee9a5,2016年12月6日,16點24分13秒),CFFI 1.9.1
我所知道的resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
,但它需要超級用戶權限。我相信這是不需要的,因爲只有兩個函數調用剩餘的堆棧是不正常的。
CFFI或exec()是否可以限制被調用者的堆棧大小?
功能從DSL被稱爲:
ffi_builder.cdef('''
//...
int FooNode_SetProperty(struct FooNode *pThis, const char *szPropertyName, int nValue);
''')
def set_channel(node, channel):
node.SetProperty(b'channel', channel)
exec
調用代碼:
self._globals = {
'__builtins__': __builtins__,
# https://docs.python.org/3/library/functions.html#eval "If the globals dictionary is present and lacks
# ‘__builtins__’, the current globals are copied into globals before expression is parsed."
'run': {
'duration': 60 * MICROS,
'success': None
},
'set_channel': set_channel,
'turn_off': turn_off,
'turn_on': turn_on,
'finish': finish,
# 6 more functions here
}
exec(event_text, self._globals, {})
蘋果報告片:
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Application Specific Information:
[35633] stack overflow
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x00000001003bfdd6 __pthread_kill + 10
1 libsystem_pthread.dylib 0x00007fffe03dc787 pthread_kill + 90
2 libsystem_c.dylib 0x00007fffe02564bb __abort + 140
3 libsystem_c.dylib 0x00007fffe0256d7e __stack_chk_fail + 205
4 libmush_real.dylib 0x0000000104c4d714 send_counters_report_request + 532
(此線程真的到此爲止,沒有別的蘋果報告)