Skip to content

Commit

Permalink
Merge pull request #17 from da4089/16-fixparser-does-not-correctly-in…
Browse files Browse the repository at this point in the history
…terpret-cme-execution-reports

Save raw_len across calls to get_message().  Fixes #16.
  • Loading branch information
da4089 authored Nov 26, 2018
2 parents eaf8656 + 18c4df5 commit d99b31e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
17 changes: 10 additions & 7 deletions simplefix/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def __init__(self):

# Copy raw field data tags.
self.raw_data_tags = RAW_DATA_TAGS[:]

# Parsed length of data field.
self.raw_len = 0
return

def add_raw(self, length_tag, value_tag):
Expand Down Expand Up @@ -98,6 +101,7 @@ def reset(self):

self.buf = b""
self.pairs = []
self.raw_len = 0
return

def append_buffer(self, buf):
Expand Down Expand Up @@ -135,7 +139,6 @@ def get_message(self):
start = 0
point = 0
in_tag = True
raw_len = 0
tag = 0

while point < len(self.buf):
Expand All @@ -144,15 +147,15 @@ def get_message(self):
point += 1

tag = int(tag_string)
if tag in self.raw_data_tags and raw_len > 0:
if raw_len > len(self.buf) - point:
if tag in self.raw_data_tags and self.raw_len > 0:
if self.raw_len > len(self.buf) - point:
break

value = self.buf[point:point + raw_len]
value = self.buf[point:point + self.raw_len]
self.pairs.append((tag, value))
self.buf = self.buf[point + raw_len + 1:]
self.buf = self.buf[point + self.raw_len + 1:]
point = 0
raw_len = 0
self.raw_len = 0
start = point

else:
Expand All @@ -168,7 +171,7 @@ def get_message(self):
in_tag = True

if tag in self.raw_len_tags:
raw_len = int(value)
self.raw_len = int(value)

point += 1

Expand Down
39 changes: 39 additions & 0 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,45 @@ def test_embedded_equals_96_no_95(self):
self.assertIsNotNone(msg)
return

def test_simplefix_on_split_execution_report(self):
"""Test parsing with length and data appended separately."""

part1 = b'8=FIX.4.2\x019=606\x0135=n\x0134=18\x01369=XX\x01' \
b'52=XXXXXXXX-XX:XX:XX.XXX\x0143=Y\x0149=CME\x0150=G\x01' \
b'56=XXXXXXX\x0157=NULL\x01122=XXXXXXXX-XX:XX:XX.XXX\x01' \
b'143=XX\x01212=481\x01213=<RTRF>8=FIX.4'

part2 = b'.2\x019=XXX\x0135=8\x0134=2087\x01369=2122\x01' \
b'52=XXXXXXXX-XX:XX:XX.XXX\x0149=CME\x0150=G\x01' \
b'56=XXXXXXX\x0157=XXXXXXXXXX\x01143=XXXXX\x011=XXXXXXXX\x01' \
b'6=X\x0111=XXXXX\x0114=XX\x0117=XXXXXXXXXXXXXXXXXXXXXXX\x01' \
b'20=X\x0131=XXXXXX\x0132=X\x0137=XXXXXXXXXXXX\x0138=XXX\x01' \
b'39=X\x0140=X\x0141=X\x0144=XXXXXX\x0148=XXXXXX\x0154=X\x01' \
b'55=XX\x0159=X\x0160=XXXXXXXX-XX:XX:XX.XXX\x01' \
b'75=XXXXXXXX\x01107=XXXX\x01150=X\x01151=XX\x01167=FUT\x01' \
b'337=TRADE\x01375=CME000A\x01432=XXXXXXXX\x01' \
b'442=1\x01527=XXXXXXXXXXXXXXXXXXXXXXXX\x011028=Y\x01' \
b'1057=Y\x015979=XXXXXXXXXXXXXXXXXXX\x019717=XXXXX\x01' \
b'37711=XXXXXX\x0110=171\x01</RTRF>\x0110=169\x01'

# Test append / append / parse
parser = FixParser()
parser.append_buffer(part1)
parser.append_buffer(part2)
msg = parser.get_message()
self.assertEqual(msg.get(10), b'169')

# Test append / parse / append / parse
parser = FixParser()
parser.append_buffer(part1)
msg = parser.get_message()
self.assertEqual(msg, None)

parser.append_buffer(part2)
msg = parser.get_message()
checksum = msg.get(10)
self.assertEqual(checksum, b'169')


if __name__ == "__main__":
unittest.main()

0 comments on commit d99b31e

Please sign in to comment.