Pythonのお勉強 Part3000.12
■ このスレッドは過去ログ倉庫に格納されています
0034デフォルトの名無しさん
2006/08/30(水) 05:37:00sisinfo.pyのソースをすこし眺めてみたんだけど、
これ本当にちゃんと動くプログラムなのかな?
例えば sisreader.py にこんなコードがある。
def readSignedBytes(self, numBytes) :
buf = self.readPlainBytes(numBytes)
if len(buf) < numBytes :
return []
format = ""
for i in range(numBytes) :
format += "b"
return struct.unpack(format, buf)
def readBytesAsInt(self, numBytes) :
result = 0
bytes = self.readSignedBytes(numBytes)
if len(bytes) == numBytes :
for i in range(numBytes) :
result |= bytes[i] << (i*8)
return result
普通、2バイトを符号付きの16ビット整数として読む場合、
>>> struct.unpack("h", "\x88\x88")[0]
-30584
となるんだけど、引用したコードで readBytesAsInt(2) とやると
>>> bytes = struct.unpack("bb", "\x88\x88")
>>> (bytes[0]) | (bytes[1] << 8)
-120
>>>
というおかしな結果になる。展開できるファイルの方が少ないのでは?
sisfields.py の SISField#readFieldLength() も63ビットの符号なし整数を読む場合の
処理が仕様書(google://softwareinstallsis.pdf)と合致してない希ガス。
あと、format = "b" * numBytes だよなぁとか、むしろ
struct.unpack("%db" % numbytes, buf) だよなぁとか。Python 歴が短い印象。
■ このスレッドは過去ログ倉庫に格納されています