Stop at exception in my, not library code

后端 未结 5 2202
故里飘歌
故里飘歌 2021-02-14 22:33

I\'m developing an app using a Python library urllib and it is sometimes rising exceptions due to not being able to access an URL.

However, the exception is

5条回答
  •  囚心锁ツ
    2021-02-14 23:21

    It can be done with some hacking. These docs show how you can turn on post-mortem debugging with the following code in the entry point:

    import sys
    from IPython.core import ultratb
    sys.excepthook = ultratb.FormattedTB(mode='Verbose',
                                         color_scheme='Linux', call_pdb=1)
    

    Stepping through this hook after an exception is raised shows that we need to tinker with the debugger method. Unfortunately I can see no better way to do this other than to copy the entire method and modify it where needed (I tried modifying self.tb but traceback objects are read only and can't be used with copy.deepcopy). Here's a demo:

    import json
    import sys
    from IPython.core import debugger, ultratb
    from IPython.core.display_trap import DisplayTrap
    
    class CustomTB(ultratb.FormattedTB):
        def debugger(self, force=False):
            if force or self.call_pdb:
                if self.pdb is None:
                    self.pdb = debugger.Pdb(
                        self.color_scheme_table.active_scheme_name)
                # the system displayhook may have changed, restore the original
                # for pdb
                display_trap = DisplayTrap(hook=sys.__displayhook__)
                with display_trap:
                    self.pdb.reset()
                    # Find the right frame so we don't pop up inside ipython itself
                    if hasattr(self, 'tb') and self.tb is not None:
                        etb = self.tb
                    else:
                        etb = self.tb = sys.last_traceback
    
                    # only modification is here -----+
                    #                                |
                    #                                V
                    while self.tb is not None and '/lib/python3' not in self.tb.tb_next.tb_frame.f_code.co_filename:
                        self.tb = self.tb.tb_next
                    if etb and etb.tb_next:
                        etb = etb.tb_next
                    self.pdb.botframe = etb.tb_frame
                    self.pdb.interaction(self.tb.tb_frame, self.tb)
    
            if hasattr(self, 'tb'):
                del self.tb
    
    sys.excepthook = CustomTB(mode='Verbose',
                              color_scheme='Linux', call_pdb=1)
    
    def foo():
        bar()
    
    def bar():
        json.dumps(json)
    
    foo()
    

    As you can see it stops searching through the traceback when it's about to reach library code. Here's the result:

    TypeErrorTraceback (most recent call last)
    /Users/alexhall/Dropbox/python/sandbox3/sandbox.py in ()
         40     json.dumps(json)
         41 
    ---> 42 foo()
            global foo = 
    
    /Users/alexhall/Dropbox/python/sandbox3/sandbox.py in foo()
         35 
         36 def foo():
    ---> 37     bar()
            global bar = 
         38 
         39 def bar():
    
    /Users/alexhall/Dropbox/python/sandbox3/sandbox.py in bar()
         38 
         39 def bar():
    ---> 40     json.dumps(json)
            global json.dumps = 
            global json = 
         41 
         42 foo()
    
    /Users/alexhall/.pyenv/versions/3.5.0/lib/python3.5/json/__init__.py in dumps(obj=, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw={})
        228         cls is None and indent is None and separators is None and
        229         default is None and not sort_keys and not kw):
    --> 230         return _default_encoder.encode(obj)
            global _default_encoder.encode = >
            obj = 
        231     if cls is None:
        232         cls = JSONEncoder
    
    /Users/alexhall/.pyenv/versions/3.5.0/lib/python3.5/json/encoder.py in encode(self=, o=)
        197         # exceptions aren't as detailed.  The list call should be roughly
        198         # equivalent to the PySequence_Fast that ''.join() would do.
    --> 199         chunks = self.iterencode(o, _one_shot=True)
            chunks = undefined
            self.iterencode = >
            o = 
            global _one_shot = undefined
        200         if not isinstance(chunks, (list, tuple)):
        201             chunks = list(chunks)
    
    /Users/alexhall/.pyenv/versions/3.5.0/lib/python3.5/json/encoder.py in iterencode(self=, o=, _one_shot=True)
        255                 self.key_separator, self.item_separator, self.sort_keys,
        256                 self.skipkeys, _one_shot)
    --> 257         return _iterencode(o, 0)
            _iterencode = <_json.Encoder object at 0x1031296d8>
            o = 
        258 
        259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
    
    /Users/alexhall/.pyenv/versions/3.5.0/lib/python3.5/json/encoder.py in default(self=, o=)
        178 
        179         ""
    --> 180         raise TypeError(repr(o) + " is not JSON serializable")
            global TypeError = undefined
            global repr = undefined
            o = 
        181 
        182     def encode(self, o):
    
    TypeError:  is not JSON serializable
    > /Users/alexhall/Dropbox/python/sandbox3/sandbox.py(40)bar()
         38 
         39 def bar():
    ---> 40     json.dumps(json)
         41 
         42 foo()
    
    ipdb> down
    > /Users/alexhall/.pyenv/versions/3.5.0/lib/python3.5/json/__init__.py(230)dumps()
        228         cls is None and indent is None and separators is None and
        229         default is None and not sort_keys and not kw):
    --> 230         return _default_encoder.encode(obj)
        231     if cls is None:
        232         cls = JSONEncoder
    
    ipdb> 
    

    Basically the full traceback is still printed out but ipdb starts at your own code. If you enter the down command you find yourself in a library frame.

提交回复
热议问题