Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception has occurred: KeyError #1

Open
LeCoxer opened this issue Apr 10, 2024 · 0 comments
Open

Exception has occurred: KeyError #1

LeCoxer opened this issue Apr 10, 2024 · 0 comments

Comments

@LeCoxer
Copy link

LeCoxer commented Apr 10, 2024

middle.py:
def middle(x, y, z):
m = z
if y < z:
if x < y:
m = y
elif x < z:
m = y # bug
else:
if x > y:
m = y
elif x > z:
m = x
return m

tmp.py:
import sflkitlib.lib

def middle(x, y, z):
sflkitlib.lib.add_line_event(0)
m = z
sflkitlib.lib.add_line_event(1)
if y < z:
sflkitlib.lib.add_line_event(2)
if x < y:
sflkitlib.lib.add_line_event(3)
m = y
else:
sflkitlib.lib.add_line_event(4)
if x < z:
sflkitlib.lib.add_line_event(5)
m = y
else:
sflkitlib.lib.add_line_event(6)
if x > y:
sflkitlib.lib.add_line_event(7)
m = y
else:
sflkitlib.lib.add_line_event(8)
if x > z:
sflkitlib.lib.add_line_event(9)
m = x
sflkitlib.lib.add_line_event(10)
return m

working.py:
import enum
import importlib
import inspect
import os
import shutil
import middle

from IPython.display import HTML

from sflkit.color import ColorCode
from sflkit import instrument_config, analyze_config
from sflkit.config import Config

class TestResult(enum.Enum):

def repr(self):
    return self.value

PASS = 'PASS'
FAIL = 'FAIL'

def test(function, x, y, z, expected):
try:
if function(x, y, z) == expected:
return TestResult.PASS
else:
return TestResult.FAIL
except BaseException:
return TestResult.FAIL

def test_middle(x, y, z, expected):
return test(middle, x, y, z, expected)

test_middle(3, 2, 1, expected=2)
test_middle(3, 1, 2, expected=2)
test_middle(2, 1, 3, expected=2)

source = inspect.getsource(middle)
print(source)

middle_py = 'middle.py'
tmp_py = 'tmp.py'

with open(middle_py, 'w') as fp:
fp.write(source)

def test_middle_import(x, y, z, expected):
from middle import middle
return test(middle, x, y, z, expected)

test_middle_import(3, 2, 1, expected=2), test_middle_import(3, 1, 2, expected=2), test_middle_import(2, 1, 3, expected=2)

language='Python'
predicates='Line'
metrics='Tarantula'
passing='event-files/0,event-files/1'
failing='event-files/2'

def get_config():
return Config.create(path=middle_py, working=tmp_py, language=language, predicates=predicates, metrics=metrics, passing=passing, failing=failing)

def instrument(out=True):
instrument_config(get_config())
if out:
with open(tmp_py, 'r') as fp:
print(fp.read())

instrument()

def test_tmp(x, y, z, expected):
import tmp
importlib.reload(tmp)
tmp.sflkitlib.lib.reset()
try:
return test(tmp.middle, x, y, z, expected)
finally:
tmp.sflkitlib.lib.dump_events()
del tmp

event_files = 'event-files'

def run_tests():
if os.path.exists(event_files):
shutil.rmtree(event_files)
os.mkdir(event_files)
os.environ['EVENTS_PATH'] = os.path.join(event_files, '0')
test_tmp(3, 2, 1, expected=2)
os.environ['EVENTS_PATH'] = os.path.join(event_files, '1')
test_tmp(3, 1, 2, expected=2)
os.environ['EVENTS_PATH'] = os.path.join(event_files, '2')
test_tmp(2, 1, 3, expected=2)

def analyze():
run_tests()
print(Config)
return analyze_config(get_config())

results = analyze()

results

def sfl():
instrument(out=False)
results = analyze()
code = ColorCode(results[predicates.upper()][metrics])
return HTML(code.code(middle_py, source, color=True, suspiciousness=True))

sfl()

I wanted to test the library's functionality on a demo case
An exception KeyError is thrown
sflkit version 0.2.16
The same thing happens when using it through the terminal

Exception:
Traceback (most recent call last):
File "C:\SFLkitpr\working.py", line 98, in
results = analyze()
File "C:\SFLkitpr\working.py", line 96, in analyze
return analyze_config(get_config())
File "..\sflkit_init_.py", line 46, in analyze_config
analyzer.analyze()
File "..\sflkit\analysis\analyzer.py", line 32, in analyze
self._analyze(event_file)
File "..\sflkit\analysis\analyzer.py", line 24, in _analyze
for event in event_file.load():
File "..\sflkit\model\event_file.py", line 34, in load
yield event.load_next_event(self._file_pointer, self.mapping.mapping)
File "..\sflkitlib\events\event.py", line 587, in load_next_event
event = events[read_int(stream, int.from_bytes(test, ENDIAN))]
KeyError: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant