Commit 26f29b6c authored by Jason Madden's avatar Jason Madden

Merge pull request #765 from gevent/tblib-update

Update tblib to 1.3.0
parents d17b5e0b 9a5fd30c
......@@ -8,6 +8,7 @@
==================
- Update libev to version 4.22.
- Update tblib to 1.3.0.
1.1.0 (Mar 5, 2016)
===================
......
# -*- coding: utf-8 -*-
# A vendored version of part of https://github.com/ionelmc/python-tblib
####
# Copyright (c) 2013-2014, Ionel Cristian Mărieș
# Copyright (c) 2013-2016, Ionel Cristian Mărieș
# All rights reserved.
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
......@@ -51,6 +51,7 @@ def _init_ugly_crap():
# regular python
class _PyObject(ctypes.Structure):
pass
_PyObject._fields_ = [
('ob_refcnt', _Py_ssize_t),
('ob_type', ctypes.POINTER(_PyObject))
......@@ -60,6 +61,7 @@ def _init_ugly_crap():
if hasattr(sys, 'getobjects'):
class _PyObject(ctypes.Structure):
pass
_PyObject._fields_ = [
('_ob_next', ctypes.POINTER(_PyObject)),
('_ob_prev', ctypes.POINTER(_PyObject)),
......@@ -69,6 +71,7 @@ def _init_ugly_crap():
class _Traceback(_PyObject):
pass
_Traceback._fields_ = [
('tb_next', ctypes.POINTER(_Traceback)),
('tb_frame', ctypes.POINTER(_PyObject)),
......@@ -78,8 +81,7 @@ def _init_ugly_crap():
def tb_set_next(tb, next):
"""Set the tb_next attribute of a traceback object."""
if not (isinstance(tb, TracebackType) and
(next is None or isinstance(next, TracebackType))):
if not (isinstance(tb, TracebackType) and (next is None or isinstance(next, TracebackType))):
raise TypeError('tb_set_next arguments must be traceback objects')
obj = _Traceback.from_address(id(tb))
if tb.tb_next is not None:
......@@ -94,44 +96,51 @@ def _init_ugly_crap():
return tb_set_next
tb_set_next = None
# try:
# if platform.python_implementation() == 'CPython':
# #tb_set_next = _init_ugly_crap()
# tb_set_next = None
# except Exception as exc:
# sys.stderr.write("Failed to initialize cpython support: {!r}".format(exc))
# del _init_ugly_crap
#try:
# if platform.python_implementation() == 'CPython':
# tb_set_next = _init_ugly_crap()
#except Exception as exc:
# sys.stderr.write("Failed to initialize cpython support: {!r}".format(exc))
#del _init_ugly_crap
# __init__.py
import re
from types import CodeType
from types import TracebackType
try:
from __pypy__ import tproxy
except ImportError:
tproxy = None
#if not tb_set_next and not tproxy:
# raise ImportError("Cannot use tblib. Runtime not supported.")
from types import CodeType
from types import TracebackType
__version__ = '1.3.0'
__all__ = 'Traceback',
PY3 = sys.version_info[0] == 3
FRAME_RE = re.compile(r'^\s*File "(?P<co_filename>.+)", line (?P<tb_lineno>\d+)(, in (?P<co_name>.+))?$')
class _AttrDict(dict):
def __getattr__(self, attr):
return self[attr]
__slots__ = ()
__getattr__ = dict.__getitem__
# noinspection PyPep8Naming
class __traceback_maker(Exception):
pass
class TracebackParseError(Exception):
pass
class Code(object):
def __init__(self, code):
self.co_filename = code.co_filename
self.co_name = code.co_name
# gevent: copy more attributes
self.co_nlocals = code.co_nlocals
self.co_stacksize = code.co_stacksize
self.co_flags = code.co_flags
......@@ -140,16 +149,19 @@ class Code(object):
class Frame(object):
def __init__(self, frame):
# gevent: python 2.6 syntax fix
self.f_globals = {'__file__': frame.f_globals.get('__file__'),
'__name__': frame.f_globals.get('__name__')}
self.f_globals = dict([
(k, v)
for k, v in frame.f_globals.items()
if k in ("__file__", "__name__")
])
self.f_code = Code(frame.f_code)
class Traceback(object):
def __init__(self, tb):
self.tb_frame = Frame(tb.tb_frame)
self.tb_lineno = tb.tb_lineno
# noinspection SpellCheckingInspection
self.tb_lineno = int(tb.tb_lineno)
if tb.tb_next is None:
self.tb_next = None
else:
......@@ -163,23 +175,22 @@ class Traceback(object):
code = compile('\n' * (self.tb_lineno - 1) + 'raise __traceback_maker', self.tb_frame.f_code.co_filename, 'exec')
if PY3:
code = CodeType(
0, 0,
f_code.co_nlocals, f_code.co_stacksize, f_code.co_flags,
0, code.co_kwonlyargcount,
code.co_nlocals, code.co_stacksize, code.co_flags,
code.co_code, code.co_consts, code.co_names, code.co_varnames,
f_code.co_filename, f_code.co_name,
code.co_firstlineno, b"",
(), ()
code.co_firstlineno, code.co_lnotab, (), ()
)
else:
code = CodeType(
0,
f_code.co_nlocals, f_code.co_stacksize, f_code.co_flags,
code.co_nlocals, code.co_stacksize, code.co_flags,
code.co_code, code.co_consts, code.co_names, code.co_varnames,
f_code.co_filename.encode(), f_code.co_name.encode(),
code.co_firstlineno, b"",
(), ()
code.co_firstlineno, code.co_lnotab, (), ()
)
# noinspection PyBroadException
try:
exec(code, self.tb_frame.f_globals, {})
except:
......@@ -188,10 +199,13 @@ class Traceback(object):
try:
return tb
finally:
# gevent: don't leak the traceback objects, this
# makes our leaktests fail
del tb
else:
raise RuntimeError("Cannot re-create traceback !")
# noinspection SpellCheckingInspection
def __tproxy_handler(self, operation, *args, **kwargs):
if operation in ('__getattribute__', '__getattr__'):
if args[0] == 'tb_next':
......@@ -201,6 +215,86 @@ class Traceback(object):
else:
return getattr(self, operation)(*args, **kwargs)
def to_dict(self):
"""Convert a Traceback into a dictionary representation"""
if self.tb_next is None:
tb_next = None
else:
tb_next = self.tb_next.to_dict()
code = {
'co_filename': self.tb_frame.f_code.co_filename,
'co_name': self.tb_frame.f_code.co_name,
}
frame = {
'f_globals': self.tb_frame.f_globals,
'f_code': code,
}
return {
'tb_frame': frame,
'tb_lineno': self.tb_lineno,
'tb_next': tb_next,
}
@classmethod
def from_dict(cls, dct):
if dct['tb_next']:
tb_next = cls.from_dict(dct['tb_next'])
else:
tb_next = None
code = _AttrDict(
co_filename=dct['tb_frame']['f_code']['co_filename'],
co_name=dct['tb_frame']['f_code']['co_name'],
)
frame = _AttrDict(
f_globals=dct['tb_frame']['f_globals'],
f_code=code,
)
tb = _AttrDict(
tb_frame=frame,
tb_lineno=dct['tb_lineno'],
tb_next=tb_next,
)
return cls(tb)
@classmethod
def from_string(cls, string, strict=True):
frames = []
header = strict
for line in string.splitlines():
line = line.rstrip()
if header:
if line == 'Traceback (most recent call last):':
header = False
continue
frame_match = FRAME_RE.match(line)
if frame_match:
frames.append(frame_match.groupdict())
elif line.startswith(' '):
pass
elif strict:
break # traceback ended
if frames:
previous = None
for frame in reversed(frames):
previous = _AttrDict(
frame,
tb_frame=_AttrDict(
frame,
f_globals=_AttrDict(
__file__=frame['co_filename'],
__name__='?',
),
f_code=_AttrDict(frame),
),
tb_next=previous,
)
return cls(previous)
else:
raise TracebackParseError("Could not find any frames in %r." % string)
# pickling_support.py
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment