一個實際的錯誤案例(Django controller)
def parse_weburl(weburl):
index = "http%253A%252F%252F"
if index >= 0:
weburl = weburl[:index]
# fix the issue for now news
index = weburl.find('http%3A%2F%2')
if index >= 0:
weburl = weburl[:index]
weburl = force_decode(weburl)
return weburl
會造成以下error
2012-02-15 18:38:15.935
slice indices must be integers or None or have an __index__ method
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 701, in __call__
handler.get(*groups)
File "/base/data/home/apps/tagtoov1/1.356847196571547955/libs/handlers.py", line 332, in get
self.get_handler(*args, **atts)
File "/base/data/home/apps/tagtoov1/1.356847196571547955/views/ajaxs.py", line 506, in get_handler
referer = parse_weburl(referer)
File "/base/data/home/apps/tagtoov1/1.356847196571547955/views/ajaxs.py", line 16, in parse_weburl
weburl = weburl[:index]
TypeError: slice indices must be integers or None or have an __index__ method
改成下面的樣式就不會error
def parse_weburl(weburl):
index = weburl.find("http%253A%252F%252F")
if index >= 0:
weburl = weburl[:index]
# fix the issue for now news
index = weburl.find('http%3A%2F%2')
if index >= 0:
weburl = weburl[:index]
weburl = force_decode(weburl)
return weburl
注意到index 從舊版的"字串"變成"整數"
留言
張貼留言