0

Here's a snippet of the config file:

[{u'sequenceNumber': 10, u'text': u'--- 10-999 are broad permits'},
 {u'action': u'permit',
  u'counterData': {},
  u'log': False,
  u'payload': {u'payload': []},
  u'ruleFilter': {u'destination': {u'ip': u'0.0.0.0', u'mask': 0},
                  u'dscp': {u'match': False, u'value': 0},
                  u'dstPort': {u'maxPorts': 10,
                               u'oper': u'any',
                               u'ports': []},
                  u'established': False,
                  u'fragments': False,
                  u'gre': {u'protoMask': 0, u'protocol': 0},
                  u'gtp': {u'teid': 0, u'teidMask': 0},
                  u'icmp': {u'code': 65535, u'type': 65535},
                  u'nvgre': {u'protoMask': 0,
                             u'protocol': 0,
                             u'tni': 0,
                             u'tniMask': 0},
                  u'protocol': 2,
                  u'source': {u'ip': u'0.0.0.0', u'mask': 0},
                  u'srcPort': {u'maxPorts': 10,
                               u'oper': u'any',
                               u'ports': []},
                  u'standard': False,
                  u'tcpFlags': 0,
                  u'tracked': False,
                  u'ttl': {u'oper': u'any', u'value': 0},
                  u'userL4': {u'pattern': 0, u'patternMask': 0},
                  u'vlan': {u'id': 0,
                            u'innerId': 0,
                            u'innerMask': 0,
                            u'mask': 0},
                  u'vxlan': {u'vni': 0, u'vniMask': 0, u'vxlanValid': False}},
  u'sequenceNumber': 20,
  u'text': u'permit igmp any any'},

Snippet of code with problem.
Hereby, I'm trying to run through loop with KeyError and NameError handlers as not all lines in input have a value of 'src_mk'.

for seq in acl:
    try:
        src_mk = seq['ruleFilter']['source']['mask']
    except (KeyError, NameError):
        pass
    print src_mk

I'm getting a NameError - name not defined. I tired handling exception separately but it didn't work.

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
NameError: name 'src_mk' is not defined
3
  • print src_mk is after your try - so I'm not sure how you tried handling this exception Commented Mar 21, 2017 at 18:45
  • Where are you expecting a NameError? Commented Mar 21, 2017 at 19:04
  • What are you expecting src_mk to be in case a KeyError happens? Commented Mar 21, 2017 at 19:04

1 Answer 1

1

You need to define the variable outside the try-except block beforehand. See in the comments section @Daniel Roseman's comment for WHY your code doesn't work

Something in this lines should work:

for seq in acl:
    src_mk = None

    try:
        src_mk = seq['ruleFilter']['source']['mask']
    except (KeyError,NameError):
        pass

    print src_mk # Note that if it is None it means there was an exception
Sign up to request clarification or add additional context in comments.

3 Comments

Works! Thanks a lot.
The solution is right, but the explanation is not. Python doesn't have block scope; the error is happening because the exception is being triggered, so the assignment is never reached. If the exception wasn't raised, the code would work and the variable would be visible outside the block.
You should really incorporate that comment into your answer to make it complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.