Skip to content

Commit

Permalink
馃棑 Mar 28, 2022 12:26:40 PM
Browse files Browse the repository at this point in the history
馃悰 fixes #19
馃攳 added some float type handlers and tests
  • Loading branch information
securisecctf committed Mar 28, 2022
1 parent 631a999 commit 35aff9d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
import subprocess
import sys
import struct
import webbrowser
from configparser import ConfigParser
from importlib.machinery import SourceFileLoader
Expand Down Expand Up @@ -502,6 +503,8 @@ def _convert_to_bytes(self) -> bytes:
return str(self.state).encode()
elif isinstance(self.state, bytearray):
return bytes(self.state)
elif isinstance(self.state, float):
return bytearray(struct.pack("f", self.state))
else: # pragma: no cover
# todo check more types here
raise NotImplementedError
Expand Down Expand Up @@ -536,6 +539,8 @@ def _convert_to_str(self) -> str:
return str(self.state)
elif isinstance(self.state, bytearray):
return bytearray(self.state).decode()
elif isinstance(self.state, float):
return format(self.state, "f")
else: # pragma: no cover
# todo check more types here
raise NotImplementedError
Expand Down
3 changes: 2 additions & 1 deletion chepy/modules/aritmeticlogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def multiply(self, n: int) -> AritmeticLogicT:

@ChepyDecorators.call_stack
def divide(self, n: int) -> AritmeticLogicT:
"""Divide a number to the state
"""Divide a number to the state. Chepy is not optimized for float math.
Subsequent methods may fail.
Args:
n (int): Number to divide with
Expand Down
3 changes: 3 additions & 0 deletions tests/test_aritmeticlogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ def test_multiply():
def test_divide():
assert Chepy("0x40").divide(2).o == 32

def test_divide_float():
assert Chepy("179").divide(178).to_hex().o == b'17b8803f'


def test_power():
assert Chepy("0x02").power(2).o == 4
Expand Down

0 comments on commit 35aff9d

Please sign in to comment.