From 18c4df549c051cbed38b56365785695c7d1adc9a Mon Sep 17 00:00:00 2001 From: David Arnold Date: Mon, 26 Nov 2018 16:46:08 +1100 Subject: [PATCH] Save raw_len across calls to get_message(). Fixes #16. --- simplefix/parser.py | 17 ++++++++++------- test/test_parser.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/simplefix/parser.py b/simplefix/parser.py index edf5d11..54bebf7 100644 --- a/simplefix/parser.py +++ b/simplefix/parser.py @@ -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): @@ -98,6 +101,7 @@ def reset(self): self.buf = b"" self.pairs = [] + self.raw_len = 0 return def append_buffer(self, buf): @@ -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): @@ -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: @@ -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 diff --git a/test/test_parser.py b/test/test_parser.py index 2f9f84d..2df4b11 100644 --- a/test/test_parser.py +++ b/test/test_parser.py @@ -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=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\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()