pyang chokes on UnboundLocalError

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce Korb
    New Member
    • Sep 2010
    • 2

    #1

    pyang chokes on UnboundLocalError

    Does this mean that the pyang code should have the
    "except ImportError:" before the "except libxml2.treeErr or" ? -- The stack follows the code fragment:

    Code:
    def validate_pattern_expr(errors, stmt):
        # check that it's syntactically correct
        try:
            import libxml2
            re = libxml2.regexpCompile(stmt.arg)
            return (re, stmt.pos)
        except libxml2.treeError, v:
            err_add(errors, stmt.pos, 'PATTERN_ERROR', str(v))
            return None
        except ImportError:
            err_add(errors, stmt.pos, 'PATTERN_FAILURE',
                    "Could not import python module libxml2 "
                        "(see http://xmlsoft.org)")
            return None
    And now, the stack trace:
    Code:
    $ pyang -o modv-events.xml -f yin modv-events.yang
    Traceback (most recent call last):
      File "/usr/local/src/pyang-0.9.3/bin/pyang", line 186, in <module>
        run()
      File "/usr/local/src/pyang-0.9.3/bin/pyang", line 139, in run
        module = ctx.add_module(filename, text)
      File "/usr/local/src/pyang-0.9.3/pyang/__init__.py", line 44, in add_module
        module = self._add_module(ref, text, format)
      File "/usr/local/src/pyang-0.9.3/pyang/__init__.py", line 84, in _add_module
        statements.validate_module(self, module)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 76, in validate_module
        iterate(module, phase)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 39, in iterate
        res = f(ctx, stmt)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 141, in <lambda>
        ('import', 'module'):lambda ctx, s: v_import_module(ctx, s),
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 354, in v_import_module
        module = ctx.search_module(i.pos, modulename)
      File "/usr/local/src/pyang-0.9.3/pyang/__init__.py", line 117, in search_module
        module = self._add_module(ref, text, format)
      File "/usr/local/src/pyang-0.9.3/pyang/__init__.py", line 84, in _add_module
        statements.validate_module(self, module)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 76, in validate_module
        iterate(module, phase)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 71, in iterate
        iterate(s, phase)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 39, in iterate
        res = f(ctx, stmt)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 144, in <lambda>
        ('type', 'typedef'):lambda ctx, s: v_type_typedef(ctx, s),
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 388, in v_type_typedef
        v_type_type(ctx, type)
      File "/usr/local/src/pyang-0.9.3/pyang/statements.py", line 520, in v_type_type
        for p in patterns]
      File "/usr/local/src/pyang-0.9.3/pyang/types.py", line 252, in validate_pattern_expr
        except libxml2.treeError, v:
    UnboundLocalError: local variable 'libxml2' referenced before assignment
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Yes. My guess is libxml2 failed to import, Python evaluated the first except statement and failed because libxml2 was not defined.

    Comment

    • Bruce Korb
      New Member
      • Sep 2010
      • 2

      #3
      The SVN repository for pyang (googlecode) has the fix:
      do the import first and then the "try:" clause, re-indenting
      as needed. I filed a bug with them asking them to bump out
      their next release. (libxml2 is apparently "nice", but not
      required.) Thanks!

      Comment

      Working...