From 27ef16a4b813d21c7bfcd63a6d1da7664ad5d42d Mon Sep 17 00:00:00 2001 From: Samedhan Karamese Date: Thu, 20 Sep 2018 22:52:05 +0300 Subject: [PATCH 1/6] 2to3 fixes --- examples/gephi/basic.py | 2 +- examples/gephi/large.py | 7 ++++++- examples/graphviz/all.py | 7 ++++++- examples/graphviz/basic.py | 2 +- examples/graphviz/colors.py | 5 ++++- .../example_with_submodules.py | 4 ++-- .../graphviz/example_with_submodules/submodule_two.py | 2 +- examples/graphviz/large.py | 7 ++++++- examples/graphviz/recursive.py | 2 +- pycallgraph/config.py | 3 ++- pycallgraph/output/graphviz.py | 6 ++++-- pycallgraph/output/output.py | 7 +++++-- pycallgraph/tracer.py | 11 +++++++---- 13 files changed, 46 insertions(+), 19 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/gephi/large.py b/examples/gephi/large.py index bedc39eb..953a13af 100755 --- a/examples/gephi/large.py +++ b/examples/gephi/large.py @@ -13,7 +13,12 @@ def main(): gephi.output_file = 'large.gdf' with PyCallGraph(output=gephi): - from urllib2 import urlopen + + try: + from urllib2 import urlopen + except ImportError: # Python 3.x + from urllib.request import urlopen + from xml.dom.minidom import parseString parseString(urlopen('http://w3.org/').read()) diff --git a/examples/graphviz/all.py b/examples/graphviz/all.py index 68e65661..d9ac6f47 100755 --- a/examples/graphviz/all.py +++ b/examples/graphviz/all.py @@ -2,6 +2,7 @@ ''' Execute all pycallgraph examples in this directory. ''' +import sys from glob import glob @@ -9,4 +10,8 @@ examples.remove('all.py') for example in examples: print(example) - execfile(example) + + if sys.version_info[0] < 3: + execfile(example) + else: + exec(open(example).read()) 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/colors.py b/examples/graphviz/colors.py index 56c67fa3..e38cfcbd 100755 --- a/examples/graphviz/colors.py +++ b/examples/graphviz/colors.py @@ -56,7 +56,10 @@ def main(): ) pycallgraph.start() - import HTMLParser # noqa + try: + import HTMLParser + except ImportError: # Python 3.x + import html.parser# noqa pycallgraph.stop() # Set the edge colour to black for all examples diff --git a/examples/graphviz/example_with_submodules/example_with_submodules.py b/examples/graphviz/example_with_submodules/example_with_submodules.py index 2fb6d042..c42eaa50 100644 --- a/examples/graphviz/example_with_submodules/example_with_submodules.py +++ b/examples/graphviz/example_with_submodules/example_with_submodules.py @@ -1,5 +1,5 @@ -from submodule_one import SubmoduleOne -from submodule_two import SubmoduleTwo +from .submodule_one import SubmoduleOne +from .submodule_two import SubmoduleTwo def main(): diff --git a/examples/graphviz/example_with_submodules/submodule_two.py b/examples/graphviz/example_with_submodules/submodule_two.py index d3c0ac6c..463e51dc 100644 --- a/examples/graphviz/example_with_submodules/submodule_two.py +++ b/examples/graphviz/example_with_submodules/submodule_two.py @@ -1,4 +1,4 @@ -from helpers import helper +from .helpers import helper class SubmoduleTwo(object): diff --git a/examples/graphviz/large.py b/examples/graphviz/large.py index 069439a2..c9a76958 100755 --- a/examples/graphviz/large.py +++ b/examples/graphviz/large.py @@ -15,7 +15,12 @@ def main(): config = Config(include_stdlib=True) with PyCallGraph(output=graphviz, config=config): - from urllib2 import urlopen + + try: + from urllib2 import urlopen + except ImportError: # Python 3.x + from urllib.request import urlopen + from xml.dom.minidom import parseString parseString(urlopen('http://w3.org/').read()) diff --git a/examples/graphviz/recursive.py b/examples/graphviz/recursive.py index d61a1b15..fc289a9c 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) if __name__ == '__main__': diff --git a/pycallgraph/config.py b/pycallgraph/config.py index 2bce9301..3861d726 100755 --- a/pycallgraph/config.py +++ b/pycallgraph/config.py @@ -1,5 +1,6 @@ import argparse import sys +import six from .output import outputters from .globbing_filter import GlobbingFilter @@ -38,7 +39,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 six.iteritems(kwargs)] self.create_parser() diff --git a/pycallgraph/output/graphviz.py b/pycallgraph/output/graphviz.py index e875e398..ea95ca8e 100644 --- a/pycallgraph/output/graphviz.py +++ b/pycallgraph/output/graphviz.py @@ -5,6 +5,8 @@ import textwrap import subprocess as sub +import six + from ..metadata import __version__ from ..exceptions import PyCallGraphException from ..color import Color @@ -151,7 +153,7 @@ def generate(self): def attrs_from_dict(self, d): output = [] - for attr, val in d.iteritems(): + for attr, val in six.iteritems(d): output.append('%s = "%s"' % (attr, val)) return ', '.join(output) @@ -167,7 +169,7 @@ def edge(self, edge, attr): def generate_attributes(self): output = [] - for section, attrs in self.graph_attributes.iteritems(): + for section, attrs in six.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 662d5630..7d730a1d 100644 --- a/pycallgraph/output/output.py +++ b/pycallgraph/output/output.py @@ -1,11 +1,14 @@ import re import os +import six + from distutils.spawn import find_executable from ..exceptions import PyCallGraphException from ..color import Color + class Output(object): '''Base class for all outputters.''' @@ -16,14 +19,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 six.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 six.iteritems(config.__dict__): if hasattr(self, k) and \ callable(getattr(self, k)): continue diff --git a/pycallgraph/tracer.py b/pycallgraph/tracer.py index ffcab158..e09e0c9e 100644 --- a/pycallgraph/tracer.py +++ b/pycallgraph/tracer.py @@ -7,6 +7,9 @@ from distutils import sysconfig from collections import defaultdict from threading import Thread + +import six + try: from Queue import Queue, Empty except ImportError: @@ -294,7 +297,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 six.iteritems(grp): yield g def stat_group_from_func(self, func, calls): @@ -312,14 +315,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 six.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 six.iteritems(self.call_dict): if not src_func: continue - for dst_func, calls in dests.iteritems(): + for dst_func, calls in six.iteritems(dests): edge = self.stat_group_from_func(dst_func, calls) edge.src_func = src_func edge.dst_func = dst_func From a636c79e71c90b2e9a50d9007f9b62a3e1928ecc Mon Sep 17 00:00:00 2001 From: Samedhan Karamese Date: Thu, 20 Sep 2018 23:08:41 +0300 Subject: [PATCH 2/6] pep8 --- examples/graphviz/colors.py | 2 +- pycallgraph/output/output.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/graphviz/colors.py b/examples/graphviz/colors.py index e38cfcbd..64130320 100755 --- a/examples/graphviz/colors.py +++ b/examples/graphviz/colors.py @@ -59,7 +59,7 @@ def main(): try: import HTMLParser except ImportError: # Python 3.x - import html.parser# noqa + import html.parser # noqa pycallgraph.stop() # Set the edge colour to black for all examples diff --git a/pycallgraph/output/output.py b/pycallgraph/output/output.py index 7d730a1d..8b7bc840 100644 --- a/pycallgraph/output/output.py +++ b/pycallgraph/output/output.py @@ -8,7 +8,6 @@ from ..color import Color - class Output(object): '''Base class for all outputters.''' From 470718f70e0d32e2c957310aa12b2b0c2ec20a5b Mon Sep 17 00:00:00 2001 From: Samedhan Karamese Date: Thu, 20 Sep 2018 23:18:25 +0300 Subject: [PATCH 3/6] flake8 --- pycallgraph/tracer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pycallgraph/tracer.py b/pycallgraph/tracer.py index e09e0c9e..b73cd0ff 100644 --- a/pycallgraph/tracer.py +++ b/pycallgraph/tracer.py @@ -375,4 +375,5 @@ def wrapper(*rest): return wrapper + inspect.getmodule = simple_memoize(inspect.getmodule) From 7c51eb1639db1a754a850ad146efbaba40069c85 Mon Sep 17 00:00:00 2001 From: Samedhan Karamese Date: Thu, 20 Sep 2018 23:46:05 +0300 Subject: [PATCH 4/6] changed htmllib to glob --- examples/graphviz/import.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/graphviz/import.py b/examples/graphviz/import.py index 78fda7c6..1e375959 100755 --- a/examples/graphviz/import.py +++ b/examples/graphviz/import.py @@ -11,7 +11,7 @@ def main(): import_list = ( 'pickle', - 'htmllib', + 'glob', 'urllib2', ) graphviz = GraphvizOutput() @@ -23,5 +23,6 @@ def main(): __import__(module) + if __name__ == '__main__': main() From 878a49cc59bbca1101e3c450ec3e2ca1a5861b45 Mon Sep 17 00:00:00 2001 From: Samedhan Karamese Date: Thu, 20 Sep 2018 23:51:48 +0300 Subject: [PATCH 5/6] changed urllib to datetime --- examples/graphviz/import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/graphviz/import.py b/examples/graphviz/import.py index 1e375959..9f040144 100755 --- a/examples/graphviz/import.py +++ b/examples/graphviz/import.py @@ -12,7 +12,7 @@ def main(): import_list = ( 'pickle', 'glob', - 'urllib2', + 'datetime', ) graphviz = GraphvizOutput() config = Config(include_stdlib=True) From f9b972523a0742504e9ac14e4d226406debe87d6 Mon Sep 17 00:00:00 2001 From: Samedhan Karamese Date: Fri, 21 Sep 2018 00:05:18 +0300 Subject: [PATCH 6/6] added six lib for using iteritems --- requirements/development.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/development.txt b/requirements/development.txt index 5e9f2b92..a6b019e9 100644 --- a/requirements/development.txt +++ b/requirements/development.txt @@ -3,3 +3,4 @@ pytest-pep8 pytest-cov python-coveralls flake8 +six