diff --git a/.gitignore b/.gitignore index 2523144..6b42ecb 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ dist/ *.pyc __pycache__ +.idea + diff --git a/.travis.yml b/.travis.yml index be55292..8ab1108 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,18 @@ language: python: - "2.7" - "3.3" + - "3.4" + - "pypy" +# pytest does not support python 3.5 +# https://bitbucket.org/pytest-dev/pytest/pull-request/296/astcall-signature-changed-on-35 +# - "nightly" + +matrix: + allow_failures: + - python: + - "pypy" + - python: + - "nigthly" env: global: @@ -16,7 +28,7 @@ before_install: install: - "pip install -r requirements/development.txt --use-mirrors" - - "if [[ $TRAVIS_PYTHON_VERSION == '3.3' ]]; then make 2to3; fi" + - "if [[ $TRAVIS_PYTHON_VERSION == '3.3' ]] || [[ $TRAVIS_PYTHON_VERSION == '3.4' ]] || [[ $TRAVIS_PYTHON_VERSION == 'nightly' ]]; then make 2to3; fi" script: - make tests diff --git a/pycallgraph/__init__.py b/pycallgraph/__init__.py index 7ac4695..6bfdd1d 100644 --- a/pycallgraph/__init__.py +++ b/pycallgraph/__init__.py @@ -14,6 +14,7 @@ from .pycallgraph import PyCallGraph from .exceptions import PyCallGraphException +from . import decorators from .config import Config from .globbing_filter import GlobbingFilter from .grouper import Grouper diff --git a/pycallgraph/decorators.py b/pycallgraph/decorators.py new file mode 100644 index 0000000..78b3ce0 --- /dev/null +++ b/pycallgraph/decorators.py @@ -0,0 +1,15 @@ +import functools + +from .pycallgraph import PyCallGraph + + +def trace(output=None, config=None): + def inner(func): + @functools.wraps(func) + def exec_func(*args, **kw_args): + with(PyCallGraph(output, config)): + return func(*args, **kw_args) + + return exec_func + + return inner diff --git a/pycallgraph/pycallgraph.py b/pycallgraph/pycallgraph.py index 1a03a50..6af5428 100644 --- a/pycallgraph/pycallgraph.py +++ b/pycallgraph/pycallgraph.py @@ -7,12 +7,11 @@ class PyCallGraph(object): - def __init__(self, output=None, config=None): '''output can be a single Output instance or an iterable with many of them. Example usage: - PyCallGraph(config=Config(), output=GraphvizOutput()) + PyCallGraph(output=GraphvizOutput(), config=Config()) ''' locale.setlocale(locale.LC_ALL, '') diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..5e40900 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[wheel] +universal = 1 diff --git a/test/test_decorators.py b/test/test_decorators.py new file mode 100644 index 0000000..8392a4c --- /dev/null +++ b/test/test_decorators.py @@ -0,0 +1,39 @@ +import pytest + +import pycallgraph +from pycallgraph import PyCallGraphException +from pycallgraph.output import GephiOutput, GraphvizOutput + + +@pycallgraph.decorators.trace(output=GraphvizOutput()) +def print_something(): + print("hello") + + +@pycallgraph.decorators.trace(output=GephiOutput()) +def print_foo(): + print("foo") + + +@pycallgraph.decorators.trace() +def print_bar(): + print("bar") + + +def test_trace_decorator_graphviz_output(): + print_something() + + +def test_trace_decorator_gephi_output(): + print_foo() + + +def test_trace_decorator_parameter(): + with pytest.raises(PyCallGraphException): + print_bar() + + +if __name__ == "__main__": + test_trace_decorator_graphviz_output() + test_trace_decorator_gephi_output() + test_trace_decorator_parameter()