summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-05-01 14:04:02 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-05-01 16:24:37 -0400
commit775c14a764ff3fd32bcd25d91f4c0f635722ed50 (patch)
tree2f73d38468b11c4b5f9723265bb121352a13271f
parente96ef93d18831630687b6c026bed89a1f9149c90 (diff)
downloadpython-coveragepy-git-775c14a764ff3fd32bcd25d91f4c0f635722ed50.tar.gz
refactor: remove more unneeded backward.py shims
Gone are: - iitems - litems - iternext - to_bytes - to_string - binary_bytes - byte_to_int - bytes_to_ints - BUILTINS
-rw-r--r--coverage/backward.py66
-rw-r--r--coverage/collector.py5
-rw-r--r--coverage/config.py5
-rw-r--r--coverage/control.py3
-rw-r--r--coverage/execfile.py3
-rw-r--r--coverage/html.py6
-rw-r--r--coverage/misc.py5
-rw-r--r--coverage/numbits.py15
-rw-r--r--coverage/parser.py5
-rw-r--r--coverage/phystokens.py5
-rw-r--r--coverage/results.py9
-rw-r--r--coverage/sqldata.py11
-rw-r--r--coverage/xmlreport.py5
-rw-r--r--tests/test_backward.py24
-rw-r--r--tests/test_execfile.py3
-rw-r--r--tests/test_numbits.py3
16 files changed, 35 insertions, 138 deletions
diff --git a/coverage/backward.py b/coverage/backward.py
index 15f4e88a..26d8d0e5 100644
--- a/coverage/backward.py
+++ b/coverage/backward.py
@@ -5,72 +5,6 @@
import sys
-# A function to iterate listlessly over a dict's items, and one to get the
-# items as a list.
-try:
- {}.iteritems
-except AttributeError:
- # Python 3
- def iitems(d):
- """Produce the items from dict `d`."""
- return d.items()
-
- def litems(d):
- """Return a list of items from dict `d`."""
- return list(d.items())
-else:
- # Python 2
- def iitems(d):
- """Produce the items from dict `d`."""
- return d.iteritems()
-
- def litems(d):
- """Return a list of items from dict `d`."""
- return d.items()
-
-# Getting the `next` function from an iterator is different in 2 and 3.
-try:
- iter([]).next
-except AttributeError:
- def iternext(seq):
- """Get the `next` function for iterating over `seq`."""
- return iter(seq).__next__
-else:
- def iternext(seq):
- """Get the `next` function for iterating over `seq`."""
- return iter(seq).next
-
-# Python 3.x is picky about bytes and strings, so provide methods to
-# get them right.
-def to_bytes(s):
- """Convert string `s` to bytes."""
- return s.encode('utf8')
-
-def to_string(b):
- """Convert bytes `b` to string."""
- return b.decode('utf8')
-
-def binary_bytes(byte_values):
- """Produce a byte string with the ints from `byte_values`."""
- return bytes(byte_values)
-
-def byte_to_int(byte):
- """Turn a byte indexed from a bytes object into an int."""
- return byte
-
-def bytes_to_ints(bytes_value):
- """Turn a bytes object into a sequence of ints."""
- # In Python 3, iterating bytes gives ints.
- return bytes_value
-
-try:
- # In Python 2.x, the builtins were in __builtin__
- BUILTINS = sys.modules['__builtin__']
-except KeyError:
- # In Python 3.x, they're in builtins
- BUILTINS = sys.modules['builtins']
-
-
# imp was deprecated in Python 3.3
try:
import importlib
diff --git a/coverage/collector.py b/coverage/collector.py
index 17dcac1c..e6bb9829 100644
--- a/coverage/collector.py
+++ b/coverage/collector.py
@@ -7,7 +7,6 @@ import os
import sys
from coverage import env
-from coverage.backward import litems
from coverage.debug import short_stack
from coverage.disposition import FileDisposition
from coverage.misc import CoverageException, isolate_module
@@ -404,14 +403,14 @@ class Collector(object):
def mapped_file_dict(self, d):
"""Return a dict like d, but with keys modified by file_mapper."""
- # The call to litems() ensures that the GIL protects the dictionary
+ # The call to list(items()) ensures that the GIL protects the dictionary
# iterator against concurrent modifications by tracers running
# in other threads. We try three times in case of concurrent
# access, hoping to get a clean copy.
runtime_err = None
for _ in range(3):
try:
- items = litems(d)
+ items = list(d.items())
except RuntimeError as ex:
runtime_err = ex
else:
diff --git a/coverage/config.py b/coverage/config.py
index 7bfc74db..608c027a 100644
--- a/coverage/config.py
+++ b/coverage/config.py
@@ -11,7 +11,6 @@ import os.path
import re
from coverage import env
-from coverage.backward import iitems
from coverage.misc import contract, CoverageException, isolate_module
from coverage.misc import substitute_variables
@@ -246,7 +245,7 @@ class CoverageConfig(object):
def from_args(self, **kwargs):
"""Read config values from `kwargs`."""
- for k, v in iitems(kwargs):
+ for k, v in kwargs.items():
if v is not None:
if k in self.MUST_BE_LIST and isinstance(v, str):
v = [v]
@@ -298,7 +297,7 @@ class CoverageConfig(object):
section, option = option_spec[1].split(":")
all_options[section].add(option)
- for section, options in iitems(all_options):
+ for section, options in all_options.items():
real_section = cp.has_section(section)
if real_section:
for unknown in set(cp.options(section)) - options:
diff --git a/coverage/control.py b/coverage/control.py
index 5c5d13aa..3ccf313e 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -14,7 +14,6 @@ import time
from coverage import env
from coverage.annotate import AnnotateReporter
-from coverage.backward import iitems
from coverage.collector import Collector, CTracer
from coverage.config import read_coverage_config
from coverage.context import should_start_context_test_function, combine_context_switchers
@@ -1063,7 +1062,7 @@ class Coverage(object):
('path', sys.path),
('environment', sorted(
("%s = %s" % (k, v))
- for k, v in iitems(os.environ)
+ for k, v in os.environ.items()
if any(slug in k for slug in ("COV", "PY"))
)),
('command_line', " ".join(getattr(sys, 'argv', ['-none-']))),
diff --git a/coverage/execfile.py b/coverage/execfile.py
index fd6846e0..32bb8223 100644
--- a/coverage/execfile.py
+++ b/coverage/execfile.py
@@ -11,7 +11,6 @@ import sys
import types
from coverage import env
-from coverage.backward import BUILTINS
from coverage.backward import PYC_MAGIC_NUMBER, imp, importlib_util_find_spec
from coverage.files import canonical_filename, python_reported_file
from coverage.misc import CoverageException, ExceptionDuringRun, NoCode, NoSource, isolate_module
@@ -216,7 +215,7 @@ class PyRunner(object):
if self.spec is not None:
main_mod.__spec__ = self.spec
- main_mod.__builtins__ = BUILTINS
+ main_mod.__builtins__ = sys.modules['builtins']
sys.modules['__main__'] = main_mod
diff --git a/coverage/html.py b/coverage/html.py
index b48bb80b..0093342a 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -10,7 +10,7 @@ import re
import shutil
import coverage
-from coverage.backward import iitems, SimpleNamespace, format_local_datetime
+from coverage.backward import SimpleNamespace, format_local_datetime
from coverage.data import add_data_to_hash
from coverage.files import flat_rootname
from coverage.misc import CoverageException, ensure_dir, file_be_gone, Hasher, isolate_module
@@ -429,7 +429,7 @@ class IncrementalChecker(object):
if usable:
self.files = {}
- for filename, fileinfo in iitems(status['files']):
+ for filename, fileinfo in status['files'].items():
fileinfo['index']['nums'] = Numbers(*fileinfo['index']['nums'])
self.files[filename] = fileinfo
self.globals = status['globals']
@@ -440,7 +440,7 @@ class IncrementalChecker(object):
"""Write the current status."""
status_file = os.path.join(self.directory, self.STATUS_FILE)
files = {}
- for filename, fileinfo in iitems(self.files):
+ for filename, fileinfo in self.files.items():
fileinfo['index']['nums'] = fileinfo['index']['nums'].init_args()
files[filename] = fileinfo
diff --git a/coverage/misc.py b/coverage/misc.py
index 148f42e1..7182d385 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -16,7 +16,6 @@ import sys
import types
from coverage import env
-from coverage.backward import to_bytes
ISOLATED_MODULES = {}
@@ -203,7 +202,7 @@ class Hasher(object):
def update(self, v):
"""Add `v` to the hash, recursively if needed."""
- self.md5.update(to_bytes(str(type(v))))
+ self.md5.update(str(type(v)).encode("utf8"))
if isinstance(v, str):
self.md5.update(v.encode('utf8'))
elif isinstance(v, bytes):
@@ -211,7 +210,7 @@ class Hasher(object):
elif v is None:
pass
elif isinstance(v, (int, float)):
- self.md5.update(to_bytes(str(v)))
+ self.md5.update(str(v).encode("utf8"))
elif isinstance(v, (tuple, list)):
for e in v:
self.update(e)
diff --git a/coverage/numbits.py b/coverage/numbits.py
index 7a17fc56..9c49d55d 100644
--- a/coverage/numbits.py
+++ b/coverage/numbits.py
@@ -17,7 +17,6 @@ import json
from itertools import zip_longest
-from coverage.backward import byte_to_int, bytes_to_ints, binary_bytes
from coverage.misc import contract, new_contract
def _to_blob(b):
@@ -63,7 +62,7 @@ def numbits_to_nums(numbits):
"""
nums = []
- for byte_i, byte in enumerate(bytes_to_ints(numbits)):
+ for byte_i, byte in enumerate(numbits):
for bit_i in range(8):
if (byte & (1 << bit_i)):
nums.append(byte_i * 8 + bit_i)
@@ -77,8 +76,8 @@ def numbits_union(numbits1, numbits2):
Returns:
A new numbits, the union of `numbits1` and `numbits2`.
"""
- byte_pairs = zip_longest(bytes_to_ints(numbits1), bytes_to_ints(numbits2), fillvalue=0)
- return _to_blob(binary_bytes(b1 | b2 for b1, b2 in byte_pairs))
+ byte_pairs = zip_longest(numbits1, numbits2, fillvalue=0)
+ return _to_blob(bytes(b1 | b2 for b1, b2 in byte_pairs))
@contract(numbits1='blob', numbits2='blob', returns='blob')
@@ -88,8 +87,8 @@ def numbits_intersection(numbits1, numbits2):
Returns:
A new numbits, the intersection `numbits1` and `numbits2`.
"""
- byte_pairs = zip_longest(bytes_to_ints(numbits1), bytes_to_ints(numbits2), fillvalue=0)
- intersection_bytes = binary_bytes(b1 & b2 for b1, b2 in byte_pairs)
+ byte_pairs = zip_longest(numbits1, numbits2, fillvalue=0)
+ intersection_bytes = bytes(b1 & b2 for b1, b2 in byte_pairs)
return _to_blob(intersection_bytes.rstrip(b'\0'))
@@ -103,7 +102,7 @@ def numbits_any_intersection(numbits1, numbits2):
Returns:
A bool, True if there is any number in both `numbits1` and `numbits2`.
"""
- byte_pairs = zip_longest(bytes_to_ints(numbits1), bytes_to_ints(numbits2), fillvalue=0)
+ byte_pairs = zip_longest(numbits1, numbits2, fillvalue=0)
return any(b1 & b2 for b1, b2 in byte_pairs)
@@ -117,7 +116,7 @@ def num_in_numbits(num, numbits):
nbyte, nbit = divmod(num, 8)
if nbyte >= len(numbits):
return False
- return bool(byte_to_int(numbits[nbyte]) & (1 << nbit))
+ return bool(numbits[nbyte] & (1 << nbit))
def register_sqlite_functions(connection):
diff --git a/coverage/parser.py b/coverage/parser.py
index abcda5fb..61ef7539 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -11,7 +11,6 @@ import token
import tokenize
from coverage import env
-from coverage.backward import bytes_to_ints
from coverage.bytecode import code_objects
from coverage.debug import short_stack
from coverage.misc import contract, join_regex, new_contract, nice_pair, one_of
@@ -402,8 +401,8 @@ class ByteParser(object):
yield line
else:
# Adapted from dis.py in the standard library.
- byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
- line_increments = bytes_to_ints(self.code.co_lnotab[1::2])
+ byte_increments = self.code.co_lnotab[0::2]
+ line_increments = self.code.co_lnotab[1::2]
last_line_num = None
line_num = self.code.co_firstlineno
diff --git a/coverage/phystokens.py b/coverage/phystokens.py
index 7556d310..4b69c476 100644
--- a/coverage/phystokens.py
+++ b/coverage/phystokens.py
@@ -8,7 +8,6 @@ import re
import token
import tokenize
-from coverage.backward import iternext
from coverage.misc import contract
@@ -140,7 +139,7 @@ class CachedTokenizer(object):
"""A stand-in for `tokenize.generate_tokens`."""
if text != self.last_text:
self.last_text = text
- readline = iternext(text.splitlines(True))
+ readline = iter(text.splitlines(True)).__next__
self.last_tokens = list(tokenize.generate_tokens(readline))
return self.last_tokens
@@ -159,7 +158,7 @@ def source_encoding(source):
Returns a string, the name of the encoding.
"""
- readline = iternext(source.splitlines(True))
+ readline = iter(source.splitlines(True)).__next__
return tokenize.detect_encoding(readline)[0]
diff --git a/coverage/results.py b/coverage/results.py
index 4916864d..35f79ded 100644
--- a/coverage/results.py
+++ b/coverage/results.py
@@ -5,7 +5,6 @@
import collections
-from coverage.backward import iitems
from coverage.debug import SimpleReprMixin
from coverage.misc import contract, CoverageException, nice_pair
@@ -32,8 +31,8 @@ class Analysis(object):
self.no_branch = self.file_reporter.no_branch_lines()
n_branches = self._total_branches()
mba = self.missing_branch_arcs()
- n_partial_branches = sum(len(v) for k,v in iitems(mba) if k not in self.missing)
- n_missing_branches = sum(len(v) for k,v in iitems(mba))
+ n_partial_branches = sum(len(v) for k,v in mba.items() if k not in self.missing)
+ n_missing_branches = sum(len(v) for k,v in mba.items())
else:
self._arc_possibilities = []
self.exit_counts = {}
@@ -59,7 +58,7 @@ class Analysis(object):
"""
if branches and self.has_arcs():
- arcs = iitems(self.missing_branch_arcs())
+ arcs = self.missing_branch_arcs().items()
else:
arcs = None
@@ -113,7 +112,7 @@ class Analysis(object):
def _branch_lines(self):
"""Returns a list of line numbers that have more than one exit."""
- return [l1 for l1,count in iitems(self.exit_counts) if count > 1]
+ return [l1 for l1,count in self.exit_counts.items() if count > 1]
def _total_branches(self):
"""How many total branches are there?"""
diff --git a/coverage/sqldata.py b/coverage/sqldata.py
index 9af08030..b85da057 100644
--- a/coverage/sqldata.py
+++ b/coverage/sqldata.py
@@ -17,7 +17,6 @@ import sys
import threading
import zlib
-from coverage.backward import iitems, to_bytes, to_string
from coverage.debug import NoDebugging, SimpleReprMixin, clipped_repr
from coverage.files import PathAliases
from coverage.misc import CoverageException, contract, file_be_gone, filename_suffix, isolate_module
@@ -333,7 +332,7 @@ class CoverageData(SimpleReprMixin):
if self._debug.should('dataio'):
self._debug.write("Dumping data from data file {!r}".format(self._filename))
with self._connect() as con:
- return b'z' + zlib.compress(to_bytes(con.dump()))
+ return b'z' + zlib.compress(con.dump().encode("utf8"))
@contract(data='bytes')
def loads(self, data):
@@ -357,7 +356,7 @@ class CoverageData(SimpleReprMixin):
raise CoverageException(
"Unrecognized serialization: {!r} (head of {} bytes)".format(data[:40], len(data))
)
- script = to_string(zlib.decompress(data[1:]))
+ script = zlib.decompress(data[1:]).decode("utf8")
self._dbs[threading.get_ident()] = db = SqliteDb(self._filename, self._debug)
with db:
db.executescript(script)
@@ -447,7 +446,7 @@ class CoverageData(SimpleReprMixin):
return
with self._connect() as con:
self._set_context_id()
- for filename, linenos in iitems(line_data):
+ for filename, linenos in line_data.items():
linemap = nums_to_numbits(linenos)
file_id = self._file_id(filename, add=True)
query = "select numbits from line_bits where file_id = ? and context_id = ?"
@@ -479,7 +478,7 @@ class CoverageData(SimpleReprMixin):
return
with self._connect() as con:
self._set_context_id()
- for filename, arcs in iitems(arc_data):
+ for filename, arcs in arc_data.items():
file_id = self._file_id(filename, add=True)
data = [(file_id, self._current_context_id, fromno, tono) for fromno, tono in arcs]
con.executemany(
@@ -517,7 +516,7 @@ class CoverageData(SimpleReprMixin):
return
self._start_using()
with self._connect() as con:
- for filename, plugin_name in iitems(file_tracers):
+ for filename, plugin_name in file_tracers.items():
file_id = self._file_id(filename)
if file_id is None:
raise CoverageException(
diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py
index 470e991c..db1d0116 100644
--- a/coverage/xmlreport.py
+++ b/coverage/xmlreport.py
@@ -11,7 +11,6 @@ import time
import xml.dom.minidom
from coverage import __url__, __version__, files
-from coverage.backward import iitems
from coverage.misc import isolate_module
from coverage.report import get_analysis_to_report
@@ -92,13 +91,13 @@ class XmlReporter(object):
xcoverage.appendChild(xpackages)
# Populate the XML DOM with the package info.
- for pkg_name, pkg_data in sorted(iitems(self.packages)):
+ for pkg_name, pkg_data in sorted(self.packages.items()):
class_elts, lhits, lnum, bhits, bnum = pkg_data
xpackage = self.xml_out.createElement("package")
xpackages.appendChild(xpackage)
xclasses = self.xml_out.createElement("classes")
xpackage.appendChild(xclasses)
- for _, class_elt in sorted(iitems(class_elts)):
+ for _, class_elt in sorted(class_elts.items()):
xclasses.appendChild(class_elt)
xpackage.setAttribute("name", pkg_name.replace(os.sep, '.'))
xpackage.setAttribute("line-rate", rate(lhits, lnum))
diff --git a/tests/test_backward.py b/tests/test_backward.py
deleted file mode 100644
index b40a174b..00000000
--- a/tests/test_backward.py
+++ /dev/null
@@ -1,24 +0,0 @@
-# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
-# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt
-
-"""Tests that our version shims in backward.py are working."""
-
-from coverage.backward import iitems, binary_bytes, bytes_to_ints
-
-from tests.coveragetest import CoverageTest
-from tests.helpers import assert_count_equal
-
-
-class BackwardTest(CoverageTest):
- """Tests of things from backward.py."""
-
- def test_iitems(self):
- d = {'a': 1, 'b': 2, 'c': 3}
- items = [('a', 1), ('b', 2), ('c', 3)]
- assert_count_equal(list(iitems(d)), items)
-
- def test_binary_bytes(self):
- byte_values = [0, 255, 17, 23, 42, 57]
- bb = binary_bytes(byte_values)
- assert len(bb) == len(byte_values)
- assert byte_values == list(bytes_to_ints(bb))
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index 3cdd1ed9..ec8cd180 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -14,7 +14,6 @@ import sys
import pytest
from coverage import env
-from coverage.backward import binary_bytes
from coverage.execfile import run_python_file, run_python_module
from coverage.files import python_reported_file
from coverage.misc import NoCode, NoSource
@@ -151,7 +150,7 @@ class RunPycFileTest(CoverageTest):
# Jam Python 2.1 magic number into the .pyc file.
with open(pycfile, "r+b") as fpyc:
fpyc.seek(0)
- fpyc.write(binary_bytes([0x2a, 0xeb, 0x0d, 0x0a]))
+ fpyc.write(bytes([0x2a, 0xeb, 0x0d, 0x0a]))
with pytest.raises(NoCode, match="Bad magic number in .pyc file"):
run_python_file([pycfile])
diff --git a/tests/test_numbits.py b/tests/test_numbits.py
index 946f8fcb..98399086 100644
--- a/tests/test_numbits.py
+++ b/tests/test_numbits.py
@@ -10,7 +10,6 @@ from hypothesis import example, given, settings
from hypothesis.strategies import sets, integers
from coverage import env
-from coverage.backward import byte_to_int
from coverage.numbits import (
nums_to_numbits, numbits_to_nums, numbits_union, numbits_intersection,
numbits_any_intersection, num_in_numbits, register_sqlite_functions,
@@ -33,7 +32,7 @@ if env.METACOV:
def good_numbits(numbits):
"""Assert that numbits is good."""
# It shouldn't end with a zero byte, that should have been trimmed off.
- assert (not numbits) or (byte_to_int(numbits[-1]) != 0)
+ assert (not numbits) or (numbits[-1] != 0)
class NumbitsOpTest(CoverageTest):