djc / rnc2rng

RELAX NG Compact to regular syntax conversion library
MIT License
16 stars 13 forks source link

loads() is broken #10

Closed dargueta-work closed 7 years ago

dargueta-work commented 7 years ago

When using rnc2rng in lxml.etree.RelaxNG.from_rnc_string() as described here, we get a crash with the following stack trace:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/dargueta/git/clinicalfiles/setup.py", line 107, in <module>
        compile_all()
      File "/Users/dargueta/git/clinicalfiles/setup.py", line 78, in compile_all
        fileformat_xsd = xml.RelaxNG.from_rnc_string(rnc_fd.read())
      File "src/lxml/relaxng.pxi", line 150, in lxml.etree.RelaxNG.from_rnc_string (src/lxml/lxml.etree.c:195588)
      File "/Users/dargueta/.pyenv/versions/3.5.2/envs/clinical_files_35/lib/python3.5/site-packages/rnc2rng/__init__.py", line 7, in loads
        return parser.parse(src)
      File "/Users/dargueta/.pyenv/versions/3.5.2/envs/clinical_files_35/lib/python3.5/site-packages/rnc2rng/parser.py", line 529, in parse
        return parser.parse(lex(src), state=State(fn, src))
    UnboundLocalError: local variable 'fn' referenced before assignment

The problem is in parser.parse(), specifically here. If parse() is only given source code and no file, there is no else clause to set fn. We can fix this like so:

    elif f is not None:
        fn, src = f.name, f.read()
    else:
        fn = None

State properly handles a null filename (see here) so we know that None is a good default value.

ksharenkov commented 7 years ago

Have you any plans to make release with this fix? with PyPI https://pypi.python.org/pypi/rnc2rng :)

djc commented 7 years ago

Find version 2.5 on a PyPI mirror near you now! 😄

ksharenkov commented 7 years ago

Wow. Thanks. Its crazy fast :)