mbj4668 / pyang

An extensible YANG validator and converter in python
ISC License
530 stars 343 forks source link

Traceback when yang file is corrupt #784

Closed jhaas-pfrc closed 2 years ago

jhaas-pfrc commented 2 years ago

As part of installing RFC 9127 BFD yang files into the IANA yang-parameters site, there was a corruption of two of the files: iana-bfd-types@2021-10-21.yang ietf-bfd@2021-10-21.yang

(This has been brought to IANA's attention and they'll fix it soon.)

When pyang attempts to use these files, a traceback is generated: Traceback (most recent call last): File "/usr/local/bin/pyang", line 546, in run() File "/usr/local/bin/pyang", line 465, in run if (ctx.implicit_errors is False and AttributeError: 'NoneType' object has no attribute 'arg'

Looking at the contents of the objects in the code when this error occurs in pdb let me figure out what the corrupt file was. It'd be better if pyang emitted the broken file name without traceback in the face of such issues.

jhaas-pfrc commented 2 years ago

Note that this was originally observed in 2.50 and again observed in 2.52.

mbj4668 commented 2 years ago

I guess you were using pyang -f tree or some other plugin?

Anyway, I don't think we can print the filename in this case, since the problem is the code that tries to print the error. I will fix this specific bug.

jhaas-pfrc commented 2 years ago

To give insight into the use pattern, this was found when the following scripts were being used.

https://github.com/mjethanandani/ietf-bgp-yang/blob/v13/src/validate-and-gen-trees.sh

I haven't spent any serious time looking at how pyang leverages stderr vs. stdout, but emitting the error to stderr rather than needing to diagnose using the debugger would be a path forward.

jhaas-pfrc commented 2 years ago

epos.ref had the relevant information. We just didn't get there because of the rest of the conditionals testing deeper than could be satisfied given the issue.

root@47a4323953a4:/git/ietf-bgp-model/draft# pyang --ietf --strict --canonical -p ../../yang-parameters -p ../bin/submodules -p ../bin -f tree --tree-depth=3 --max-line-length=72 --tree-line-length=69 ../bin/ietf-bgp\@2021-12-29.yang

/usr/local/bin/pyang(465)run() -> if (ctx.implicit_errors is False and (Pdb) l 460
461 for epos, etag, eargs in ctx.errors: 462 if etag in o.ignore_error_tags: 463 continue 464 import pdb; pdb.set_trace() 465 -> if (ctx.implicit_errors is False and 466 epos.top.arg not in modulenames and 467 (not hasattr(epos.top, 'i_modulename') or 468 epos.top.i_modulename not in modulenames) and 469 epos.ref not in filenames): 470 # this module was added implicitly (by import); skip this error (Pdb) p ctx.implict_errors *** AttributeError: 'Context' object has no attribute 'implict_errors' (Pdb) p epos.top None (Pdb) dir(epos) ['class', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge', 'getattribute', 'gt', 'hash', 'init', 'init_subclass', 'le', 'lt', 'module', 'ne', 'new', 'reduce', 'reduce_ex', 'repr', 'setattr', 'sizeof', 'slotnames', 'slots', 'str', 'subclasshook', 'label', 'line', 'ref', 'top', 'uses_pos'] (Pdb) p epos.label <bound method Position.label of <pyang.error.Position object at 0x7f1150093a80>> (Pdb) p epos.line 1 (Pdb) p epos.ref '../../yang-parameters/iana-bfd-types@2021-10-21.yang'