2013-10-15 25 views
0

我有一個python腳本,我可以在機器上成功運行jenkins用戶。當我使用jenkins web前端配置作業在機器上運行時,我將它作爲要運行的作業的python腳本。出於某種原因,我得到的是沒有意義的我在Python的情況下最奇怪的KeyError異常,所以也許有人在那裏已經看到這種事情之前:Python從Jenkins運行時會拋出奇怪的KeyError buildbot

log_name = '-'.join([status_map[test.status], test.config[0], test.config[1], test.group_raw+'.'+test.title])+'.log' 
KeyError: 'error: failed to open driver pseudo terminal : Device not configuredFAILURE' 

多數民衆贊成追加到末尾的「失敗」的錯誤信息實際上是test.status的值,該錯誤被引發的行中的status_map中被查找。但是,據我可以告訴代碼是健全的,應該沒有錯誤,當我通過命令行自己運行它時,腳本運行良好。問題是什麼?

回答

0

你有一個變量命名爲test.status,其值是error: failed to open driver pseudo terminal : Device not configuredFAILURE

但字典status_map不包含任何這樣的鍵,因此accesing這個字典與test.status鍵,使KeyError異常。

status_map[test.status] 

KeyError: 'error: failed to open driver pseudo terminal : Device not configuredFAILURE' 
0
log_name = '-'.join([status_map[test.status], test.config[0], test.config[1], test.group_raw+'.'+test.title])+'.log' 
KeyError: 'error: failed to open driver pseudo terminal : Device not configuredFAILURE' 

你需要先檢查一下字典的關鍵存在與否,首先檢查test.status鍵是否使用status_map.has_key(test.status)在status_map存在

if status_map.has_key(test.status): 
    log_name = '-'.join([status_map[test.status], test.config[0], test.config[1], str(test.group_raw)+'.'+test.title])+'.log'