changeset 384:94f5b719ad0b

Small refactor
author Windel Bouwman
date Sun, 27 Apr 2014 17:50:25 +0200
parents 173e20a47fda
children d056b552d3f4
files python/ppci/assembler.py python/ppci/objectfile.py test/testemulation.py
diffstat 3 files changed, 8 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/python/ppci/assembler.py	Sun Apr 27 17:40:39 2014 +0200
+++ b/python/ppci/assembler.py	Sun Apr 27 17:50:25 2014 +0200
@@ -2,7 +2,7 @@
 import re
 import pyyacc
 from baselex import BaseLexer
-from . import Token, CompilerError, SourceLocation
+from . import Token, CompilerError, SourceLocation, make_num
 from .target import Target, Label
 
 
@@ -37,10 +37,7 @@
         return (typ, val)
 
     def handle_number(self, typ, val):
-        if val.startswith('0x'):
-            val = int(val[2:], 16)
-        else:
-            val = int(val)
+        val = make_num(val)
         typ = bit_type(val)
         return typ, val
 
--- a/python/ppci/objectfile.py	Sun Apr 27 17:40:39 2014 +0200
+++ b/python/ppci/objectfile.py	Sun Apr 27 17:50:25 2014 +0200
@@ -6,7 +6,7 @@
 
 import json
 import binascii
-from . import CompilerError
+from . import CompilerError, make_num
 
 class Symbol:
     def __init__(self, name, value, section):
@@ -139,23 +139,16 @@
     return res
 
 
-def make_int(txt):
-    if txt.startswith('0x'):
-        return int(txt[2:], 16)
-    else:
-        return int(txt)
-
-
 def deserialize(d):
     obj = ObjectFile()
     for section in d['sections']:
         so = obj.get_section(section['name'])
-        so.address = make_int(section['address'])
+        so.address = make_num(section['address'])
         so.data = bytearray(binascii.unhexlify(section['data'].encode('ascii')))
     for reloc in d['relocations']:
-        obj.add_relocation(reloc['symbol'], make_int(reloc['offset']),
+        obj.add_relocation(reloc['symbol'], make_num(reloc['offset']),
             reloc['type'], reloc['section'])
     for sym in d['symbols']:
-        obj.add_symbol(sym['name'], make_int(sym['value']), sym['section'])
+        obj.add_symbol(sym['name'], make_num(sym['value']), sym['section'])
     return obj
 
--- a/test/testemulation.py	Sun Apr 27 17:40:39 2014 +0200
+++ b/test/testemulation.py	Sun Apr 27 17:50:25 2014 +0200
@@ -21,6 +21,8 @@
 
 def has_qemu():
     """ Determines if qemu is possible """
+    if not hasattr(shutil, 'which'):
+        return False
     return bool(shutil.which(qemu_app))