Modernize Python 2 code to get ready for Python 3

This commit is contained in:
cclauss
2018-12-01 23:58:26 +01:00
parent 701bb1b44e
commit d7bacfa611
83 changed files with 398 additions and 323 deletions
+11 -10
View File
@@ -1,3 +1,4 @@
from __future__ import print_function
#---------------------------------------------------------------------
# IDAPython - Python plugin for Interactive Disassembler
#
@@ -26,7 +27,7 @@ uint64 = ctypes.c_uint64
uint16 = ctypes.c_ushort
ushort = uint16
# __EA64__ is set if IDA is running in 64-bit mode
__EA64__ = ida_idaapi.BADADDR == 0xFFFFFFFFFFFFFFFFL
__EA64__ = ida_idaapi.BADADDR == 0xFFFFFFFFFFFFFFFF
ea_t = uint64 if __EA64__ else uint32
# parse a ctypes struct from byte data in str_ at 'off'
@@ -92,8 +93,8 @@ def unpack_dq(buf, off):
(xl, off) = unpack_dd(buf, off)
(xh, off) = unpack_dd(buf, off)
x = (long(xh) << 32) | xl
if x > 0x8000000000000000L:
x = x - 0x10000000000000000L
if x > 0x8000000000000000:
x = x - 0x10000000000000000
return (x, off)
def unpack_ea(buf, off):
@@ -282,7 +283,7 @@ class Dex(object):
nn_var = self.get_nn_var(from_ea)
val = nn_var.supval(method_idx, Dex.DEXVAR_METHOD)
if len(val) != ctypes.sizeof(dex_method):
print "bad data in DEXVAR_METHOD for index 0x%X" % method_idx
print("bad data in DEXVAR_METHOD for index 0x%X" % method_idx)
return None
method = get_struct(val,0, dex_method)
return method
@@ -430,7 +431,7 @@ class Dex(object):
nn_var = self.get_nn_var(from_ea)
val = nn_var.supval(field_idx, Dex.DEXVAR_FIELD)
if len(val) != ctypes.sizeof(dex_field):
print "bad data in DEXVAR_FIELD for index 0x%X" % field_idx
print("bad data in DEXVAR_FIELD for index 0x%X" % field_idx)
return None
field = get_struct(val,0, dex_field)
return field
@@ -462,14 +463,14 @@ if __name__ == '__main__':
# reproduce IDA function header
f = idaapi.get_func(here())
if not f:
print "ERROR: must be in a function!"
print("ERROR: must be in a function!")
exit(1)
func_start_ea = f.start_ea
methno = dex.get_method_idx(func_start_ea)
func_method = dex.get_method(func_start_ea, methno)
if func_method is None:
print "ERROR: Missing method info"
print("ERROR: Missing method info")
exit(1)
out = ""
# Return type
@@ -495,9 +496,9 @@ if __name__ == '__main__':
out += "%x" % methno
# Method parameters
if func_method.nparams == 0:
print out + "()"
print(out + "()")
else:
print out + "("
print(out + "(")
out = ""
maxp = min(func_method.nparams, 32)
start_reg = func_method.reg_total - func_method.reg_params
@@ -519,4 +520,4 @@ if __name__ == '__main__':
else:
out += r.user
out += ')' if i + 1 == maxp else ','
print out
print(out)
+5 -5
View File
@@ -263,7 +263,7 @@ def Chunks(start):
while status:
chunk = func_iter.chunk()
yield (chunk.start_ea, chunk.end_ea)
status = func_iter.next()
status = next(func_iter)
def Modules():
@@ -419,7 +419,7 @@ def GetDataList(ea, count, itemsize=1):
elif itemsize == 8:
getdata = ida_bytes.get_qword
else:
raise ValueError, "Invalid data size! Must be 1, 2, 4 or 8"
raise ValueError("Invalid data size! Must be 1, 2, 4 or 8")
endea = ea + itemsize * count
curea = ea
@@ -593,7 +593,7 @@ def _Assemble(ea, line):
"""
Please refer to Assemble() - INTERNAL USE ONLY
"""
if type(line) == types.StringType:
if type(line) == bytes:
lines = [line]
else:
lines = line
@@ -636,7 +636,7 @@ def _copy_obj(src, dest, skip_list = None):
Otherwise dest should be an instance of another class
@return: A new instance or "dest"
"""
if type(dest) == types.StringType:
if type(dest) == bytes:
# instantiate a new destination class of the specified type name?
dest = new.classobj(dest, (), {})
for x in dir(src):
@@ -703,7 +703,7 @@ class __process_ui_actions_helper(object):
elif isinstance(actions, (list, tuple)):
lst = actions
else:
raise ValueError, "Must pass a string, list or a tuple"
raise ValueError("Must pass a string, list or a tuple")
# Remember the action list and the flags
self.__action_list = lst
+53 -52
View File
@@ -25,6 +25,7 @@ the byte value). These 32 bits are used in get_full_flags/get_flags functions.
This file is subject to change without any notice.
Future versions of IDA may use other definitions.
"""
from __future__ import print_function
# FIXME: Perhaps those should be loaded on-demand
import ida_idaapi
import ida_auto
@@ -68,7 +69,7 @@ import time
import types
import sys
__EA64__ = ida_idaapi.BADADDR == 0xFFFFFFFFFFFFFFFFL
__EA64__ = ida_idaapi.BADADDR == 0xFFFFFFFFFFFFFFFF
WORDMASK = 0xFFFFFFFFFFFFFFFF if __EA64__ else 0xFFFFFFFF
class DeprecatedIDCError(Exception):
"""
@@ -94,7 +95,7 @@ def _IDC_GetAttr(obj, attrmap, attroffs):
return getattr(obj, attrmap[attroffs][1])
else:
errormsg = "attribute with offset %d not found, check the offset and report the problem" % attroffs
raise KeyError, errormsg
raise KeyError(errormsg)
def _IDC_SetAttr(obj, attrmap, attroffs, value):
@@ -105,11 +106,11 @@ def _IDC_SetAttr(obj, attrmap, attroffs, value):
# check for read-only atributes
if attroffs in attrmap:
if attrmap[attroffs][0]:
raise KeyError, "attribute with offset %d is read-only" % attroffs
raise KeyError("attribute with offset %d is read-only" % attroffs)
elif hasattr(obj, attrmap[attroffs][1]):
return setattr(obj, attrmap[attroffs][1], value)
errormsg = "attribute with offset %d not found, check the offset and report the problem" % attroffs
raise KeyError, errormsg
raise KeyError(errormsg)
BADADDR = ida_idaapi.BADADDR # Not allowed address value
@@ -293,12 +294,12 @@ NEF_FLAT = ida_loader.NEF_FLAT # Autocreated FLAT group (PE)
# ----------------------------------------------------------------------------
# M I S C E L L A N E O U S
# ----------------------------------------------------------------------------
def value_is_string(var): raise NotImplementedError, "this function is not needed in Python"
def value_is_long(var): raise NotImplementedError, "this function is not needed in Python"
def value_is_float(var): raise NotImplementedError, "this function is not needed in Python"
def value_is_func(var): raise NotImplementedError, "this function is not needed in Python"
def value_is_pvoid(var): raise NotImplementedError, "this function is not needed in Python"
def value_is_int64(var): raise NotImplementedError, "this function is not needed in Python"
def value_is_string(var): raise NotImplementedError("this function is not needed in Python")
def value_is_long(var): raise NotImplementedError("this function is not needed in Python")
def value_is_float(var): raise NotImplementedError("this function is not needed in Python")
def value_is_func(var): raise NotImplementedError("this function is not needed in Python")
def value_is_pvoid(var): raise NotImplementedError("this function is not needed in Python")
def value_is_int64(var): raise NotImplementedError("this function is not needed in Python")
def to_ea(seg, off):
"""
@@ -307,19 +308,19 @@ def to_ea(seg, off):
return (seg << 4) + off
def form(format, *args):
raise DeprecatedIDCError, "form() is deprecated. Use python string operations instead."
raise DeprecatedIDCError("form() is deprecated. Use python string operations instead.")
def substr(s, x1, x2):
raise DeprecatedIDCError, "substr() is deprecated. Use python string operations instead."
raise DeprecatedIDCError("substr() is deprecated. Use python string operations instead.")
def strstr(s1, s2):
raise DeprecatedIDCError, "strstr() is deprecated. Use python string operations instead."
raise DeprecatedIDCError("strstr() is deprecated. Use python string operations instead.")
def strlen(s):
raise DeprecatedIDCError, "strlen() is deprecated. Use python string operations instead."
raise DeprecatedIDCError("strlen() is deprecated. Use python string operations instead.")
def xtol(s):
raise DeprecatedIDCError, "xtol() is deprecated. Use python long() instead."
raise DeprecatedIDCError("xtol() is deprecated. Use python long() instead.")
def atoa(ea):
"""
@@ -332,10 +333,10 @@ def atoa(ea):
return ida_kernwin.ea2str(ea)
def ltoa(n, radix):
raise DeprecatedIDCError, "ltoa() is deprecated. Use python string operations instead."
raise DeprecatedIDCError("ltoa() is deprecated. Use python string operations instead.")
def atol(s):
raise DeprecatedIDCError, "atol() is deprecated. Use python long() instead."
raise DeprecatedIDCError("atol() is deprecated. Use python long() instead.")
def rotate_left(value, count, nbits, offset):
@@ -414,7 +415,7 @@ def eval_idc(expr):
elif rv.vtype == '\x07': # VT_STR
return rv.c_str()
else:
raise NotImplementedError, "eval_idc() supports only expressions returning strings or longs"
raise NotImplementedError("eval_idc() supports only expressions returning strings or longs")
def EVAL_FAILURE(code):
@@ -425,7 +426,7 @@ def EVAL_FAILURE(code):
@return: True if there was an evaluation error
"""
return type(code) == types.StringType and code.startswith("IDC_FAILURE: ")
return type(code) == bytes and code.startswith("IDC_FAILURE: ")
def save_database(idbname, flags=0):
@@ -855,15 +856,15 @@ def set_array_params(ea, flags, litems, align):
"""
return eval_idc("set_array_params(0x%X, 0x%X, %d, %d)"%(ea, flags, litems, align))
AP_ALLOWDUPS = 0x00000001L # use 'dup' construct
AP_SIGNED = 0x00000002L # treats numbers as signed
AP_INDEX = 0x00000004L # display array element indexes as comments
AP_ARRAY = 0x00000008L # reserved (this flag is not stored in database)
AP_IDXBASEMASK = 0x000000F0L # mask for number base of the indexes
AP_IDXDEC = 0x00000000L # display indexes in decimal
AP_IDXHEX = 0x00000010L # display indexes in hex
AP_IDXOCT = 0x00000020L # display indexes in octal
AP_IDXBIN = 0x00000030L # display indexes in binary
AP_ALLOWDUPS = 0x00000001 # use 'dup' construct
AP_SIGNED = 0x00000002 # treats numbers as signed
AP_INDEX = 0x00000004 # display array element indexes as comments
AP_ARRAY = 0x00000008 # reserved (this flag is not stored in database)
AP_IDXBASEMASK = 0x000000F0 # mask for number base of the indexes
AP_IDXDEC = 0x00000000 # display indexes in decimal
AP_IDXHEX = 0x00000010 # display indexes in hex
AP_IDXOCT = 0x00000020 # display indexes in octal
AP_IDXBIN = 0x00000030 # display indexes in binary
op_bin = ida_bytes.op_bin
op_oct = ida_bytes.op_oct
@@ -1790,7 +1791,7 @@ def get_inf_attr(offset):
def set_inf_attr(offset, value):
if offset == INF_PROCNAME:
raise NotImplementedError, "Please use ida_idp.set_processor_type() to change processor"
raise NotImplementedError("Please use ida_idp.set_processor_type() to change processor")
# We really want to go through IDC's equivalent, because it might
# have side-effects (i.e., send a notification, etc...)
return eval_idc("set_inf_attr(%d, %d)" % (offset, value))
@@ -2872,26 +2873,26 @@ def get_xref_type():
@return: constants fl_* or dr_*
"""
raise DeprecatedIDCError, "use XrefsFrom() XrefsTo() from idautils instead."
raise DeprecatedIDCError("use XrefsFrom() XrefsTo() from idautils instead.")
#----------------------------------------------------------------------------
# F I L E I / O
#----------------------------------------------------------------------------
def fopen(f, mode):
raise DeprecatedIDCError, "fopen() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("fopen() deprecated. Use Python file objects instead.")
def fclose(handle):
raise DeprecatedIDCError, "fclose() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("fclose() deprecated. Use Python file objects instead.")
def filelength(handle):
raise DeprecatedIDCError, "filelength() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("filelength() deprecated. Use Python file objects instead.")
def fseek(handle, offset, origin):
raise DeprecatedIDCError, "fseek() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("fseek() deprecated. Use Python file objects instead.")
def ftell(handle):
raise DeprecatedIDCError, "ftell() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("ftell() deprecated. Use Python file objects instead.")
def LoadFile(filepath, pos, ea, size):
@@ -2945,31 +2946,31 @@ def savefile(filepath, pos, ea, size): return SaveFile(filepath, pos, ea, size)
def fgetc(handle):
raise DeprecatedIDCError, "fgetc() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("fgetc() deprecated. Use Python file objects instead.")
def fputc(byte, handle):
raise DeprecatedIDCError, "fputc() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("fputc() deprecated. Use Python file objects instead.")
def fprintf(handle, format, *args):
raise DeprecatedIDCError, "fprintf() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("fprintf() deprecated. Use Python file objects instead.")
def readshort(handle, mostfirst):
raise DeprecatedIDCError, "readshort() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("readshort() deprecated. Use Python file objects instead.")
def readlong(handle, mostfirst):
raise DeprecatedIDCError, "readlong() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("readlong() deprecated. Use Python file objects instead.")
def writeshort(handle, word, mostfirst):
raise DeprecatedIDCError, "writeshort() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("writeshort() deprecated. Use Python file objects instead.")
def writelong(handle, dword, mostfirst):
raise DeprecatedIDCError, "writelong() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("writelong() deprecated. Use Python file objects instead.")
def readstr(handle):
raise DeprecatedIDCError, "readstr() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("readstr() deprecated. Use Python file objects instead.")
def writestr(handle, s):
raise DeprecatedIDCError, "writestr() deprecated. Use Python file objects instead."
raise DeprecatedIDCError("writestr() deprecated. Use Python file objects instead.")
# ----------------------------------------------------------------------------
# F U N C T I O N S
@@ -4376,11 +4377,11 @@ def next_func_chunk(funcea, tailea):
fci.chunk().end_ea > tailea:
found = True
break
if not fci.next():
if not next(fci):
break
# Return the next chunk, if there is one
if found and fci.next():
if found and next(fci):
return fci.chunk().start_ea
else:
return BADADDR
@@ -5484,7 +5485,7 @@ def send_dbg_command(cmd):
"""
s = eval_idc('send_dbg_command("%s");' % ida_kernwin.str2user(cmd))
if s.startswith("IDC_FAILURE"):
raise Exception, "Debugger command is available only when the debugger is active!"
raise Exception("Debugger command is available only when the debugger is active!")
return s
# wfne flag is combination of the following:
@@ -5773,10 +5774,10 @@ def set_reg_value(value, name):
A register name in the left side of an assignment will do too.
"""
rv = ida_idd.regval_t()
if type(value) == types.StringType:
if type(value) == bytes:
value = int(value, 16)
elif type(value) != types.IntType and type(value) != types.LongType:
print "set_reg_value: value must be integer!"
elif type(value) != int and type(value) != int:
print("set_reg_value: value must be integer!")
return BADADDR
if value < 0:
@@ -6067,7 +6068,7 @@ def get_color(ea, what):
@return: color code in RGB (hex 0xBBGGRR)
"""
if what not in [ CIC_ITEM, CIC_FUNC, CIC_SEGM ]:
raise ValueError, "'what' must be one of CIC_ITEM, CIC_FUNC and CIC_SEGM"
raise ValueError("'what' must be one of CIC_ITEM, CIC_FUNC and CIC_SEGM")
if what == CIC_ITEM:
return ida_nalt.get_item_color(ea)
@@ -6105,7 +6106,7 @@ def set_color(ea, what, color):
@return: success (True or False)
"""
if what not in [ CIC_ITEM, CIC_FUNC, CIC_SEGM ]:
raise ValueError, "'what' must be one of CIC_ITEM, CIC_FUNC and CIC_SEGM"
raise ValueError("'what' must be one of CIC_ITEM, CIC_FUNC and CIC_SEGM")
if what == CIC_ITEM:
return ida_nalt.set_item_color(ea, color)
+4 -3
View File
@@ -11,6 +11,7 @@
# -----------------------------------------------------------------------
# init.py - Essential init routines
# -----------------------------------------------------------------------
from __future__ import print_function
import os
import sys
import time
@@ -22,7 +23,7 @@ lib_dynload = os.path.join(
IDAPYTHON_DYNLOAD_BASE,
"python", "lib", "python2.7", "lib-dynload")
is_x64 = sys.maxint >= 0x100000000L
is_x64 = sys.maxsize >= 0x100000000
if is_x64:
# x64 python requires our lib_dynload to be added; sys.path seems
# to be composed differently than x86 builds.
@@ -42,9 +43,9 @@ try:
import ida_kernwin
import ida_diskio
except ImportError as e:
print "Import failed: %s. Current sys.path:" % str(e)
print("Import failed: %s. Current sys.path:" % str(e))
for p in sys.path:
print "\t%s" % p
print("\t%s" % p)
raise