Commit cf24a7aa authored by Samuel Huang's avatar Samuel Huang Committed by Commit Bot

[SuperSize] Modernize syntax for int divisions and exception variable assignments.

To aid in porting SuperSize to Python 3, this CL does the following:
* Sort out int vs. float divisions.
  * Add "'from __future__ import division" to each python file
    (except mock files for testing).
  * For int division: Use "//" (was "/")
  * For float division: Ensure this can be inferred, by using
    float(), or float constants (e.g., 1024.0).
* Change "except" variable assignment style.
  * Use "except Exception as e" (was "except Exception, e").

Bug: 1054018
Change-Id: I25925196fe33fc7f4ca28f126f180eb4060fa2b2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2094544
Commit-Queue: Samuel Huang <huangs@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#748375}
parent 064eb2e8
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
Assumes that apk_path.mapping and apk_path.jar.info is available. Assumes that apk_path.mapping and apk_path.jar.info is available.
""" """
from __future__ import division
import logging import logging
import os import os
import subprocess import subprocess
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import unittest import unittest
import apkanalyzer import apkanalyzer
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
"""Logic for reading .a files.""" """Logic for reading .a files."""
from __future__ import division
from __future__ import print_function from __future__ import print_function
import argparse import argparse
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"""Main Python API for analyzing binary size.""" """Main Python API for analyzing binary size."""
from __future__ import division
import argparse import argparse
import bisect import bisect
import calendar import calendar
......
...@@ -20,6 +20,7 @@ This file can also be run stand-alone in order to test out the logic on smaller ...@@ -20,6 +20,7 @@ This file can also be run stand-alone in order to test out the logic on smaller
sample sizes. sample sizes.
""" """
from __future__ import division
from __future__ import print_function from __future__ import print_function
import argparse import argparse
...@@ -225,7 +226,8 @@ class _BcTypeInfo: ...@@ -225,7 +226,8 @@ class _BcTypeInfo:
[size, item_type_id] = list(_ParseOpItems(line, attrib_pos)) # op0, op1. [size, item_type_id] = list(_ParseOpItems(line, attrib_pos)) # op0, op1.
bits = self.int_types.get(item_type_id) bits = self.int_types.get(item_type_id)
if bits is not None: # |bits| can be None for non-int arrays. if bits is not None: # |bits| can be None for non-int arrays.
self.int_array_types[self.cur_type_id] = _BcIntArrayType(size, bits / 8) self.int_array_types[self.cur_type_id] = _BcIntArrayType(
size, bits // 8)
self.cur_type_id += 1 self.cur_type_id += 1
def GetArrayType(self, idx): def GetArrayType(self, idx):
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import ast import ast
import os import os
import unittest import unittest
...@@ -31,7 +33,7 @@ def _MakeString(bits, toks): ...@@ -31,7 +33,7 @@ def _MakeString(bits, toks):
the result of concatanating tokens. the result of concatanating tokens.
""" """
s = ''.join(tok if isinstance(tok, basestring) else chr(tok) for tok in toks) s = ''.join(tok if isinstance(tok, basestring) else chr(tok) for tok in toks)
padding = '\x00' * ((bits - 8) / 8) padding = '\x00' * ((bits - 8) // 8)
return ''.join(ch + padding for ch in s) return ''.join(ch + padding for ch in s)
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"""Contains a set of Chrome-specific size queries.""" """Contains a set of Chrome-specific size queries."""
from __future__ import division
import logging import logging
import models import models
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"""Helpers related to multiprocessing.""" """Helpers related to multiprocessing."""
from __future__ import division
import __builtin__ # __builtins__ does not have exception types. import __builtin__ # __builtins__ does not have exception types.
import atexit import atexit
import itertools import itertools
...@@ -68,7 +70,7 @@ class _FuncWrapper(object): ...@@ -68,7 +70,7 @@ class _FuncWrapper(object):
def __call__(self, index, _=None): def __call__(self, index, _=None):
try: try:
return self._func(*_fork_params[index], **_fork_kwargs) return self._func(*_fork_params[index], **_fork_kwargs)
except Exception, e: except Exception as e:
# Only keep the exception type for builtin exception types or else risk # Only keep the exception type for builtin exception types or else risk
# further marshalling exceptions. # further marshalling exceptions.
exception_type = None exception_type = None
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import os import os
import threading import threading
import unittest import unittest
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"""An interactive console for looking analyzing .size files.""" """An interactive console for looking analyzing .size files."""
from __future__ import division
from __future__ import print_function from __future__ import print_function
import argparse import argparse
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"""Utilities for demangling C++ symbols.""" """Utilities for demangling C++ symbols."""
from __future__ import division
import collections import collections
import itertools import itertools
import logging import logging
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# found in the LICENSE file. # found in the LICENSE file.
"""Methods for converting model objects to human-readable formats.""" """Methods for converting model objects to human-readable formats."""
from __future__ import division
import abc import abc
import cStringIO import cStringIO
import collections import collections
...@@ -109,7 +111,7 @@ class Histogram(object): ...@@ -109,7 +111,7 @@ class Histogram(object):
bucket_values = [str(self.data[k]) for k in keys] bucket_values = [str(self.data[k]) for k in keys]
num_items = len(keys) num_items = len(keys)
num_cols = 6 num_cols = 6
num_rows = (num_items + num_cols - 1) / num_cols # Divide and round up. num_rows = (num_items + num_cols - 1) // num_cols # Divide and round up.
# Needed for xrange to not throw due to step by 0. # Needed for xrange to not throw due to step by 0.
if num_rows == 0: if num_rows == 0:
return return
...@@ -287,7 +289,7 @@ class DescriberText(Describer): ...@@ -287,7 +289,7 @@ class DescriberText(Describer):
# * Accounts for < .5% of PSS # * Accounts for < .5% of PSS
# * Symbols are smaller than 1.0 byte (by PSS) # * Symbols are smaller than 1.0 byte (by PSS)
# * Always show at least 50 symbols. # * Always show at least 50 symbols.
min_remaining_pss_to_show = max(1024, total / 1000 * 5) min_remaining_pss_to_show = max(1024.0, total / 1000.0 * 5)
min_symbol_pss_to_show = 1.0 min_symbol_pss_to_show = 1.0
min_symbols_to_show = 50 min_symbols_to_show = 50
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# found in the LICENSE file. # found in the LICENSE file.
"""Logic for diffing two SizeInfo objects.""" """Logic for diffing two SizeInfo objects."""
from __future__ import division
import collections import collections
import logging import logging
import re import re
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import unittest import unittest
import diff import diff
......
...@@ -101,6 +101,8 @@ All remaining bytes are a valid gzipped sparse .size file containing the ...@@ -101,6 +101,8 @@ All remaining bytes are a valid gzipped sparse .size file containing the
"after" snapshot. "after" snapshot.
""" """
from __future__ import division
import cStringIO import cStringIO
import contextlib import contextlib
import gzip import gzip
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
Much of this logic is duplicated at Much of this logic is duplicated at
tools/binary_size/libsupersize/caspian/function_signature.cc.""" tools/binary_size/libsupersize/caspian/function_signature.cc."""
from __future__ import division
def _FindParameterListParen(name): def _FindParameterListParen(name):
"""Finds index of the "(" that denotes the start of a parameter list.""" """Finds index of the "(" that denotes the start of a parameter list."""
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import logging import logging
import re import re
import unittest import unittest
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"""Creates an html report that allows you to view binary size by component.""" """Creates an html report that allows you to view binary size by component."""
from __future__ import division
from __future__ import print_function from __future__ import print_function
import codecs import codecs
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import cStringIO import cStringIO
import contextlib import contextlib
import copy import copy
......
...@@ -18,6 +18,7 @@ The |linker_name| parameter in various functions must take one of the above ...@@ -18,6 +18,7 @@ The |linker_name| parameter in various functions must take one of the above
coded linker name values. coded linker name values.
""" """
from __future__ import division
from __future__ import print_function from __future__ import print_function
import argparse import argparse
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import glob import glob
import os import os
import sys import sys
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
"""Collect, archive, and analyze Chrome's binary size.""" """Collect, archive, and analyze Chrome's binary size."""
from __future__ import division
import argparse import argparse
import atexit import atexit
import collections import collections
...@@ -26,7 +28,7 @@ import start_server ...@@ -26,7 +28,7 @@ import start_server
def _LogPeakRamUsage(): def _LogPeakRamUsage():
peak_ram_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss peak_ram_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
peak_ram_usage += resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss peak_ram_usage += resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss
logging.info('Peak RAM usage was %d MB.', peak_ram_usage / 1024) logging.info('Peak RAM usage was %d MB.', peak_ram_usage // 1024)
def _AddCommonArguments(parser): def _AddCommonArguments(parser):
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"""Regular expression helpers.""" """Regular expression helpers."""
from __future__ import division
import re import re
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import re import re
import unittest import unittest
......
...@@ -28,6 +28,8 @@ Description of common properties: ...@@ -28,6 +28,8 @@ Description of common properties:
Never None, but will be '' when no component exists. Never None, but will be '' when no component exists.
""" """
from __future__ import division
import collections import collections
import logging import logging
import os import os
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
# found in the LICENSE file. # found in the LICENSE file.
"""Extract source file information from .ninja files.""" """Extract source file information from .ninja files."""
from __future__ import division
from __future__ import print_function from __future__ import print_function
import argparse import argparse
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import unittest import unittest
import ninja_parser import ninja_parser
......
...@@ -17,6 +17,8 @@ RunNmOnIntermediates(): ...@@ -17,6 +17,8 @@ RunNmOnIntermediates():
offset information. offset information.
""" """
from __future__ import division
import collections import collections
import subprocess import subprocess
......
...@@ -35,6 +35,7 @@ This file can also be run stand-alone in order to test out the logic on smaller ...@@ -35,6 +35,7 @@ This file can also be run stand-alone in order to test out the logic on smaller
sample sizes. sample sizes.
""" """
from __future__ import division
from __future__ import print_function from __future__ import print_function
import argparse import argparse
...@@ -389,7 +390,7 @@ class _BulkObjectFileAnalyzerSlave(object): ...@@ -389,7 +390,7 @@ class _BulkObjectFileAnalyzerSlave(object):
self._HandleMessage(self._pipe.recv()) self._HandleMessage(self._pipe.recv())
except EOFError: except EOFError:
pass pass
except EnvironmentError, e: except EnvironmentError as e:
# Parent process exited so don't log. # Parent process exited so don't log.
if e.errno in (errno.EPIPE, errno.ECONNRESET): if e.errno in (errno.EPIPE, errno.ECONNRESET):
sys.exit(1) sys.exit(1)
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"""Functions for dealing with determining --tool-prefix.""" """Functions for dealing with determining --tool-prefix."""
from __future__ import division
import abc import abc
import distutils.spawn import distutils.spawn
import logging import logging
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import logging import logging
import os import os
import sys import sys
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
"""Runs a server to let the user interact with supersize using a web UI.""" """Runs a server to let the user interact with supersize using a web UI."""
from __future__ import division
import BaseHTTPServer import BaseHTTPServer
import logging import logging
import os import os
......
...@@ -31,6 +31,8 @@ ResolveStringPieces(): ...@@ -31,6 +31,8 @@ ResolveStringPieces():
- Returns [{path: [string_ranges]} for each string_section]. - Returns [{path: [string_ranges]} for each string_section].
""" """
from __future__ import division
import ast import ast
import collections import collections
import itertools import itertools
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
from __future__ import division
import difflib import difflib
import logging import logging
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
"""Update the Google Cloud Storage bucket hosting the Super Size UI.""" """Update the Google Cloud Storage bucket hosting the Super Size UI."""
from __future__ import division
import argparse import argparse
import os import os
import subprocess import subprocess
......
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