Commit 4be85061 authored by Ng Zhi An's avatar Ng Zhi An Committed by Commit Bot

[Py3] Get blink_gc_plugin Py3 ready

Tested with:

python build.py --without-android --without-fuchsia \
  --run-tests --skip-checkout

With python == python3.7 (inside a pipenv).

- get maxint from sys.maxsize from Py3 (sys.maxint is removed)
- use StringIO from io
- use .values() instead of .itervalues()
- tweak lambda used in sort (became syntax error in Py3)
- check_output with universal newlines, will open stream as text

Bug: chromium:941669
Change-Id: I80ca9ae6d2f6d712c6e3cac5343627512ce84b86
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2261715Reviewed-by: default avatarAnton Bikineev <bikineev@chromium.org>
Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Commit-Queue: Zhi An Ng <zhin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#782053}
parent a2585c74
......@@ -4,7 +4,7 @@
# found in the LICENSE file.
from __future__ import print_function
import argparse, os, sys, json, subprocess, pickle, StringIO
import argparse, io, os, sys, json, subprocess, pickle
parser = argparse.ArgumentParser(
description =
......@@ -57,6 +57,14 @@ ignored_cycles = []
# Global flag to determine exit code.
global_reported_error = False
try:
# Python3 remove sys.maxint.
maxint = sys.maxint
except AttributeError:
# Also see https://stackoverflow.com/a/13795777/4052492.
maxint = sys.maxsize
def set_reported_error(value):
global global_reported_error
global_reported_error = value
......@@ -109,18 +117,20 @@ class Node:
else:
self.edges[new_edge.key] = new_edge
def super_edges(self):
return [ e for e in self.edges.itervalues() if e.is_super() ]
return [e for e in self.edges.values() if e.is_super()]
def subclass_edges(self):
return [ e for e in self.edges.itervalues() if e.is_subclass() ]
return [e for e in self.edges.values() if e.is_subclass()]
def reset(self):
self.cost = sys.maxint
self.cost = maxint
self.visited = False
self.path = None
self.counts = {}
for ptr in ptr_types:
self.counts[ptr] = 0
def update_counts(self):
for e in self.edges.itervalues():
for e in self.edges.values():
inc_ptr(e.dst, e.ptr)
# Representation of directed graph edges.
......@@ -199,7 +209,7 @@ def copy_super_edges(edge):
copy_super_edges(e)
# Copy strong super-class edges (ignoring sub-class edges) to the sub class.
sub_node = graph[edge.src]
for e in super_node.edges.itervalues():
for e in super_node.edges.values():
if e.keeps_alive() and not e.is_subclass():
new_edge = Edge(
src = sub_node.name,
......@@ -222,16 +232,16 @@ def copy_super_edges(edge):
super_node.edges[sub_edge.key] = sub_edge
def complete_graph():
for node in graph.itervalues():
for node in graph.values():
for edge in node.super_edges():
copy_super_edges(edge)
for edge in node.edges.itervalues():
for edge in node.edges.values():
if edge.is_root():
roots.append(edge)
log("Copied edges down <super> edges for %d graph nodes" % global_inc_copy)
def reset_graph():
for n in graph.itervalues():
for n in graph.values():
n.reset()
def shortest_path(start, end):
......@@ -243,7 +253,7 @@ def shortest_path(start, end):
current.visited = True
if current == end or current.cost >= end.cost + 1:
return
for e in current.edges.itervalues():
for e in current.edges.values():
if not e.keeps_alive():
continue
dst = graph.get(e.dst)
......@@ -276,7 +286,7 @@ def detect_cycles():
continue
# Find the shortest path from the root target (dst) to its host (src)
shortest_path(dst, src)
if src.cost < sys.maxint:
if src.cost < maxint:
report_cycle(root_edge)
def is_ignored_cycle(cycle):
......@@ -307,7 +317,7 @@ def report_cycle(root_edge):
for p in path:
if len(p.loc) > max_loc:
max_loc = len(p.loc)
out = StringIO.StringIO()
out = io.StringIO()
for p in path[:-1]:
print((p.loc + ':').ljust(max_loc + 1), p, file=out)
sout = out.getvalue()
......@@ -370,7 +380,7 @@ def print_stats():
gc_managed = []
hierarchies = []
for node in graph.itervalues():
for node in graph.values():
node.update_counts()
for sup in node.super_edges():
if sup.dst in gcref_bases:
......@@ -395,7 +405,7 @@ def print_stats():
hierarchies.append((base, stats))
print("\nHierarchies in transition (RefCountedGarbageCollected):")
hierarchies.sort(key=lambda (n,s): -s['classes'])
hierarchies.sort(key=lambda n: -n[1]['classes'])
for (node, stats) in hierarchies:
total = stats['mem'] + stats['ref'] + stats['raw']
print(
......
......@@ -26,11 +26,13 @@ class BlinkGcPluginTest(plugin_testing.ClangPluginTest):
# use the processed results as the actual results of the test.
if os.path.exists('%s.graph.json' % test_name):
try:
actual = subprocess.check_output(
['python', '../process-graph.py', '-c',
'%s.graph.json' % test_name],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError, e:
actual = subprocess.check_output([
'python', '../process-graph.py', '-c',
'%s.graph.json' % test_name
],
stderr=subprocess.STDOUT,
universal_newlines=True)
except subprocess.CalledProcessError as e:
# The graph processing script returns a failure exit code if the graph
# is bad (e.g. it has a cycle). The output still needs to be captured in
# that case, since the expected results capture the errors.
......
......@@ -63,7 +63,7 @@ class ClangPluginTest(object):
cmd = clang_cmd[:]
try:
# Some tests need to run with extra flags.
cmd.extend(file('%s.flags' % test_name).read().split())
cmd.extend(open('%s.flags' % test_name).read().split())
except IOError:
pass
cmd.append(test)
......@@ -85,7 +85,9 @@ class ClangPluginTest(object):
def RunOneTest(self, test_name, cmd):
try:
actual = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
actual = subprocess.check_output(cmd,
stderr=subprocess.STDOUT,
universal_newlines=True)
except subprocess.CalledProcessError as e:
# Some plugin tests intentionally trigger compile errors, so just ignore
# an exit code that indicates failure.
......
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