Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support catch_any_exception #31

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def outer():

### Save the exception traceback.

In the code, find the place where we need to do the `try ... except ...` and use `save_dumpling()`. When we save the dump file, it will default to `${exception filename}:${error lineno}.dump`.
In the code, find the place where we need to do the `try ... except ...` and use `save_dumpling()`. When we save the dump file, it will default to `${exception filename}-${error lineno}.dump`.

```python
from pydumpling import save_dumping
Expand Down Expand Up @@ -160,5 +160,25 @@ It will open the debugger on port 4444, then we can access pdb using telnet、ne
`nc 127.0.0.1 4444`
![alt text](static/rpdb.png)

#### Enable global exception catching:
```python
from pydumpling import catch_any_exception

catch_any_exception()

def inner():
a = 1
b = "2"
c = a + b # noqa: F841


def outer():
inner()

if __name__ == "__main__":
outer()

```

## TODO
- []
22 changes: 21 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def outer():


### 在异常发生时进行异常堆栈的保存
在异常捕获的处理代码中使用`save_dumpling()`. 如果不指定文件名,默认使用:`${exception file}:${line number of the exception}.dump`.
在异常捕获的处理代码中使用`save_dumpling()`. 如果不指定文件名,默认使用:`${exception file}-${line number of the exception}.dump`.

```python
from pydumpling import save_dumping
Expand Down Expand Up @@ -156,5 +156,25 @@ TypeError: unsupported operand type(s) for +: 'int' and 'str'
`nc 127.0.0.1 4444`
![alt text](static/rpdb.png)

#### 开启全局异常捕获:
```python
from pydumpling import catch_any_exception

catch_any_exception()

def inner():
a = 1
b = "2"
c = a + b # noqa: F841


def outer():
inner()

if __name__ == "__main__":
outer()

```

## TODO
- []
26 changes: 25 additions & 1 deletion docs/source/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ help message
-h, --help show this help message and exit
--print print traceback information
--debug enter pdb debugging interface
--rdebug enter rpdb debugging interface

Print the traceback
###################
Expand Down Expand Up @@ -164,4 +165,27 @@ It will open the debugger on port 4444, then we can access pdb using `telnet`_,
.. _netcat: https://netcat.sourceforge.net/


.. image:: _static/rpdb.png
.. image:: _static/rpdb.png


Enable global exception catching
################################

.. code-block:: python

from pydumpling import catch_any_exception

catch_any_exception()

def inner():
a = 1
b = "2"
c = a + b # noqa: F841


def outer():
inner()

if __name__ == "__main__":
outer()

12 changes: 7 additions & 5 deletions pydumpling/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from .rpdb import r_post_mortem
from .debug_dumpling import debug_dumpling, load_dumpling
from .pydumpling import save_dumping, dump_current_traceback, __version__

from .helpers import catch_any_exception
from .pydumpling import __version__, dump_current_traceback, save_dumping
from .rpdb import r_post_mortem

__version__ == __version__
__all__ = ["debug_dumpling", "load_dumpling", "save_dumping", "dump_current_traceback", "r_post_mortem"]
__all__ = ["debug_dumpling", "load_dumpling", "save_dumping",
"dump_current_traceback", "r_post_mortem", "catch_any_exception"]
3 changes: 2 additions & 1 deletion pydumpling/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import argparse
import os.path

from .debug_dumpling import debug_dumpling, load_dumpling
from .rpdb import r_post_mortem
from .helpers import print_traceback_and_except
from .rpdb import r_post_mortem

DUMP_FILE_EXTENSION: str = ".dump"

Expand Down
13 changes: 8 additions & 5 deletions pydumpling/debug_dumpling.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import gzip
import inspect
import pdb
import dill
import pickle
from packaging.version import parse
import inspect
import types
from .fake_types import FakeFrame, FakeTraceback, FakeCode

import dill
from packaging.version import parse

from .fake_types import FakeCode, FakeFrame, FakeTraceback


def load_dumpling(filename):
Expand Down
6 changes: 4 additions & 2 deletions pydumpling/fake_types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import os
import sys

import dill


Expand Down Expand Up @@ -41,7 +43,7 @@ def _convert(cls, v):
except Exception:
return cls._safe_repr(v)
else:
from datetime import date, time, datetime, timedelta
from datetime import date, datetime, time, timedelta

BUILTIN = (str, unicode, int, long, float, date, time, datetime, timedelta) if sys.version_info.major == 2 \
else (str, int, float, date, time, datetime, timedelta) # noqa: F821
Expand Down
3 changes: 2 additions & 1 deletion pydumpling/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
from traceback import print_tb, print_exception
from traceback import print_exception, print_tb

from .pydumpling import save_dumping


Expand Down
11 changes: 7 additions & 4 deletions pydumpling/pydumpling.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import gzip
import sys
import dill
import inspect
import pickle
import sys
import warnings
import inspect

import dill

from .fake_types import FakeFrame, FakeTraceback

__version__ = "0.1.5"
Expand Down
3 changes: 2 additions & 1 deletion pydumpling/rpdb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pdb
import socket
import threading
import sys
import threading

from .debug_dumpling import load_dumpling, mock_inspect

DEFAULT_ADDR = "127.0.0.1"
Expand Down
Loading