Handling blank lines in x12xml_simple.seg, x12file._parse_segment #35
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I've been parsing a bunch of 835 files and found the following error terminating the parsing of many of them:
Traceback: Traceback (most recent call last):
...
File "/usr/local/lib/python2.7/dist-packages/pyx12/x12n_document.py", line 240, in x12n_document
xmldoc.seg(node, seg)
File "/usr/local/lib/python2.7/dist-packages/pyx12/x12xml_simple.py", line 72, in seg
if child_node.usage == 'N' or seg_data.get('%02i' % (i + 1)).is_empty():
AttributeError: 'NoneType' object has no attribute 'usage'
It appears this error is caused by 835 files that have blank lines, which I realize are malformed (out of spec), but my assumption is that the goal is to enable X12 parsing to continue (i.e., not terminate) while logging errors encountered during parsing.
I fixed this error by adding a
child_node is None
clause in thatif
statement, but I see there is anelse
clause at the end that raises anEngineError
, so I'm not sure if this fix aligns with the desired behavior.Once this was fixed, a new
IndexError
was generated byx12file._parse_segment()
, due - I think - to the presence of a blank line between an invalid IEA segment and a valid ISA segment.I fixed this by adding a check for empty loops before checking the preceding loop to generate error messages, but was not sure what error code to use ... and am less confident that my fix in this file is consistent with desired behavior.
FWIW, here is the section of the log file that is generated for one of the files with errant blank lines and a malformed IEA segment:
20150306 16:37:58 pyx12.error_handler ERROR: Line:4383 SEG:1 - Segment
ISA*00 not found. Started at /ISA_LOOP/IEA
20150306 16:37:58 pyx12.error_handler ERROR: No current segment in error_handler. Line:4383 SEG:1 - Segment identifier "
ISA" is invalid
20150306 16:37:58 pyx12.error_handler ERROR: Line:4610 SEG:5 - Segment IEA exceeded max count. Found 2, should have 1
20150306 16:37:58 pyx12.error_handler ERROR: Line:4382 ISA:000 - IEA loop with malformed preceding segment
I thought it might be more useful to submit a pull request - even though one or both fixes may not be acceptable - to help more easily identify the problem areas. I suspect that if these errors are to be caught, a more extensive set of error checks will need to be added to one or both files.
If it is more helpful to simply report errors than trying to fix them, let me know, and I'll switch tactics.