这种方法是否有可能返回-1?
def tree_value(self, team, x, y):
if team == NO_TEAM or self.team_sees_tree[team, x, y]:
return int(self.tree[x, y])
else:
return TREE_NONE
设置:
import numpypy as np
TREE_NONE = 255
NO_TEAM = -1
self.tree= np.empty((dim, dim), np.uint8)
for i in xrange(dim):
for j in xrange(dim):
self.tree[i, j] = TREE_NONE
self.team_sees_tree = np.zeros((16, dim, dim), np.bool_)
在正常操作期间,树值设置为 0-15(团队编号)或 TREE_NONE (255)。即使它们被意外设置为 -1 (NO_TEAM),也应该等同于 255 作为 uint8。
调用获得 -1 的 tree_value():
data = [(self.Tiles.tree_value(COLOR_NONE, x, y),
self.Tiles.mountain_value(x, y)) for (x, y) in coords]
str = ""
try:
for (t, m) in data:
str += chr(t) + chr(m)
except ValueError as e:
print data
# [(255, 0), (255, 0), (255, 0), (255, 0), (255, 0), (-1, 0), (255, 0)]
print e
# ValueError: character code not in range(256)
无法准确地重现此问题,但我的服务器大约每小时会遇到两次。在 Ubuntu 12.04 32 位上运行 PyPy 最近的夜间构建 (pypy-c-jit-64927-e0ba4acfd3c2-linux.tar.bz2)。