Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #140 from luzfcb/add-decorator
Browse files Browse the repository at this point in the history
Add first decorator
  • Loading branch information
gak committed May 28, 2015
2 parents 9a3d29a + 09ef7fa commit 0732a01
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ dist/

*.pyc
__pycache__
.idea

14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pycallgraph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions pycallgraph/decorators.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions pycallgraph/pycallgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')

Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[wheel]
universal = 1
39 changes: 39 additions & 0 deletions test/test_decorators.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0732a01

Please sign in to comment.