Commit 1daff8b1 authored by Tobias Pape's avatar Tobias Pape

RPython chokes on dicts with values of different types

this is a problem in the case when we want to "as_str" the pseudo-enum
. So we just filter the int-value part of the dict and, as a small optimization
use a translate-time unrolling of the if-switch.
parent 972fead3
from rpython.rlib.unroll import unrolling_iterable
# Symbol is a 'lightweight' enum, in Python 3.4, we could use Enum as superclass
class Symbol(object):
NONE = -1
......@@ -33,9 +35,16 @@ class Symbol(object):
KeywordSequence = 29
OperatorSequence = 30
def symbol_as_str(symbol):
for key, val in Symbol.__dict__:
symbols = unrolling_iterable(int_constants_of(Symbol))
for (key, val) in symbols:
if val == symbol:
return key
raise ValueError('No Symbol defined for the value %d.' % symbol)
raise ValueError('No Symbol defined for the value %d.' % symbol)
def int_constants_of(cls):
out = {}
for key, value in cls.__dict__.items():
if isinstance(value, int):
out[key] = value
return out
from rpython.rlib.unroll import unrolling_iterable
class Bytecodes(object):
# Bytecodes used by the Simple Object Machine (SOM)
......@@ -76,8 +78,16 @@ def bytecode_as_str(bytecode):
if not isinstance(bytecode, int):
raise ValueError('bytecode is expected to be an integer.')
for key, val in Bytecodes.__dict__.iteritems():
bytecodes = unrolling_iterable(int_constants_of(Bytecodes))
for key, val in bytecodes:
if val == bytecode:
return key.upper()
raise ValueError('No defined defined for the value %d.' % bytecode)
def int_constants_of(cls):
out = {}
for key, value in cls.__dict__.items():
if isinstance(value, int):
out[key] = value
return out
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