From 05f87a939738e5f7ae461c89f316cf46e1bd5d49 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 8 Jun 2018 17:24:46 +0200 Subject: [PATCH 1/7] fix(style): pep8 flake8 was complaining about builtin-named args --- pycallgraph/output/output.py | 2 +- pycallgraph/pycallgraph.py | 2 +- pycallgraph/tracer.py | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pycallgraph/output/output.py b/pycallgraph/output/output.py index 662d5630..f6f3efa5 100644 --- a/pycallgraph/output/output.py +++ b/pycallgraph/output/output.py @@ -116,7 +116,7 @@ def debug(self, text): self.processor.config.log_debug(text) @classmethod - def add_output_file(cls, subparser, defaults, help): + def add_output_file(cls, subparser, defaults, help): # noqa: A002 subparser.add_argument( '-o', '--output-file', type=str, default=defaults.output_file, help=help, diff --git a/pycallgraph/pycallgraph.py b/pycallgraph/pycallgraph.py index 6af54287..73e018bb 100644 --- a/pycallgraph/pycallgraph.py +++ b/pycallgraph/pycallgraph.py @@ -33,7 +33,7 @@ def __init__(self, output=None, config=None): def __enter__(self): self.start() - def __exit__(self, type, value, traceback): + def __exit__(self, type, value, traceback): # noqa: A002 self.done() def get_tracer_class(self): diff --git a/pycallgraph/tracer.py b/pycallgraph/tracer.py index ffcab158..f83cf424 100644 --- a/pycallgraph/tracer.py +++ b/pycallgraph/tracer.py @@ -372,4 +372,5 @@ def wrapper(*rest): return wrapper + inspect.getmodule = simple_memoize(inspect.getmodule) From 658bd0c7f6bedfb1c453df1de7632d40957c68ed Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 8 Jun 2018 17:43:24 +0200 Subject: [PATCH 2/7] fix(TCs): flake complained about import * (F405) --- setup.py | 18 +++++++++--------- test/conftest.py | 3 ++- test/helpers.py | 7 ------- test/test_color.py | 4 +++- test/test_config.py | 2 +- test/test_gephi.py | 9 +++++++-- test/test_graphviz.py | 9 +++++++-- test/test_output.py | 3 ++- test/test_pycallgraph.py | 5 ++++- test/test_script.py | 2 -- test/test_trace_processor.py | 14 ++++++++------ test/test_util.py | 2 +- 12 files changed, 44 insertions(+), 34 deletions(-) diff --git a/setup.py b/setup.py index a2a23800..4641eb38 100755 --- a/setup.py +++ b/setup.py @@ -1,13 +1,13 @@ #!/usr/bin/env python -from os import path -from setuptools import setup import sys +from setuptools import setup from setuptools.command.test import test as TestCommand import pycallgraph + # Only install the man page if the correct directory exists # XXX: Commented because easy_install doesn't like it #man_path = '/usr/share/man/man1/' @@ -15,8 +15,8 @@ # data_files=[['/usr/share/man/man1/', ['man/pycallgraph.1']]] #else: # data_files=None +data_files = None -data_files=None class PyTest(TestCommand): @@ -30,6 +30,7 @@ def run_tests(self): errno = pytest.main(self.test_args) sys.exit(errno) + setup( name='pycallgraph', version=pycallgraph.__version__, @@ -45,15 +46,15 @@ def run_tests(self): use_2to3=True, # TODO: Update download_url - download_url = - 'http://pycallgraph.slowchop.com/files/download/pycallgraph-%s.tar.gz' % \ - pycallgraph.__version__, + download_url= + 'http://pycallgraph.slowchop.com/files/download/pycallgraph-%s.tar.gz' % + pycallgraph.__version__, # Testing tests_require=['pytest'], - cmdclass = {'test': PyTest}, + cmdclass={'test': PyTest}, - classifiers = [ + classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: GNU General Public License (GPL)', @@ -68,4 +69,3 @@ def run_tests(self): 'Topic :: Software Development :: Debuggers', ], ) - diff --git a/test/conftest.py b/test/conftest.py index b54e2531..b8b7a0f5 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,6 +1,7 @@ +from pycallgraph import PyCallGraph, Config import tempfile -from helpers import * +import pytest @pytest.fixture(scope='module') diff --git a/test/helpers.py b/test/helpers.py index 4ea4abbb..0bbee370 100644 --- a/test/helpers.py +++ b/test/helpers.py @@ -1,13 +1,6 @@ # flake8: noqa import time -import pytest - -import fix_path -from pycallgraph import * -from pycallgraph.tracer import * -from pycallgraph.output import * - def wait_100ms(): time.sleep(0.1) diff --git a/test/test_color.py b/test/test_color.py index 0d33fc51..535a577a 100644 --- a/test/test_color.py +++ b/test/test_color.py @@ -1,4 +1,6 @@ -from helpers import * +from pycallgraph import ColorException, Color + +import pytest def test_bad_range(): diff --git a/test/test_config.py b/test/test_config.py index 5d5335fd..ce0cbc22 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -1,4 +1,4 @@ -from helpers import * +from pycallgraph.config import Config def test_init(): diff --git a/test/test_gephi.py b/test/test_gephi.py index 15dd7087..f3f2dfad 100644 --- a/test/test_gephi.py +++ b/test/test_gephi.py @@ -1,5 +1,10 @@ -from helpers import * -from calls import * +from pycallgraph import PyCallGraph +from pycallgraph.output.gephi import GephiOutput +import os + +import pytest + +from calls import one_nop @pytest.fixture diff --git a/test/test_graphviz.py b/test/test_graphviz.py index 35d8d8eb..2669aede 100644 --- a/test/test_graphviz.py +++ b/test/test_graphviz.py @@ -1,5 +1,10 @@ -from helpers import * -from calls import * +from pycallgraph.output.graphviz import GraphvizOutput +from pycallgraph.pycallgraph import PyCallGraph +import os + +import pytest + +from calls import one_nop @pytest.fixture diff --git a/test/test_output.py b/test/test_output.py index 9149e5c8..6b17775b 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -1,4 +1,5 @@ -from helpers import * +from pycallgraph import Config +from pycallgraph.output import Output def test_set_config(): diff --git a/test/test_pycallgraph.py b/test/test_pycallgraph.py index bdc7dc47..76a2fbae 100644 --- a/test/test_pycallgraph.py +++ b/test/test_pycallgraph.py @@ -1,4 +1,7 @@ -from helpers import * +from pycallgraph.exceptions import PyCallGraphException +from pycallgraph.tracer import AsyncronousTracer, SyncronousTracer + +import pytest def test_start_no_outputs(pycg): diff --git a/test/test_script.py b/test/test_script.py index 0aa90169..fad47e16 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -1,7 +1,5 @@ import subprocess -from helpers import * - def execute(arguments): command = 'PYTHONPATH=. scripts/pycallgraph ' + arguments diff --git a/test/test_trace_processor.py b/test/test_trace_processor.py index bdf16ccf..7b8a0cf3 100644 --- a/test/test_trace_processor.py +++ b/test/test_trace_processor.py @@ -1,9 +1,11 @@ +from pycallgraph.tracer import TraceProcessor import re import sys -from helpers import * -import calls -from pycallgraph.tracer import TraceProcessor +import pytest + +from pycallgraph import Config +from calls import one_nop, nop @pytest.fixture @@ -20,7 +22,7 @@ def test_empty(trace_processor): def test_nop(trace_processor): sys.settrace(trace_processor.process) - calls.nop() + nop() sys.settrace(None) assert trace_processor.call_dict == { @@ -32,7 +34,7 @@ def test_nop(trace_processor): def test_one_nop(trace_processor): sys.settrace(trace_processor.process) - calls.one_nop() + one_nop() sys.settrace(None) assert trace_processor.call_dict == { @@ -45,7 +47,7 @@ def stdlib_trace(trace_processor, include_stdlib): trace_processor.config = Config(include_stdlib=include_stdlib) sys.settrace(trace_processor.process) re.match("asdf", "asdf") - calls.one_nop() + one_nop() sys.settrace(None) return trace_processor.call_dict diff --git a/test/test_util.py b/test/test_util.py index 084c6f31..bf8cf09e 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -1,4 +1,4 @@ -from helpers import * +from pycallgraph.util import Util def test_human_readable_biyte(): From 3a13f6644eb4d2df35264231fae2e593c6fe054d Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 8 Jun 2018 18:13:15 +0200 Subject: [PATCH 3/7] fix(TCs): flake8 complained about examples --- Makefile | 2 +- .../example_with_submodules/example_with_submodules.py | 1 + examples/graphviz/recursive.py | 1 + examples/graphviz/regexp.py | 5 +++-- 4 files changed, 6 insertions(+), 3 deletions(-) mode change 100755 => 100644 examples/graphviz/regexp.py diff --git a/Makefile b/Makefile index 702bda91..e9abc768 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ tests: flake8 --exclude=__init__.py,memory_profiler.py pycallgraph flake8 --ignore=F403 test - flake8 examples + flake8 examples --exclude=all.py doc: cd docs/examples && ./generate.py diff --git a/examples/graphviz/example_with_submodules/example_with_submodules.py b/examples/graphviz/example_with_submodules/example_with_submodules.py index 2fb6d042..0324c4bd 100644 --- a/examples/graphviz/example_with_submodules/example_with_submodules.py +++ b/examples/graphviz/example_with_submodules/example_with_submodules.py @@ -9,5 +9,6 @@ def main(): s2 = SubmoduleTwo() s2.report() + if __name__ == "__main__": main() diff --git a/examples/graphviz/recursive.py b/examples/graphviz/recursive.py index d61a1b15..4043878d 100755 --- a/examples/graphviz/recursive.py +++ b/examples/graphviz/recursive.py @@ -20,5 +20,6 @@ def main(): for a in xrange(1, 10): factorial(a) + if __name__ == '__main__': main() diff --git a/examples/graphviz/regexp.py b/examples/graphviz/regexp.py old mode 100755 new mode 100644 index 63bb2c23..4054668b --- a/examples/graphviz/regexp.py +++ b/examples/graphviz/regexp.py @@ -15,11 +15,11 @@ def main(): config = Config(include_stdlib=True) with PyCallGraph(output=graphviz, config=config): - reo = compile() + reo = compile_regex() match(reo) -def compile(): +def compile_regex(): return re.compile('^[abetors]*$') @@ -41,5 +41,6 @@ def words(): 'abrasives', ] + if __name__ == '__main__': main() From 8c8bb551e1ed7a7a9206e01284653ce59e5e10f4 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 8 Jun 2018 15:09:56 +0100 Subject: [PATCH 4/7] feat: PY3.5+ compatibility: iteritems() --- pycallgraph/config.py | 8 +++++++- pycallgraph/output/graphviz.py | 11 +++++++++-- pycallgraph/output/output.py | 12 +++++++++--- pycallgraph/tracer.py | 16 +++++++++++----- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/pycallgraph/config.py b/pycallgraph/config.py index 2bce9301..54d717ce 100755 --- a/pycallgraph/config.py +++ b/pycallgraph/config.py @@ -1,4 +1,5 @@ import argparse +import operator import sys from .output import outputters @@ -6,6 +7,11 @@ from .grouper import Grouper +iteritems = operator.methodcaller('iteritems' + if sys.version_info < (3, ) else + 'items') + + class Config(object): '''Handles configuration settings for pycallgraph, tracer, and each output module. It also handles command line arguments. @@ -38,7 +44,7 @@ def __init__(self, **kwargs): self.did_init = True # Update the defaults with anything from kwargs - [setattr(self, k, v) for k, v in kwargs.iteritems()] + [setattr(self, k, v) for k, v in iteritems(kwargs)] self.create_parser() diff --git a/pycallgraph/output/graphviz.py b/pycallgraph/output/graphviz.py index e875e398..e04c2a30 100644 --- a/pycallgraph/output/graphviz.py +++ b/pycallgraph/output/graphviz.py @@ -1,7 +1,9 @@ from __future__ import division import tempfile +import operator import os +import sys import textwrap import subprocess as sub @@ -11,6 +13,11 @@ from .output import Output +iteritems = operator.methodcaller('iteritems' + if sys.version_info < (3, ) else + 'items') + + class GraphvizOutput(Output): def __init__(self, **kwargs): @@ -151,7 +158,7 @@ def generate(self): def attrs_from_dict(self, d): output = [] - for attr, val in d.iteritems(): + for attr, val in iteritems(d): output.append('%s = "%s"' % (attr, val)) return ', '.join(output) @@ -167,7 +174,7 @@ def edge(self, edge, attr): def generate_attributes(self): output = [] - for section, attrs in self.graph_attributes.iteritems(): + for section, attrs in iteritems(self.graph_attributes): output.append('{0} [ {1} ];'.format( section, self.attrs_from_dict(attrs), )) diff --git a/pycallgraph/output/output.py b/pycallgraph/output/output.py index f6f3efa5..c88768f7 100644 --- a/pycallgraph/output/output.py +++ b/pycallgraph/output/output.py @@ -1,10 +1,16 @@ -import re +import operator import os +import re +import sys from distutils.spawn import find_executable from ..exceptions import PyCallGraphException from ..color import Color +iteritems = operator.methodcaller('iteritems' + if sys.version_info < (3, ) else + 'items') + class Output(object): '''Base class for all outputters.''' @@ -16,14 +22,14 @@ def __init__(self, **kwargs): self.edge_label_func = self.edge_label # Update the defaults with anything from kwargs - [setattr(self, k, v) for k, v in kwargs.iteritems()] + [setattr(self, k, v) for k, v in iteritems(kwargs)] def set_config(self, config): ''' This is a quick hack to move the config variables set in Config into the output module config variables. ''' - for k, v in config.__dict__.iteritems(): + for k, v in iteritems(config.__dict__): if hasattr(self, k) and \ callable(getattr(self, k)): continue diff --git a/pycallgraph/tracer.py b/pycallgraph/tracer.py index f83cf424..42319a81 100644 --- a/pycallgraph/tracer.py +++ b/pycallgraph/tracer.py @@ -1,8 +1,9 @@ from __future__ import division import inspect -import sys +import operator import os +import sys import time from distutils import sysconfig from collections import defaultdict @@ -15,6 +16,11 @@ from .util import Util +iteritems = operator.methodcaller('iteritems' + if sys.version_info < (3, ) else + 'items') + + class SyncronousTracer(object): def __init__(self, outputs, config): @@ -294,7 +300,7 @@ def groups(self): grp = defaultdict(list) for node in self.nodes(): grp[node.group].append(node) - for g in grp.iteritems(): + for g in iteritems(grp): yield g def stat_group_from_func(self, func, calls): @@ -312,14 +318,14 @@ def stat_group_from_func(self, func, calls): return stat_group def nodes(self): - for func, calls in self.func_count.iteritems(): + for func, calls in iteritems(self.func_count): yield self.stat_group_from_func(func, calls) def edges(self): - for src_func, dests in self.call_dict.iteritems(): + for src_func, dests in iteritems(self.call_dict): if not src_func: continue - for dst_func, calls in dests.iteritems(): + for dst_func, calls in iteritems(dests): edge = self.stat_group_from_func(dst_func, calls) edge.src_func = src_func edge.dst_func = dst_func From c07796c667419978f5a0d4b0ad4f872739dd1d12 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 8 Jun 2018 18:15:42 +0200 Subject: [PATCH 5/7] feat: PY3.5+ compatibility: xrange() --- examples/gephi/basic.py | 2 +- examples/graphviz/basic.py | 2 +- examples/graphviz/recursive.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/gephi/basic.py b/examples/gephi/basic.py index 1f15fccb..14b15f20 100755 --- a/examples/gephi/basic.py +++ b/examples/gephi/basic.py @@ -34,7 +34,7 @@ def main(): with PyCallGraph(output=gephi): person = Person() - for a in xrange(10): + for a in range(10): person.add_banana(Banana()) person.eat_bananas() diff --git a/examples/graphviz/basic.py b/examples/graphviz/basic.py index 6404af59..63b476ac 100755 --- a/examples/graphviz/basic.py +++ b/examples/graphviz/basic.py @@ -34,7 +34,7 @@ def main(): with PyCallGraph(output=graphviz): person = Person() - for a in xrange(10): + for a in range(10): person.add_banana(Banana()) person.eat_bananas() diff --git a/examples/graphviz/recursive.py b/examples/graphviz/recursive.py index 4043878d..749a7372 100755 --- a/examples/graphviz/recursive.py +++ b/examples/graphviz/recursive.py @@ -17,7 +17,7 @@ def main(): graphviz.output_file = 'recursive.png' with PyCallGraph(output=graphviz): - for a in xrange(1, 10): + for a in range(1, 10): factorial(a) From 6329b2600ca2c24d58328dc129c767ebe3d95922 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 8 Jun 2018 18:24:24 +0200 Subject: [PATCH 6/7] chore(travis): add PY35, PY36 PY35-dev into tested environments --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index d3c35bf8..b4f7786a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,9 @@ python: - "2.7" - "3.3" - "3.4" + - "3.5" + - "3.6" + - "3.7-dev" - "pypy" # pytest does not support python 3.5 # https://bitbucket.org/pytest-dev/pytest/pull-request/296/astcall-signature-changed-on-35 From a1029a4c9537a87acd3e6c93a38ed3487e43c4c4 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Fri, 8 Jun 2018 18:50:38 +0200 Subject: [PATCH 7/7] fix(travis): allow py7-dev to fail due to yaml/pyyaml#126 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index b4f7786a..dc46e067 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,7 @@ matrix: allow_failures: - python: - "pypy" + - '3.7-dev' # Due to PyYAML (from `coveralls`) - python: - "nigthly"