Bug 180228 - Exception KeyError not caught but propagted instead
Exception KeyError not caught but propagted instead
Status: CLOSED WONTFIX
Product: python
Classification: Unclassified
Component: Debugger
6.x
PC Windows XP
: P2 with 1 vote (vote)
: TBD
Assigned To: jymen
issues@python
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2010-02-03 13:02 UTC by loretta
Modified: 2017-06-23 08:05 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description loretta 2010-02-03 13:02:22 UTC
In the following code KeyErrors are anticipated and action is taken accordingly by the except branch:

    def alarm_activate( self, id):
        assert isinstance(id, AlarmId)
        try:
            alarm = self._ALARM_DICT[id.as_key()]

        except KeyError:
            alarm = self._ALARM_DICT[AlarmId( 999).as_key()]
            alarm.note( id)

        self._alarms_active_by_id[id.as_key()] = alarm
        alarm.activate()
        return

This code works fine, if it is run outside the debugger. If it is run by the debugger, the code w/i the except branch is never exec'ed. The exception seems to be propagated up to main. That causes the debugger to stop:  >>>Debug session normal end.

The program's output should be (and is, if run ordinarily):

Activate unknown alarm. 
Activate known alarm. 
Print all alarms. 
Print alarm 0. 
[999] FATAL: Unknown error -1!
Deactivate alarm 0. 
Print alarm 1. 
[1] ERROR: Kinematic error!
Deactivate alarm 1. 
Finidhed. 

However, if it is run in debug mode, the output is:

>>>[LOG]PythonDebugger : overall Starting
[LOG]PythonDebugger.taskStarted : I am Starting a new Debugging Session ...
[LOG]This window is an interactive debugging context aware Python Shell 
[LOG]where you can enter python console commands while debugging 
>>>/home/loretta/.netbeans/6.8/config/nbPython/debug/nbpythondebug/jpydaemon.py
args =  ['/home/loretta/.netbeans/6.8/config/nbPython/debug/nbpythondebug/jpydaemon.py', 'localhost', '29100']
localDebuggee= None
JPyDbg connecting  localhost  on in=  29100 /out= 29101
JPyDbgI0001 : connected to  localhost
>>>[stdout:]Activate unknown alarm. >>>Debug session normal end


The complete code is:

# -*- coding: utf8 -*-

__doc__ = """Package alarms

Alarm-Katalog:
    Hält alle auf dem Target aktiven Alarme.

    -   Der Katalog hält alle verfügbaren Alarme in einem Dict _ALARM_DICT.
    -   Der Katalog hält alle aktivierten Alarme in einem Dict _active_alarms_by_id.
    -   Alarme, die im Katalog noch nicht aktiv sind, werden über ihre Id aktiviert.
    -   Alarme, die bereits aktiv sind, werden nicht nochmals aktiviert.
    -   Alarme werden beim Quittieren auf dem Target quittiert und im Katalog
        deaktiviert (so können sie in visualisierenden Widgets als nicht mehr
        anzuzeigen erkannt werden).
"""
__author__ = "loretta"
__date__ = "$Jan 25, 2010 8:39:32 PM$"


import time


class AlarmId:

    def __init__(self, id):
        assert isinstance(id, int)
        self._id = id

    def __int__(self):
        return self._id
    
    def __repr__(self):
        return str( self._id)

    def as_key(self):
        return int( self)


class _Alarm:

    def __init__(self, alarm_catalog, id, text, severity):
        self._alarm_catalog = alarm_catalog
        self._id = id
        self._text = text
        self._severity = severity

        self._timestamp_activated = 0
        self._timestamp_deactivated = 0

        self._note = ""
        return

    def __str__(self):
        text = "[%d] %s: %s" % (self._id, self._severity.split( "_")[-1], self._text)
        if self._note:
            text += " %s" % self._note

        text += "!"
        return text

    def id(self):
        return self._id

    def activate(self):
        self._timestamp_activated = Timestamp.Now()

    def deactivate(self):
        self._timestamp_deactivated = Timestamp.Now()

    def note(self, id):
        self._note = str( id)


class AlarmInfo(_Alarm):

    def __init__(self, alarm_catalog, id, text, severity, timestamp=time.time()):
        """Documentation"""
        _Alarm.__init__(self, alarm_catalog, id, text, severity, timestamp)



class AlarmCatalog:

    _SEVERITY__INFO = "_SEVERITY__INFO"
    _SEVERITY__WARNING = "_SEVERITY__WARNING"
    _SEVERITY__ERROR = "_SEVERITY__ERROR"
    _SEVERITY__FATAL = "_SEVERITY__FATAL"

    
    def __init__(self):
        self._ALARM_LIST = [ _Alarm( self, AlarmId( 999), "Unknown error", self._SEVERITY__FATAL)
                           , _Alarm( self, AlarmId( 1), "Kinematic error", self._SEVERITY__ERROR)
                           , _Alarm( self, AlarmId( 2), "Program error", self._SEVERITY__ERROR)
                           ]
        self._ALARM_DICT = {}
        for alarm in self._ALARM_LIST:
            self._ALARM_DICT[alarm.id().as_key()] = alarm

        self._alarms_active_by_id = {}
        return

    def alarm_activate( self, id):
        assert isinstance(id, AlarmId)
        try:
            alarm = self._ALARM_DICT[id.as_key()]

        except KeyError, e:
            alarm = self._ALARM_DICT[AlarmId( 999).as_key()]
            alarm.note( id)

        self._alarms_active_by_id[id.as_key()] = alarm
        alarm.activate()
        return

    def alarm_is_activated( self, id):
        return self._alarms_active_by_id.has_key( int( id))

    def alarm_deactivate( self, id):
        alarm = self._alarms_active_by_id[int( id)]
        del self._alarms_active_by_id[int( id)]
        alarm.deactivate()

    def alarms_activated(self):
        return self._alarms_active_by_id.values()


class Timestamp:

    @staticmethod
    def Now():
        return Timestamp( time.time())


    def __init__(self, timestamp):
        self._timestamp = timestamp
        return

    def __str__(self):
        raise NotImplementedError()


if __name__ == "__main__":
    # @type alarm _Alarm
    ac = AlarmCatalog()
    print "Activate unknown alarm. "
    ac.alarm_activate( AlarmId( -1))
    print "Activate known alarm. "
    ac.alarm_activate( AlarmId( 1))
    print "Print all alarms. "
    for i, alarm in enumerate( ac.alarms_activated()):
        print "Print alarm %d. " % i
        print alarm
        print "Deactivate alarm %d. " % i
        alarm.deactivate()

    print "Finidhed. "


As soon as the code contains exceptions, the debugger seems useless.

Kind regards
Loretta

P.S.: My 1st post here, hope I did it right...
Comment 1 davtom 2010-04-21 16:35:15 UTC
I have reproduced this issue in some code that was entirely different to this. I worked around it successfully by running my script, rather than debugging it.
Comment 2 Jenselme 2017-06-22 22:07:57 UTC
Should be solved with the new debugger when we add it. The debugger is currently disabled. Closing this since there is no point into importing it elsewhere.
Comment 3 Jiri Kovalsky 2017-06-23 08:05:41 UTC
Thanks for your evaluation Julien. Reporter, if you think your issue is still valid and needs to be addressed, don't hesitate to reopen it with additional information. Closing for now.


By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo