| 1 |
Metadata-Version: 2.4 |
| 2 |
Name: tblib |
| 3 |
Version: 3.2.2 |
| 4 |
Summary: Traceback serialization library. |
| 5 |
Author-email: Ionel Cristian Mărieș <[email protected]> |
| 6 |
License-Expression: BSD-2-Clause |
| 7 |
Project-URL: Sources, https://github.com/ionelmc/python-tblib |
| 8 |
Project-URL: Documentation, https://python-tblib.readthedocs.io/ |
| 9 |
Project-URL: Changelog, https://python-tblib.readthedocs.io/en/latest/changelog.html |
| 10 |
Project-URL: Issue Tracker, https://github.com/ionelmc/python-tblib/issues |
| 11 |
Keywords: traceback,debugging,exceptions |
| 12 |
Classifier: Development Status :: 5 - Production/Stable |
| 13 |
Classifier: Intended Audience :: Developers |
| 14 |
Classifier: Operating System :: Unix |
| 15 |
Classifier: Operating System :: POSIX |
| 16 |
Classifier: Operating System :: Microsoft :: Windows |
| 17 |
Classifier: Programming Language :: Python |
| 18 |
Classifier: Programming Language :: Python :: 3 |
| 19 |
Classifier: Programming Language :: Python :: 3 :: Only |
| 20 |
Classifier: Programming Language :: Python :: 3.9 |
| 21 |
Classifier: Programming Language :: Python :: 3.10 |
| 22 |
Classifier: Programming Language :: Python :: 3.11 |
| 23 |
Classifier: Programming Language :: Python :: 3.12 |
| 24 |
Classifier: Programming Language :: Python :: 3.13 |
| 25 |
Classifier: Programming Language :: Python :: 3.14 |
| 26 |
Classifier: Programming Language :: Python :: Implementation :: CPython |
| 27 |
Classifier: Programming Language :: Python :: Implementation :: PyPy |
| 28 |
Classifier: Topic :: Utilities |
| 29 |
Requires-Python: >=3.9 |
| 30 |
Description-Content-Type: text/x-rst |
| 31 |
License-File: LICENSE |
| 32 |
Dynamic: description |
| 33 |
Dynamic: description-content-type |
| 34 |
Dynamic: license-file |
| 35 |
|
| 36 |
======== |
| 37 |
Overview |
| 38 |
======== |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
Serialization library for Exceptions and Tracebacks. |
| 43 |
|
| 44 |
* Free software: BSD license |
| 45 |
|
| 46 |
It allows you to: |
| 47 |
|
| 48 |
* `Pickle <https://docs.python.org/3/library/pickle.html>`_ tracebacks and raise exceptions |
| 49 |
with pickled tracebacks in different processes. This allows better error handling when running |
| 50 |
code over multiple processes (imagine multiprocessing, billiard, futures, celery etc). |
| 51 |
* Create traceback objects from strings (the ``from_string`` method). *No pickling is used*. |
| 52 |
* Serialize tracebacks to/from plain dicts (the ``from_dict`` and ``to_dict`` methods). *No pickling is used*. |
| 53 |
* Raise the tracebacks created from the aforementioned sources. |
| 54 |
* Pickle an Exception together with its traceback and exception chain |
| 55 |
(``raise ... from ...``) *(Python 3 only)* |
| 56 |
|
| 57 |
**Again, note that using the pickle support is completely optional. You are solely responsible for |
| 58 |
security problems should you decide to use the pickle support.** |
| 59 |
|
| 60 |
Installation |
| 61 |
============ |
| 62 |
|
| 63 |
:: |
| 64 |
|
| 65 |
pip install tblib |
| 66 |
|
| 67 |
Documentation |
| 68 |
============= |
| 69 |
|
| 70 |
.. contents:: |
| 71 |
:local: |
| 72 |
|
| 73 |
Pickling tracebacks |
| 74 |
~~~~~~~~~~~~~~~~~~~ |
| 75 |
|
| 76 |
**Note**: The traceback objects that come out are stripped of some attributes (like variables). But you'll be able to raise exceptions with |
| 77 |
those tracebacks or print them - that should cover 99% of the usecases. |
| 78 |
|
| 79 |
:: |
| 80 |
|
| 81 |
>>> from tblib import pickling_support |
| 82 |
>>> pickling_support.install() |
| 83 |
>>> import pickle, sys |
| 84 |
>>> def inner_0(): |
| 85 |
... raise Exception('fail') |
| 86 |
... |
| 87 |
>>> def inner_1(): |
| 88 |
... inner_0() |
| 89 |
... |
| 90 |
>>> def inner_2(): |
| 91 |
... inner_1() |
| 92 |
... |
| 93 |
>>> try: |
| 94 |
... inner_2() |
| 95 |
... except: |
| 96 |
... s1 = pickle.dumps(sys.exc_info()) |
| 97 |
... |
| 98 |
>>> len(s1) > 1 |
| 99 |
True |
| 100 |
>>> try: |
| 101 |
... inner_2() |
| 102 |
... except: |
| 103 |
... s2 = pickle.dumps(sys.exc_info(), protocol=pickle.HIGHEST_PROTOCOL) |
| 104 |
... |
| 105 |
>>> len(s2) > 1 |
| 106 |
True |
| 107 |
|
| 108 |
>>> try: |
| 109 |
... import cPickle |
| 110 |
... except ImportError: |
| 111 |
... import pickle as cPickle |
| 112 |
>>> try: |
| 113 |
... inner_2() |
| 114 |
... except: |
| 115 |
... s3 = cPickle.dumps(sys.exc_info(), protocol=pickle.HIGHEST_PROTOCOL) |
| 116 |
... |
| 117 |
>>> len(s3) > 1 |
| 118 |
True |
| 119 |
|
| 120 |
Unpickling tracebacks |
| 121 |
~~~~~~~~~~~~~~~~~~~~~ |
| 122 |
|
| 123 |
:: |
| 124 |
|
| 125 |
>>> pickle.loads(s1) |
| 126 |
(<...Exception'>, Exception('fail'...), <traceback object at ...>) |
| 127 |
|
| 128 |
>>> pickle.loads(s2) |
| 129 |
(<...Exception'>, Exception('fail'...), <traceback object at ...>) |
| 130 |
|
| 131 |
>>> pickle.loads(s3) |
| 132 |
(<...Exception'>, Exception('fail'...), <traceback object at ...>) |
| 133 |
|
| 134 |
Raising |
| 135 |
~~~~~~~ |
| 136 |
|
| 137 |
:: |
| 138 |
|
| 139 |
>>> from tblib.decorators import reraise |
| 140 |
>>> reraise(*pickle.loads(s1)) |
| 141 |
Traceback (most recent call last): |
| 142 |
... |
| 143 |
File "<doctest README.rst[14]>", line 1, in <module> |
| 144 |
reraise(*pickle.loads(s2)) |
| 145 |
File "<doctest README.rst[8]>", line 2, in <module> |
| 146 |
inner_2() |
| 147 |
File "<doctest README.rst[5]>", line 2, in inner_2 |
| 148 |
inner_1() |
| 149 |
File "<doctest README.rst[4]>", line 2, in inner_1 |
| 150 |
inner_0() |
| 151 |
File "<doctest README.rst[3]>", line 2, in inner_0 |
| 152 |
raise Exception('fail') |
| 153 |
Exception: fail |
| 154 |
>>> reraise(*pickle.loads(s2)) |
| 155 |
Traceback (most recent call last): |
| 156 |
... |
| 157 |
File "<doctest README.rst[14]>", line 1, in <module> |
| 158 |
reraise(*pickle.loads(s2)) |
| 159 |
File "<doctest README.rst[8]>", line 2, in <module> |
| 160 |
inner_2() |
| 161 |
File "<doctest README.rst[5]>", line 2, in inner_2 |
| 162 |
inner_1() |
| 163 |
File "<doctest README.rst[4]>", line 2, in inner_1 |
| 164 |
inner_0() |
| 165 |
File "<doctest README.rst[3]>", line 2, in inner_0 |
| 166 |
raise Exception('fail') |
| 167 |
Exception: fail |
| 168 |
>>> reraise(*pickle.loads(s3)) |
| 169 |
Traceback (most recent call last): |
| 170 |
... |
| 171 |
File "<doctest README.rst[14]>", line 1, in <module> |
| 172 |
reraise(*pickle.loads(s2)) |
| 173 |
File "<doctest README.rst[8]>", line 2, in <module> |
| 174 |
inner_2() |
| 175 |
File "<doctest README.rst[5]>", line 2, in inner_2 |
| 176 |
inner_1() |
| 177 |
File "<doctest README.rst[4]>", line 2, in inner_1 |
| 178 |
inner_0() |
| 179 |
File "<doctest README.rst[3]>", line 2, in inner_0 |
| 180 |
raise Exception('fail') |
| 181 |
Exception: fail |
| 182 |
|
| 183 |
Pickling Exceptions together with their traceback and chain (Python 3 only) |
| 184 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 185 |
|
| 186 |
:: |
| 187 |
|
| 188 |
>>> try: # doctest: +SKIP |
| 189 |
... try: |
| 190 |
... 1 / 0 |
| 191 |
... except Exception as e: |
| 192 |
... raise Exception("foo") from e |
| 193 |
... except Exception as e: |
| 194 |
... s = pickle.dumps(e) |
| 195 |
>>> raise pickle.loads(s) # doctest: +SKIP |
| 196 |
Traceback (most recent call last): |
| 197 |
File "<doctest README.rst[16]>", line 3, in <module> |
| 198 |
1 / 0 |
| 199 |
ZeroDivisionError: division by zero |
| 200 |
|
| 201 |
The above exception was the direct cause of the following exception: |
| 202 |
|
| 203 |
Traceback (most recent call last): |
| 204 |
File "<doctest README.rst[17]>", line 1, in <module> |
| 205 |
raise pickle.loads(s) |
| 206 |
File "<doctest README.rst[16]>", line 5, in <module> |
| 207 |
raise Exception("foo") from e |
| 208 |
Exception: foo |
| 209 |
|
| 210 |
BaseException subclasses defined after calling ``pickling_support.install()`` will |
| 211 |
**not** retain their traceback and exception chain pickling. |
| 212 |
To cover custom Exceptions, there are three options: |
| 213 |
|
| 214 |
1. Use ``@pickling_support.install`` as a decorator for each custom Exception |
| 215 |
|
| 216 |
.. code-block:: python |
| 217 |
|
| 218 |
>>> from tblib import pickling_support |
| 219 |
>>> # Declare all imports of your package's dependencies |
| 220 |
>>> import numpy # doctest: +SKIP |
| 221 |
|
| 222 |
>>> pickling_support.install() # install for all modules imported so far |
| 223 |
|
| 224 |
>>> @pickling_support.install |
| 225 |
... class CustomError(Exception): |
| 226 |
... pass |
| 227 |
|
| 228 |
Eventual subclasses of ``CustomError`` will need to be decorated again. |
| 229 |
|
| 230 |
2. Invoke ``pickling_support.install()`` after all modules have been imported and all |
| 231 |
Exception subclasses have been declared |
| 232 |
|
| 233 |
.. code-block:: python |
| 234 |
|
| 235 |
>>> # Declare all imports of your package's dependencies |
| 236 |
>>> import numpy # doctest: +SKIP |
| 237 |
>>> from tblib import pickling_support |
| 238 |
|
| 239 |
>>> # Declare your own custom Exceptions |
| 240 |
>>> class CustomError(Exception): |
| 241 |
... pass |
| 242 |
|
| 243 |
>>> # Finally, install tblib |
| 244 |
>>> pickling_support.install() |
| 245 |
|
| 246 |
3. Selectively install tblib for Exception instances just before they are pickled |
| 247 |
|
| 248 |
.. code-block:: python |
| 249 |
|
| 250 |
pickling_support.install(<Exception instance>, [Exception instance], ...) |
| 251 |
|
| 252 |
The above will install tblib pickling for all listed exceptions as well as any other |
| 253 |
exceptions in their exception chains. |
| 254 |
|
| 255 |
For example, one could write a wrapper to be used with |
| 256 |
`ProcessPoolExecutor <https://docs.python.org/3/library/concurrent.futures.html>`_, |
| 257 |
`Dask.distributed <https://distributed.dask.org/>`_, or similar libraries: |
| 258 |
|
| 259 |
:: |
| 260 |
|
| 261 |
>>> from tblib import pickling_support |
| 262 |
>>> def wrapper(func, *args, **kwargs): |
| 263 |
... try: |
| 264 |
... return func(*args, **kwargs) |
| 265 |
... except Exception as e: |
| 266 |
... pickling_support.install(e) |
| 267 |
... raise |
| 268 |
|
| 269 |
What if we have a local stack, does it show correctly ? |
| 270 |
------------------------------------------------------- |
| 271 |
|
| 272 |
Yes it does:: |
| 273 |
|
| 274 |
>>> exc_info = pickle.loads(s3) |
| 275 |
>>> def local_0(): |
| 276 |
... reraise(*exc_info) |
| 277 |
... |
| 278 |
>>> def local_1(): |
| 279 |
... local_0() |
| 280 |
... |
| 281 |
>>> def local_2(): |
| 282 |
... local_1() |
| 283 |
... |
| 284 |
>>> local_2() |
| 285 |
Traceback (most recent call last): |
| 286 |
File "...doctest.py", line ..., in __run |
| 287 |
compileflags, 1) in test.globs |
| 288 |
File "<doctest README.rst[24]>", line 1, in <module> |
| 289 |
local_2() |
| 290 |
File "<doctest README.rst[23]>", line 2, in local_2 |
| 291 |
local_1() |
| 292 |
File "<doctest README.rst[22]>", line 2, in local_1 |
| 293 |
local_0() |
| 294 |
File "<doctest README.rst[21]>", line 2, in local_0 |
| 295 |
reraise(*exc_info) |
| 296 |
File "<doctest README.rst[11]>", line 2, in <module> |
| 297 |
inner_2() |
| 298 |
File "<doctest README.rst[5]>", line 2, in inner_2 |
| 299 |
inner_1() |
| 300 |
File "<doctest README.rst[4]>", line 2, in inner_1 |
| 301 |
inner_0() |
| 302 |
File "<doctest README.rst[3]>", line 2, in inner_0 |
| 303 |
raise Exception('fail') |
| 304 |
Exception: fail |
| 305 |
|
| 306 |
It also supports more contrived scenarios |
| 307 |
----------------------------------------- |
| 308 |
|
| 309 |
Like tracebacks with syntax errors:: |
| 310 |
|
| 311 |
>>> from tblib import Traceback |
| 312 |
>>> from examples import bad_syntax |
| 313 |
>>> try: |
| 314 |
... bad_syntax() |
| 315 |
... except: |
| 316 |
... et, ev, tb = sys.exc_info() |
| 317 |
... tb = Traceback(tb) |
| 318 |
... |
| 319 |
>>> reraise(et, ev, tb.as_traceback()) |
| 320 |
Traceback (most recent call last): |
| 321 |
... |
| 322 |
File "<doctest README.rst[58]>", line 1, in <module> |
| 323 |
reraise(et, ev, tb.as_traceback()) |
| 324 |
File "<doctest README.rst[57]>", line 2, in <module> |
| 325 |
bad_syntax() |
| 326 |
File "...tests...examples.py", line 18, in bad_syntax |
| 327 |
import badsyntax |
| 328 |
File "...tests...badsyntax.py", line 5 |
| 329 |
is very bad |
| 330 |
^ |
| 331 |
SyntaxError: invalid syntax |
| 332 |
|
| 333 |
Or other import failures:: |
| 334 |
|
| 335 |
>>> from examples import bad_module |
| 336 |
>>> try: |
| 337 |
... bad_module() |
| 338 |
... except: |
| 339 |
... et, ev, tb = sys.exc_info() |
| 340 |
... tb = Traceback(tb) |
| 341 |
... |
| 342 |
>>> reraise(et, ev, tb.as_traceback()) |
| 343 |
Traceback (most recent call last): |
| 344 |
... |
| 345 |
File "<doctest README.rst[61]>", line 1, in <module> |
| 346 |
reraise(et, ev, tb.as_traceback()) |
| 347 |
File "<doctest README.rst[60]>", line 2, in <module> |
| 348 |
bad_module() |
| 349 |
File "...tests...examples.py", line 23, in bad_module |
| 350 |
import badmodule |
| 351 |
File "...tests...badmodule.py", line 3, in <module> |
| 352 |
raise Exception("boom!") |
| 353 |
Exception: boom! |
| 354 |
|
| 355 |
Or a traceback that's caused by exceeding the recursion limit (here we're |
| 356 |
forcing the type and value to have consistency across platforms):: |
| 357 |
|
| 358 |
>>> def f(): f() |
| 359 |
>>> try: |
| 360 |
... f() |
| 361 |
... except RuntimeError: |
| 362 |
... et, ev, tb = sys.exc_info() |
| 363 |
... tb = Traceback(tb) |
| 364 |
... |
| 365 |
>>> reraise(RuntimeError, RuntimeError("maximum recursion depth exceeded"), tb.as_traceback()) |
| 366 |
Traceback (most recent call last): |
| 367 |
... |
| 368 |
File "<doctest README.rst[32]>", line 1, in f |
| 369 |
def f(): f() |
| 370 |
File "<doctest README.rst[32]>", line 1, in f |
| 371 |
def f(): f() |
| 372 |
File "<doctest README.rst[32]>", line 1, in f |
| 373 |
def f(): f() |
| 374 |
... |
| 375 |
RuntimeError: maximum recursion depth exceeded |
| 376 |
|
| 377 |
Reference |
| 378 |
~~~~~~~~~ |
| 379 |
|
| 380 |
tblib.Traceback |
| 381 |
--------------- |
| 382 |
|
| 383 |
It is used by the ``pickling_support``. You can use it too if you want more flexibility:: |
| 384 |
|
| 385 |
>>> from tblib import Traceback |
| 386 |
>>> try: |
| 387 |
... inner_2() |
| 388 |
... except: |
| 389 |
... et, ev, tb = sys.exc_info() |
| 390 |
... tb = Traceback(tb) |
| 391 |
... |
| 392 |
>>> reraise(et, ev, tb.as_traceback()) |
| 393 |
Traceback (most recent call last): |
| 394 |
... |
| 395 |
File "<doctest README.rst[21]>", line 6, in <module> |
| 396 |
reraise(et, ev, tb.as_traceback()) |
| 397 |
File "<doctest README.rst[21]>", line 2, in <module> |
| 398 |
inner_2() |
| 399 |
File "<doctest README.rst[5]>", line 2, in inner_2 |
| 400 |
inner_1() |
| 401 |
File "<doctest README.rst[4]>", line 2, in inner_1 |
| 402 |
inner_0() |
| 403 |
File "<doctest README.rst[3]>", line 2, in inner_0 |
| 404 |
raise Exception('fail') |
| 405 |
Exception: fail |
| 406 |
|
| 407 |
tblib.Traceback.to_dict |
| 408 |
``````````````````````` |
| 409 |
|
| 410 |
You can use the ``to_dict`` method and the ``from_dict`` classmethod to |
| 411 |
convert a Traceback into and from a dictionary serializable by the stdlib |
| 412 |
json.JSONDecoder:: |
| 413 |
|
| 414 |
>>> import json |
| 415 |
>>> from pprint import pprint |
| 416 |
>>> try: |
| 417 |
... inner_2() |
| 418 |
... except: |
| 419 |
... et, ev, tb = sys.exc_info() |
| 420 |
... tb = Traceback(tb) |
| 421 |
... tb_dict = tb.to_dict() |
| 422 |
... pprint(tb_dict) |
| 423 |
{'tb_frame': {'f_code': {'co_filename': '<doctest README.rst[...]>', |
| 424 |
'co_name': '<module>'}, |
| 425 |
'f_globals': {'__name__': '__main__'}, |
| 426 |
'f_lineno': 5, |
| 427 |
'f_locals': {}}, |
| 428 |
'tb_lineno': 2, |
| 429 |
'tb_next': {'tb_frame': {'f_code': {'co_filename': ..., |
| 430 |
'co_name': 'inner_2'}, |
| 431 |
'f_globals': {'__name__': '__main__'}, |
| 432 |
'f_lineno': 2, |
| 433 |
'f_locals': {}}, |
| 434 |
'tb_lineno': 2, |
| 435 |
'tb_next': {'tb_frame': {'f_code': {'co_filename': ..., |
| 436 |
'co_name': 'inner_1'}, |
| 437 |
'f_globals': {'__name__': '__main__'}, |
| 438 |
'f_lineno': 2, |
| 439 |
'f_locals': {}}, |
| 440 |
'tb_lineno': 2, |
| 441 |
'tb_next': {'tb_frame': {'f_code': {'co_filename': ..., |
| 442 |
'co_name': 'inner_0'}, |
| 443 |
'f_globals': {'__name__': '__main__'}, |
| 444 |
'f_lineno': 2, |
| 445 |
'f_locals': {}}, |
| 446 |
'tb_lineno': 2, |
| 447 |
'tb_next': None}}}} |
| 448 |
|
| 449 |
tblib.Traceback.from_dict |
| 450 |
````````````````````````` |
| 451 |
|
| 452 |
Building on the previous example:: |
| 453 |
|
| 454 |
>>> tb_json = json.dumps(tb_dict) |
| 455 |
>>> tb = Traceback.from_dict(json.loads(tb_json)) |
| 456 |
>>> reraise(et, ev, tb.as_traceback()) |
| 457 |
Traceback (most recent call last): |
| 458 |
... |
| 459 |
File "<doctest README.rst[21]>", line 6, in <module> |
| 460 |
reraise(et, ev, tb.as_traceback()) |
| 461 |
File "<doctest README.rst[21]>", line 2, in <module> |
| 462 |
inner_2() |
| 463 |
File "<doctest README.rst[5]>", line 2, in inner_2 |
| 464 |
inner_1() |
| 465 |
File "<doctest README.rst[4]>", line 2, in inner_1 |
| 466 |
inner_0() |
| 467 |
File "<doctest README.rst[3]>", line 2, in inner_0 |
| 468 |
raise Exception('fail') |
| 469 |
Exception: fail |
| 470 |
|
| 471 |
tblib.Traceback.from_string |
| 472 |
``````````````````````````` |
| 473 |
|
| 474 |
:: |
| 475 |
|
| 476 |
>>> tb = Traceback.from_string(""" |
| 477 |
... File "skipped.py", line 123, in func_123 |
| 478 |
... Traceback (most recent call last): |
| 479 |
... File "tests/examples.py", line 2, in func_a |
| 480 |
... func_b() |
| 481 |
... File "tests/examples.py", line 6, in func_b |
| 482 |
... func_c() |
| 483 |
... File "tests/examples.py", line 10, in func_c |
| 484 |
... func_d() |
| 485 |
... File "tests/examples.py", line 14, in func_d |
| 486 |
... Doesn't: matter |
| 487 |
... """) |
| 488 |
>>> reraise(et, ev, tb.as_traceback()) |
| 489 |
Traceback (most recent call last): |
| 490 |
... |
| 491 |
File "<doctest README.rst[42]>", line 6, in <module> |
| 492 |
reraise(et, ev, tb.as_traceback()) |
| 493 |
File "...examples.py", line 2, in func_a |
| 494 |
func_b() |
| 495 |
File "...examples.py", line 6, in func_b |
| 496 |
func_c() |
| 497 |
File "...examples.py", line 10, in func_c |
| 498 |
func_d() |
| 499 |
File "...examples.py", line 14, in func_d |
| 500 |
raise Exception('Guessing time !') |
| 501 |
Exception: fail |
| 502 |
|
| 503 |
|
| 504 |
If you use the ``strict=False`` option then parsing is a bit more lax:: |
| 505 |
|
| 506 |
>>> tb = Traceback.from_string(""" |
| 507 |
... File "bogus.py", line 123, in bogus |
| 508 |
... Traceback (most recent call last): |
| 509 |
... File "tests/examples.py", line 2, in func_a |
| 510 |
... func_b() |
| 511 |
... File "tests/examples.py", line 6, in func_b |
| 512 |
... func_c() |
| 513 |
... File "tests/examples.py", line 10, in func_c |
| 514 |
... func_d() |
| 515 |
... File "tests/examples.py", line 14, in func_d |
| 516 |
... Doesn't: matter |
| 517 |
... """, strict=False) |
| 518 |
>>> reraise(et, ev, tb.as_traceback()) |
| 519 |
Traceback (most recent call last): |
| 520 |
... |
| 521 |
File "<doctest README.rst[42]>", line 6, in <module> |
| 522 |
reraise(et, ev, tb.as_traceback()) |
| 523 |
File "bogus.py", line 123, in bogus |
| 524 |
File "...examples.py", line 2, in func_a |
| 525 |
func_b() |
| 526 |
File "...examples.py", line 6, in func_b |
| 527 |
func_c() |
| 528 |
File "...examples.py", line 10, in func_c |
| 529 |
func_d() |
| 530 |
File "...examples.py", line 14, in func_d |
| 531 |
raise Exception('Guessing time !') |
| 532 |
Exception: fail |
| 533 |
|
| 534 |
tblib.decorators.return_error |
| 535 |
----------------------------- |
| 536 |
|
| 537 |
:: |
| 538 |
|
| 539 |
>>> from tblib.decorators import return_error |
| 540 |
>>> inner_2r = return_error(inner_2) |
| 541 |
>>> e = inner_2r() |
| 542 |
>>> e |
| 543 |
<tblib.decorators.Error object at ...> |
| 544 |
>>> e.reraise() |
| 545 |
Traceback (most recent call last): |
| 546 |
... |
| 547 |
File "<doctest README.rst[26]>", line 1, in <module> |
| 548 |
e.reraise() |
| 549 |
File "...tblib...decorators.py", line 19, in reraise |
| 550 |
reraise(self.exc_type, self.exc_value, self.traceback) |
| 551 |
File "...tblib...decorators.py", line 25, in return_exceptions_wrapper |
| 552 |
return func(*args, **kwargs) |
| 553 |
File "<doctest README.rst[5]>", line 2, in inner_2 |
| 554 |
inner_1() |
| 555 |
File "<doctest README.rst[4]>", line 2, in inner_1 |
| 556 |
inner_0() |
| 557 |
File "<doctest README.rst[3]>", line 2, in inner_0 |
| 558 |
raise Exception('fail') |
| 559 |
Exception: fail |
| 560 |
|
| 561 |
How's this useful? Imagine you're using multiprocessing like this:: |
| 562 |
|
| 563 |
# Note that Python 3.4 and later will show the remote traceback (but as a string sadly) so we skip testing this. |
| 564 |
>>> import traceback |
| 565 |
>>> from multiprocessing import Pool |
| 566 |
>>> from examples import func_a |
| 567 |
>>> pool = Pool() # doctest: +SKIP |
| 568 |
>>> try: # doctest: +SKIP |
| 569 |
... for i in pool.map(func_a, range(5)): |
| 570 |
... print(i) |
| 571 |
... except: |
| 572 |
... print(traceback.format_exc()) |
| 573 |
... |
| 574 |
Traceback (most recent call last): |
| 575 |
File "<doctest README.rst[...]>", line 2, in <module> |
| 576 |
for i in pool.map(func_a, range(5)): |
| 577 |
File "...multiprocessing...pool.py", line ..., in map |
| 578 |
... |
| 579 |
File "...multiprocessing...pool.py", line ..., in get |
| 580 |
... |
| 581 |
Exception: Guessing time ! |
| 582 |
<BLANKLINE> |
| 583 |
>>> pool.terminate() # doctest: +SKIP |
| 584 |
|
| 585 |
Not very useful is it? Let's sort this out:: |
| 586 |
|
| 587 |
>>> from tblib.decorators import apply_with_return_error, Error |
| 588 |
>>> from itertools import repeat |
| 589 |
>>> pool = Pool() |
| 590 |
>>> try: # doctest: +SKIP |
| 591 |
... for i in pool.map(apply_with_return_error, zip(repeat(func_a), range(5))): |
| 592 |
... if isinstance(i, Error): |
| 593 |
... i.reraise() |
| 594 |
... else: |
| 595 |
... print(i) |
| 596 |
... except: |
| 597 |
... print(traceback.format_exc()) |
| 598 |
... |
| 599 |
Traceback (most recent call last): |
| 600 |
File "<doctest README.rst[...]>", line 4, in <module> |
| 601 |
i.reraise() |
| 602 |
File "...tblib...decorators.py", line ..., in reraise |
| 603 |
reraise(self.exc_type, self.exc_value, self.traceback) |
| 604 |
File "...tblib...decorators.py", line ..., in reraise |
| 605 |
raise value.with_traceback(tb) |
| 606 |
File "...tblib...decorators.py", line ..., in return_exceptions_wrapper |
| 607 |
return func(*args, **kwargs) |
| 608 |
File "...tblib...decorators.py", line ..., in apply_with_return_error |
| 609 |
return args[0](*args[1:]) |
| 610 |
File "...examples.py", line 2, in func_a |
| 611 |
func_b() |
| 612 |
File "...examples.py", line 6, in func_b |
| 613 |
func_c() |
| 614 |
File "...examples.py", line 10, in func_c |
| 615 |
func_d() |
| 616 |
File "...examples.py", line 14, in func_d |
| 617 |
raise Exception('Guessing time !') |
| 618 |
Exception: Guessing time ! |
| 619 |
<BLANKLINE> |
| 620 |
>>> pool.terminate() |
| 621 |
|
| 622 |
Much better ! |
| 623 |
|
| 624 |
What if we have a local call stack ? |
| 625 |
```````````````````````````````````` |
| 626 |
|
| 627 |
:: |
| 628 |
|
| 629 |
>>> def local_0(): |
| 630 |
... pool = Pool() |
| 631 |
... try: |
| 632 |
... for i in pool.map(apply_with_return_error, zip(repeat(func_a), range(5))): |
| 633 |
... if isinstance(i, Error): |
| 634 |
... i.reraise() |
| 635 |
... else: |
| 636 |
... print(i) |
| 637 |
... finally: |
| 638 |
... pool.close() |
| 639 |
... |
| 640 |
>>> def local_1(): |
| 641 |
... local_0() |
| 642 |
... |
| 643 |
>>> def local_2(): |
| 644 |
... local_1() |
| 645 |
... |
| 646 |
>>> try: # doctest: +SKIP |
| 647 |
... local_2() |
| 648 |
... except: |
| 649 |
... print(traceback.format_exc()) |
| 650 |
... |
| 651 |
Traceback (most recent call last): |
| 652 |
File "<doctest README.rst[...]>", line 2, in <module> |
| 653 |
local_2() |
| 654 |
File "<doctest README.rst[...]>", line 2, in local_2 |
| 655 |
local_1() |
| 656 |
File "<doctest README.rst[...]>", line 2, in local_1 |
| 657 |
local_0() |
| 658 |
File "<doctest README.rst[...]>", line 6, in local_0 |
| 659 |
i.reraise() |
| 660 |
File "...tblib...decorators.py", line ..., in reraise |
| 661 |
reraise(self.exc_type, self.exc_value, self.traceback) |
| 662 |
File "...tblib...decorators.py", line ..., in reraise |
| 663 |
raise value.with_traceback(tb) |
| 664 |
File "...tblib...decorators.py", line ..., in return_exceptions_wrapper |
| 665 |
return func(*args, **kwargs) |
| 666 |
File "...tblib...decorators.py", line ..., in apply_with_return_error |
| 667 |
return args[0](*args[1:]) |
| 668 |
File "...tests...examples.py", line 2, in func_a |
| 669 |
func_b() |
| 670 |
File "...tests...examples.py", line 6, in func_b |
| 671 |
func_c() |
| 672 |
File "...tests...examples.py", line 10, in func_c |
| 673 |
func_d() |
| 674 |
File "...tests...examples.py", line 14, in func_d |
| 675 |
raise Exception('Guessing time !') |
| 676 |
Exception: Guessing time ! |
| 677 |
<BLANKLINE> |
| 678 |
|
| 679 |
Other weird stuff |
| 680 |
````````````````` |
| 681 |
|
| 682 |
Clearing traceback works (Python 3.4 and up):: |
| 683 |
|
| 684 |
>>> tb = Traceback.from_string(""" |
| 685 |
... File "skipped.py", line 123, in func_123 |
| 686 |
... Traceback (most recent call last): |
| 687 |
... File "tests/examples.py", line 2, in func_a |
| 688 |
... func_b() |
| 689 |
... File "tests/examples.py", line 6, in func_b |
| 690 |
... func_c() |
| 691 |
... File "tests/examples.py", line 10, in func_c |
| 692 |
... func_d() |
| 693 |
... File "tests/examples.py", line 14, in func_d |
| 694 |
... Doesn't: matter |
| 695 |
... """) |
| 696 |
>>> import traceback, sys |
| 697 |
>>> if sys.version_info > (3, 4): |
| 698 |
... traceback.clear_frames(tb) |
| 699 |
|
| 700 |
Credits |
| 701 |
======= |
| 702 |
|
| 703 |
* `mitsuhiko/jinja2 <https://github.com/mitsuhiko/jinja2>`_ for figuring a way to create traceback objects. |
| 704 |
|
| 705 |
|
| 706 |
Changelog |
| 707 |
========= |
| 708 |
|
| 709 |
3.2.2 (2025-11-12) |
| 710 |
~~~~~~~~~~~~~~~~~~ |
| 711 |
|
| 712 |
* Fixed regression occurring with ``TimeoutError`` exceptions. They should be represented now exactly as the original when unpickling. |
| 713 |
Contributed by Jacob Tomlinson in `#85 <https://github.com/ionelmc/python-tblib/pull/85>`_. |
| 714 |
|
| 715 |
3.2.1 (2025-10-31) |
| 716 |
~~~~~~~~~~~~~~~~~~ |
| 717 |
|
| 718 |
* Fixed regression occurring with ``ExceptionGroup`` exceptions. That exception type is now handled specifically in the new ``unpickle_exception_with_attrs`` function (just like ``OSError``). |
| 719 |
|
| 720 |
3.2.0 (2025-10-21) |
| 721 |
~~~~~~~~~~~~~~~~~~ |
| 722 |
|
| 723 |
* Changed ``tblib.pickling_support.install`` to support exceptions with ``__init__`` that does match the default |
| 724 |
``BaseException.__reduce__`` (as it expects the positional arguments to ``__init__`` to match the ``args`` attribute). |
| 725 |
|
| 726 |
Special handling for OSError (and subclasses) is also included. The errno, strerror, winerror, filename and filename2 attributes will be added in the reduce structure (if set). |
| 727 |
|
| 728 |
This will support exception subclasses that do this without defining a custom ``__reduce__``: |
| 729 |
|
| 730 |
.. code-block:: python |
| 731 |
|
| 732 |
def __init__(self): |
| 733 |
super().__init__('mistery argument') |
| 734 |
|
| 735 |
def __init__(self, mistery_argument): |
| 736 |
super().__init__() |
| 737 |
self.mistery_argument = mistery_argument |
| 738 |
|
| 739 |
Tests and POC contributed by Oldřich Jedlička in `#73 <https://github.com/ionelmc/python-tblib/pull/73>`_. |
| 740 |
* Fixed some doctest and coverage config. Contributed by Colin Watson in `#79 <https://github.com/ionelmc/python-tblib/pull/79>`_. |
| 741 |
|
| 742 |
|
| 743 |
3.1.0 (2025-03-31) |
| 744 |
~~~~~~~~~~~~~~~~~~ |
| 745 |
|
| 746 |
* Improved performance of ``as_traceback`` by a large factor. |
| 747 |
Contributed by Haoyu Weng in `#81 <https://github.com/ionelmc/python-tblib/pull/81>`_. |
| 748 |
* Dropped support for now-EOL Python 3.8 and added 3.13 in the test grid. |
| 749 |
|
| 750 |
3.0.0 (2023-10-22) |
| 751 |
~~~~~~~~~~~~~~~~~~ |
| 752 |
|
| 753 |
* Added support for ``__context__``, ``__suppress_context__`` and ``__notes__``. |
| 754 |
Contributed by Tim Maxwell in `#72 <https://github.com/ionelmc/python-tblib/pull/72>`_. |
| 755 |
* Added the ``get_locals`` argument to ``tblib.pickling_support.install()``, ``tblib.Traceback`` and ``tblib.Frame``. |
| 756 |
Fixes `#41 <https://github.com/ionelmc/python-tblib/issues/41>`_. |
| 757 |
* Dropped support for now-EOL Python 3.7 and added 3.12 in the test grid. |
| 758 |
|
| 759 |
2.0.0 (2023-06-22) |
| 760 |
~~~~~~~~~~~~~~~~~~ |
| 761 |
|
| 762 |
* Removed support for legacy Pythons (2.7 and 3.6) and added Python 3.11 in the test grid. |
| 763 |
* Some cleanups and refactors (mostly from ruff). |
| 764 |
|
| 765 |
1.7.0 (2020-07-24) |
| 766 |
~~~~~~~~~~~~~~~~~~ |
| 767 |
|
| 768 |
* Add more attributes to ``Frame`` and ``Code`` objects for pytest compatibility. Contributed by Ivanq in |
| 769 |
`#58 <https://github.com/ionelmc/python-tblib/pull/58>`_. |
| 770 |
|
| 771 |
1.6.0 (2019-12-07) |
| 772 |
~~~~~~~~~~~~~~~~~~ |
| 773 |
|
| 774 |
* When pickling an Exception, also pickle its traceback and the Exception chain |
| 775 |
(``raise ... from ...``). Contributed by Guido Imperiale in |
| 776 |
`#53 <https://github.com/ionelmc/python-tblib/issues/53>`_. |
| 777 |
|
| 778 |
1.5.0 (2019-10-23) |
| 779 |
~~~~~~~~~~~~~~~~~~ |
| 780 |
|
| 781 |
* Added support for Python 3.8. Contributed by Victor Stinner in |
| 782 |
`#42 <https://github.com/ionelmc/python-tblib/issues/42>`_. |
| 783 |
* Removed support for end of life Python 3.4. |
| 784 |
* Few CI improvements and fixes. |
| 785 |
|
| 786 |
1.4.0 (2019-05-02) |
| 787 |
~~~~~~~~~~~~~~~~~~ |
| 788 |
|
| 789 |
* Removed support for end of life Python 3.3. |
| 790 |
* Fixed tests for Python 3.7. Contributed by Elliott Sales de Andrade in |
| 791 |
`#36 <https://github.com/ionelmc/python-tblib/issues/36>`_. |
| 792 |
* Fixed compatibility issue with Twised (``twisted.python.failure.Failure`` expected a ``co_code`` attribute). |
| 793 |
|
| 794 |
1.3.2 (2017-04-09) |
| 795 |
~~~~~~~~~~~~~~~~~~ |
| 796 |
|
| 797 |
* Add support for PyPy3.5-5.7.1-beta. Previously ``AttributeError: |
| 798 |
'Frame' object has no attribute 'clear'`` could be raised. See PyPy |
| 799 |
issue `#2532 <https://github.com/pypy/pypy/issues/2532>`_. |
| 800 |
|
| 801 |
1.3.1 (2017-03-27) |
| 802 |
~~~~~~~~~~~~~~~~~~ |
| 803 |
|
| 804 |
* Fixed handling for tracebacks due to exceeding the recursion limit. |
| 805 |
Fixes `#15 <https://github.com/ionelmc/python-tblib/issues/15>`_. |
| 806 |
|
| 807 |
1.3.0 (2016-03-08) |
| 808 |
~~~~~~~~~~~~~~~~~~ |
| 809 |
|
| 810 |
* Added ``Traceback.from_string``. |
| 811 |
|
| 812 |
1.2.0 (2015-12-18) |
| 813 |
~~~~~~~~~~~~~~~~~~ |
| 814 |
|
| 815 |
* Fixed handling for tracebacks from generators and other internal improvements |
| 816 |
and optimizations. Contributed by DRayX in `#10 <https://github.com/ionelmc/python-tblib/issues/10>`_ |
| 817 |
and `#11 <https://github.com/ionelmc/python-tblib/pull/11>`_. |
| 818 |
|
| 819 |
1.1.0 (2015-07-27) |
| 820 |
~~~~~~~~~~~~~~~~~~ |
| 821 |
|
| 822 |
* Added support for Python 2.6. Contributed by Arcadiy Ivanov in |
| 823 |
`#8 <https://github.com/ionelmc/python-tblib/pull/8>`_. |
| 824 |
|
| 825 |
1.0.0 (2015-03-30) |
| 826 |
~~~~~~~~~~~~~~~~~~ |
| 827 |
|
| 828 |
* Added ``to_dict`` method and ``from_dict`` classmethod on Tracebacks. |
| 829 |
Contributed by beckjake in `#5 <https://github.com/ionelmc/python-tblib/pull/5>`_. |