Skip to content

Commit a56aeea

Browse files
committed
Prevent multiple UNA segments in EDIFACT parsing
fixes #73
1 parent 7fa2ad8 commit a56aeea

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pydifact/parser.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from collections.abc import Iterator
2424

25+
from pydifact.exceptions import EDISyntaxError
2526
from pydifact.tokenizer import Tokenizer
2627
from pydifact.token import Token
2728
from pydifact.segments import Element, Elements, Segment, SegmentFactory
@@ -225,8 +226,11 @@ def convert_tokens_to_segments(
225226

226227
for segment in segments:
227228
name = segment.pop(0)
229+
if with_una and name == "UNA":
230+
# we found another UNA segment.
231+
# This is not in the specs, so raise an error
232+
raise EDISyntaxError("There are not multiple UNA segments allowed.")
228233
if name == "UNB":
229234
self.version = int(segment[0][1])
230235
print("found edifact version", self.version)
231236
yield self.factory.create_segment(name, *segment, version=self.version)
232-

tests/test_double_message.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from pydifact.exceptions import EDISyntaxError
4+
from pydifact.segmentcollection import Interchange
5+
from tests.test_input_output import path
6+
7+
8+
def test_message_with_2_UNA_segments():
9+
with open(f"{path}/wikipedia_de.edi") as f:
10+
content = f.read()
11+
12+
# doouble the message (2 UNA headers in one string!!)
13+
content = content + content
14+
15+
with pytest.raises(EDISyntaxError):
16+
Interchange.from_str(content)

0 commit comments

Comments
 (0)