Skip to content

Commit

Permalink
Handle bytecode with -OO flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-mitchell committed Aug 29, 2022
1 parent c32a49e commit ee67b8b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion pure_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import warnings
import weakref

__version__ = '6.0.1'
__version__ = '6.0.2'

is_development = not hasattr(sys, 'frozen')
missing_method_warnings = []
Expand Down Expand Up @@ -173,12 +173,18 @@ def _is_empty_function(func, unwrap=False):
byte_code = code_obj.co_code
if byte_code.startswith(b'\x81\x01'):
byte_code = byte_code[2:] # remove GEN_START async def opcode
if byte_code.startswith(b'\x97\x00'):
byte_code = byte_code[2:] # remove RESUME opcode added in 3.11
if byte_code.startswith(b'\t\x00'):
byte_code = byte_code[2:] # remove NOP opcode
if byte_code.startswith(b'K\x00'):
byte_code = byte_code[2:] # remove RETURN_GENERATOR async def opcode in py311
if byte_code.startswith(b'\x01'):
byte_code = byte_code[2:] # remove POP_TOP
if byte_code.startswith(b'\x97\x00'):
byte_code = byte_code[2:] # remove RESUME opcode added in 3.11
if byte_code.startswith(b'\t\x00'):
byte_code = byte_code[2:] # remove NOP opcode
if byte_code in (b'd\x00\x00S', b'd\x00S\x00') and code_obj.co_consts[0] is None:
return True
if byte_code in (b'd\x01\x00S', b'd\x01S\x00') and code_obj.co_consts[1] is None:
Expand All @@ -189,12 +195,18 @@ def _is_empty_function(func, unwrap=False):
return True # this never happens
if instructions[0].opname == 'GEN_START':
instructions.pop(0)
if instructions[0].opname == 'RESUME':
instructions.pop(0)
if instructions[0].opname == 'NOP':
instructions.pop(0)
if instructions[0].opname == 'RETURN_GENERATOR':
instructions.pop(0)
if instructions[0].opname == 'POP_TOP':
instructions.pop(0)
if instructions[0].opname == 'RESUME':
instructions.pop(0)
if instructions[0].opname == 'NOP':
instructions.pop(0)
if instructions[-1].opname == 'RETURN_VALUE': # returns TOS (top of stack)
instruction = instructions[-2]
if not (instruction.opname == 'LOAD_CONST' and code_obj.co_consts[instruction.arg] is None): # TOS is None
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pure_interface
version = 6.0.1
version = 6.0.2
description = A Python interface library that disallows function body content on interfaces and supports adaption.
keywords = abc interface adapt adaption mapper structural typing dataclass
author = Tim Mitchell
Expand Down

0 comments on commit ee67b8b

Please sign in to comment.