From 92777d5e5aed1753bafe07265dbe98b2d271815b Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Thu, 12 Sep 2019 10:02:59 +0100 Subject: bpo-18578: Rename and document test.bytecode_helper as test.support.bytecode_helper (GH-15168) Rename and document test.bytecode_helper as test.support.bytecode_helper --- Doc/library/test.rst | 30 ++++++++++++++++ Lib/test/bytecode_helper.py | 41 ---------------------- Lib/test/support/bytecode_helper.py | 41 ++++++++++++++++++++++ Lib/test/test_dis.py | 2 +- Lib/test/test_peepholer.py | 2 +- .../2019-08-07-19-34-07.bpo-18578.xfvdb_.rst | 2 ++ 6 files changed, 75 insertions(+), 43 deletions(-) delete mode 100644 Lib/test/bytecode_helper.py create mode 100644 Lib/test/support/bytecode_helper.py create mode 100644 Misc/NEWS.d/next/Library/2019-08-07-19-34-07.bpo-18578.xfvdb_.rst diff --git a/Doc/library/test.rst b/Doc/library/test.rst index 6eef5c6..5dde55c 100644 --- a/Doc/library/test.rst +++ b/Doc/library/test.rst @@ -1503,3 +1503,33 @@ script execution tests. containing the *source*. If *compiled* is ``True``, both source files will be compiled and added to the zip package. Return a tuple of the full zip path and the archive name for the zip file. + + +:mod:`test.support.bytecode_helper` --- Support tools for testing correct bytecode generation +============================================================================================= + +.. module:: test.support.bytecode_helper + :synopsis: Support tools for testing correct bytecode generation. + +The :mod:`test.support.bytecode_helper` module provides support for testing +and inspecting bytecode generation. + +The module defines the follwing class: + +.. class:: BytecodeTestCase(unittest.TestCase) + + This class has custom assertion methods for inspecting bytecode. + +.. method:: BytecodeTestCase.get_disassembly_as_string(co) + + Return the disassembly of *co* as string. + + +.. method:: BytecodeTestCase.assertInBytecode(x, opname, argval=_UNSPECIFIED) + + Return instr if *opname* is found, otherwise throws :exc:`AssertionError`. + + +.. method:: BytecodeTestCase.assertNotInBytecode(x, opname, argval=_UNSPECIFIED) + + Throws :exc:`AssertionError` if *opname* is found. diff --git a/Lib/test/bytecode_helper.py b/Lib/test/bytecode_helper.py deleted file mode 100644 index 347d603..0000000 --- a/Lib/test/bytecode_helper.py +++ /dev/null @@ -1,41 +0,0 @@ -"""bytecode_helper - support tools for testing correct bytecode generation""" - -import unittest -import dis -import io - -_UNSPECIFIED = object() - -class BytecodeTestCase(unittest.TestCase): - """Custom assertion methods for inspecting bytecode.""" - - def get_disassembly_as_string(self, co): - s = io.StringIO() - dis.dis(co, file=s) - return s.getvalue() - - def assertInBytecode(self, x, opname, argval=_UNSPECIFIED): - """Returns instr if op is found, otherwise throws AssertionError""" - for instr in dis.get_instructions(x): - if instr.opname == opname: - if argval is _UNSPECIFIED or instr.argval == argval: - return instr - disassembly = self.get_disassembly_as_string(x) - if argval is _UNSPECIFIED: - msg = '%s not found in bytecode:\n%s' % (opname, disassembly) - else: - msg = '(%s,%r) not found in bytecode:\n%s' - msg = msg % (opname, argval, disassembly) - self.fail(msg) - - def assertNotInBytecode(self, x, opname, argval=_UNSPECIFIED): - """Throws AssertionError if op is found""" - for instr in dis.get_instructions(x): - if instr.opname == opname: - disassembly = self.get_disassembly_as_string(x) - if argval is _UNSPECIFIED: - msg = '%s occurs in bytecode:\n%s' % (opname, disassembly) - elif instr.argval == argval: - msg = '(%s,%r) occurs in bytecode:\n%s' - msg = msg % (opname, argval, disassembly) - self.fail(msg) diff --git a/Lib/test/support/bytecode_helper.py b/Lib/test/support/bytecode_helper.py new file mode 100644 index 0000000..348e277 --- /dev/null +++ b/Lib/test/support/bytecode_helper.py @@ -0,0 +1,41 @@ +"""bytecode_helper - support tools for testing correct bytecode generation""" + +import unittest +import dis +import io + +_UNSPECIFIED = object() + +class BytecodeTestCase(unittest.TestCase): + """Custom assertion methods for inspecting bytecode.""" + + def get_disassembly_as_string(self, co): + s = io.StringIO() + dis.dis(co, file=s) + return s.getvalue() + + def assertInBytecode(self, x, opname, argval=_UNSPECIFIED): + """Returns instr if opname is found, otherwise throws AssertionError""" + for instr in dis.get_instructions(x): + if instr.opname == opname: + if argval is _UNSPECIFIED or instr.argval == argval: + return instr + disassembly = self.get_disassembly_as_string(x) + if argval is _UNSPECIFIED: + msg = '%s not found in bytecode:\n%s' % (opname, disassembly) + else: + msg = '(%s,%r) not found in bytecode:\n%s' + msg = msg % (opname, argval, disassembly) + self.fail(msg) + + def assertNotInBytecode(self, x, opname, argval=_UNSPECIFIED): + """Throws AssertionError if opname is found""" + for instr in dis.get_instructions(x): + if instr.opname == opname: + disassembly = self.get_disassembly_as_string(x) + if argval is _UNSPECIFIED: + msg = '%s occurs in bytecode:\n%s' % (opname, disassembly) + elif instr.argval == argval: + msg = '(%s,%r) occurs in bytecode:\n%s' + msg = msg % (opname, argval, disassembly) + self.fail(msg) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 11f97e6..fecf203 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1,7 +1,7 @@ # Minimal tests for dis module from test.support import captured_stdout -from test.bytecode_helper import BytecodeTestCase +from test.support.bytecode_helper import BytecodeTestCase import unittest import sys import dis diff --git a/Lib/test/test_peepholer.py b/Lib/test/test_peepholer.py index c90a532..47dee33 100644 --- a/Lib/test/test_peepholer.py +++ b/Lib/test/test_peepholer.py @@ -1,7 +1,7 @@ import dis import unittest -from test.bytecode_helper import BytecodeTestCase +from test.support.bytecode_helper import BytecodeTestCase def count_instr_recursively(f, opname): diff --git a/Misc/NEWS.d/next/Library/2019-08-07-19-34-07.bpo-18578.xfvdb_.rst b/Misc/NEWS.d/next/Library/2019-08-07-19-34-07.bpo-18578.xfvdb_.rst new file mode 100644 index 0000000..cc0e02f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-08-07-19-34-07.bpo-18578.xfvdb_.rst @@ -0,0 +1,2 @@ +Renamed and documented `test.bytecode_helper` as `test.support.bytecode_helper`. +Patch by Joannah Nanjekye. \ No newline at end of file -- cgit v0.12