bpo-45214: add the LOAD_NONE opcode by iritkatriel · Pull Request #28376 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/dis.rst
48 changes: 25 additions & 23 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
MAKE_FUNCTION_FLAGS = ('defaults', 'kwdefaults', 'annotations', 'closure')

LOAD_CONST = opmap['LOAD_CONST']
LOAD_NONE = opmap['LOAD_NONE']

def _try_compile(source, name):
"""Attempts to compile the given source, first as an expression and
Expand Down Expand Up @@ -331,6 +332,8 @@ def _get_const_value(op, arg, co_consts):
if op == LOAD_CONST:
if co_consts is not None:
argval = co_consts[arg]
elif op == LOAD_NONE:
argval = None
return argval

def _get_const_info(op, arg, co_consts):
Expand Down Expand Up @@ -574,7 +577,7 @@ def _find_imports(co):
if op == IMPORT_NAME and i >= 2:
from_op = opargs[i-1]
level_op = opargs[i-2]
if (from_op[0] in hasconst and level_op[0] in hasconst):
if from_op[0] in hasconst and level_op[0] in hasconst:
level = _get_const_value(level_op[0], level_op[1], consts)
fromlist = _get_const_value(from_op[0], from_op[1], consts)
yield (names[oparg], level, fromlist)
Expand Down
2 changes: 2 additions & 0 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def jabs_op(name, op):
def_op('INPLACE_AND', 77)
def_op('INPLACE_XOR', 78)
def_op('INPLACE_OR', 79)
def_op('LOAD_NONE', 80)
hasconst.append(80)

def_op('LIST_TO_TUPLE', 82)
def_op('RETURN_VALUE', 83)
Expand Down
22 changes: 14 additions & 8 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,20 +1723,19 @@ def test_get_docstring(self):
tree = ast.parse("'docstring'\nx = 1")
self.assertEqual(ast.get_docstring(tree), 'docstring')

def get_load_const(self, tree):
# Compile to bytecode, disassemble and get parameter of LOAD_CONST
def get_values_for_opname(self, tree, opname):
# Compile to bytecode, disassemble and get parameter of opname
# instructions
co = compile(tree, '<string>', 'exec')
consts = []
for instr in dis.get_instructions(co):
if instr.opname == 'LOAD_CONST':
if instr.opname == opname:
consts.append(instr.argval)
return consts

@support.cpython_only
def test_load_const(self):
consts = [None,
True, False,
consts = [True, False,
124,
2.0,
3j,
Expand All @@ -1746,11 +1745,15 @@ def test_load_const(self):

code = '\n'.join(['x={!r}'.format(const) for const in consts])
code += '\nx = ...'
consts.extend((Ellipsis, None))
consts.append(Ellipsis)

code += '\nx = None' # LOAD_NONE, so not added to consts

tree = ast.parse(code)
self.assertEqual(self.get_load_const(tree),
self.assertEqual(self.get_values_for_opname(tree, 'LOAD_CONST'),
consts)
self.assertEqual(self.get_values_for_opname(tree, 'LOAD_NONE'),
[None, None]) # One None from return at the end

# Replace expression nodes with constants
for assign, const in zip(tree.body, consts):
Expand All @@ -1759,8 +1762,11 @@ def test_load_const(self):
ast.copy_location(new_node, assign.value)
assign.value = new_node

self.assertEqual(self.get_load_const(tree),
self.assertEqual(self.get_values_for_opname(tree, 'LOAD_CONST'),
consts)
self.assertEqual(self.get_values_for_opname(tree, 'LOAD_NONE'),
[None, None]) # One None from return at the end


def test_literal_eval(self):
tree = ast.parse("1 + 2")
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
freevars: ()
nlocals: 0
flags: 3
consts: ("'doc string'", 'None')
consts: ("'doc string'",)

>>> def keywordonly_args(a,b,*,k1):
... return a,b,k1
Expand Down Expand Up @@ -377,7 +377,7 @@ def test_co_positions_artificial_instructions(self):
],
[
("PUSH_EXC_INFO", None),
("LOAD_CONST", None), # artificial 'None'
("LOAD_NONE", None), # artificial 'None'
("STORE_NAME", "e"), # XX: we know the location for this
("DELETE_NAME", "e"),
("RERAISE", 1),
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_compile.py
Loading