Commit dc8030a6 authored by Tiago Peixoto's avatar Tiago Peixoto

Deprecate custom _wraps() decorator for Python 3

This fixes issue #547
parent 2cbad3d4
......@@ -143,7 +143,7 @@ else:
def _to_str(x):
return x
from .decorators import _wraps, _require, _attrs, _limit_args, _copy_func
from .decorators import _require, _attrs, _limit_args, _copy_func
from inspect import ismethod
__all__ = ["Graph", "GraphView", "Vertex", "Edge", "VertexBase", "EdgeBase",
......
......@@ -48,33 +48,36 @@ def exec_function(source, filename, global_map):
""","<exec_function>", "exec"))
def _wraps(func):
"""This decorator works like the functools.wraps meta-decorator, but
also preserves the function's argument signature. This uses eval, and is
thus a bit of a hack, but there no better way I know of to do this."""
def decorate(f):
argspec = inspect.getargspec(func)
___wrap_defaults = defaults = argspec[-1]
if defaults is not None:
def_string = ["___wrap_defaults[%d]" % d for
d in range(len(defaults))]
def_names = argspec[0][-len(defaults):]
else:
def_string = None
def_names = None
args_call = inspect.formatargspec(argspec[0], defaults=def_names)
argspec = inspect.formatargspec(argspec[0], defaults=def_string)
argspec = argspec.lstrip("(").rstrip(")")
wf = "def %s(%s):\n return f%s\n" % \
(func.__name__, argspec, args_call)
if def_string is not None:
for d in def_string:
wf = wf.replace("'%s'" % d, "%s" % d)
for d in def_names:
wf = wf.replace("'%s'" % d, "%s" % d)
exec_function(wf, __file__, locals())
return functools.wraps(func)(locals()[func.__name__])
return decorate
if sys.hexversion > 0x03000000:
_wraps = functools.wraps
else:
def _wraps(func):
"""This decorator works like the functools.wraps meta-decorator, but
also preserves the function's argument signature. This uses eval, and is
thus a bit of a hack, but there no better way I know of to do this."""
def decorate(f):
argspec = inspect.getargspec(func)
___wrap_defaults = defaults = argspec[-1]
if defaults is not None:
def_string = ["___wrap_defaults[%d]" % d for
d in range(len(defaults))]
def_names = argspec[0][-len(defaults):]
else:
def_string = None
def_names = None
args_call = inspect.formatargspec(argspec[0], defaults=def_names)
argspec = inspect.formatargspec(argspec[0], defaults=def_string)
argspec = argspec.lstrip("(").rstrip(")")
wf = "def %s(%s):\n return f%s\n" % \
(func.__name__, argspec, args_call)
if def_string is not None:
for d in def_string:
wf = wf.replace("'%s'" % d, "%s" % d)
for d in def_names:
wf = wf.replace("'%s'" % d, "%s" % d)
exec_function(wf, __file__, locals())
return functools.wraps(func)(locals()[func.__name__])
return decorate
def _attrs(**kwds):
......
......@@ -69,8 +69,7 @@ Contents
from __future__ import division, absolute_import, print_function
from .. import _degree, _prop, Graph, GraphView, _limit_args, _get_rng, \
PropertyMap
from .. import _degree, _prop, Graph, GraphView, _get_rng, PropertyMap
from .. stats import label_self_loops
import numpy
import numpy.random
......
......@@ -36,8 +36,6 @@ import warnings
from .. import group_vector_property, ungroup_vector_property, Vector_size_t, \
perfect_prop_hash
from .. decorators import _wraps
from .. dl_import import dl_import
dl_import("from . import libgraph_tool_inference as libinference")
......
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