ionelmc / python-tblib

Serialization library for Exceptions and Tracebacks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with generators

DRayX opened this issue · comments

commented

This library doesn't work if there are any generators in the stack as the compiled raise __traceback_maker actually executes without raising an error if flag 0x20 (CO_GENERATOR) is set.

Simple repro:

import sys
import tblib
import traceback

def foo():
  raise RuntimeError()
  yield

try:
  next(foo())
except:
  assert traceback.extract_tb(sys.exc_info()[2]) == traceback.extract_tb(
      tblib.Traceback(sys.exc_info()[2]).as_traceback())

In general, mucking around with code attributes isn't really safe, and in the worst case could potentially cause a segfault when the code is executed. The filename and name (the two that actually matter as they are used by the exception printing system) should be safe. I wouldn't touch nlocals, stacksize, or flags though. I have seen instances where the overridden nlocals causes the exec itself to throw because there are no locals which screws up the line numbers in the tracebacks.