[PATCH evemu 02/11] py: Replace print statement with function call

Peter Hutterer peter.hutterer at who-t.net
Fri Jan 3 20:14:38 PST 2014


On 4/01/2014 01:27 , Daniel Martin wrote:
> On Fri, Jan 03, 2014 at 10:24:37AM +1000, Peter Hutterer wrote:
>> On Thu, Jan 02, 2014 at 10:47:52PM +0100, Daniel Martin wrote:
>>> As of Python 3 the print statement has been replaced by a function and
>>> Python 3 has no backward compatibility for it (like Python 2.7), causing
>>> syntax errors on such statements.
>>>
>>> Replace all print statements with the function call and bump the Python
>>> package requirement to version 2.7. Because, these changes make the
>>> scripts incompatible with Python 2.6.
>>>
>>> Signed-off-by: Daniel Martin <consume.noise at gmail.com>
>>> ---
>>>   configure.ac                    |  2 +-
>>>   src/convert-old-dumps-to-1.1.py | 18 ++++----
>>>   src/make-event-names.py         | 94 ++++++++++++++++++++---------------------
>>>   3 files changed, 57 insertions(+), 57 deletions(-)
>>>
>>> diff --git a/configure.ac b/configure.ac
>>> index d9f434f..0bd51a9 100644
>>> --- a/configure.ac
>>> +++ b/configure.ac
>>> @@ -31,7 +31,7 @@ AC_PROG_LIBTOOL
>>>   AC_PROG_CC
>>>   AC_PROG_CXX
>>>   AC_PROG_INSTALL
>>> -AM_PATH_PYTHON([2.6])
>>> +AM_PATH_PYTHON([2.7])
>>
>> I'd rather keep the min requirement on 2.6 for RHEL6 compatibility and use
>> from __future__ import print_function.
>
> Just installed 2.6(.8) to test this and it works even without the
> __future__ import. print is already a function:

the from __future__ import statement removes print as a language 
statement. so it simply prevents us from accidentally introducing a 
python3-invalid print statement.

>
>      >>> help("__builtin__.print")
>      Help on built-in function print in __builtin__:
>
>      __builtin__.print = print(...)
>          print(value, ..., sep=' ', end='\n', file=sys.stdout)
>      ---8<---
>
> I've digged through the NEWS file, but couldn't find any hint since when
> this is the case. Anyone else knows some details?

found it: http://docs.python.org/2/library/functions.html#print says 
available since 2.6.

btw, not sure how you did the conversion but the 2to3 script may uncover 
a few more things.

Cheers,
   Peter

>
>>>   PKG_CHECK_MODULES([LIBEVDEV], [libevdev >= 0.5])
>>>
>>> diff --git a/src/convert-old-dumps-to-1.1.py b/src/convert-old-dumps-to-1.1.py
>>> index 8eaabfe..2eb12e9 100755
>>> --- a/src/convert-old-dumps-to-1.1.py
>>> +++ b/src/convert-old-dumps-to-1.1.py
>>> @@ -22,20 +22,20 @@ def convert_events(lines):
>>>   			type = int(type, 16)
>>>   			code = int(code, 16)
>>>   			value = int(value, 0)
>>> -			print "E: %s %04x %04x %04d\t" % (t, type, code, value),
>>> +			print("E: %s %04x %04x %04d\t" % (t, type, code, value))
>>>   			desc = ""
>>>   			if type == ev_map["EV_SYN"]:
>>>   				if code == syn_map["SYN_MT_REPORT"]:
>>> -					print "# ++++++++++++ %s (%d) ++++++++++" % (event_get_code_name(type, code), value)
>>> +					print("# ++++++++++++ %s (%d) ++++++++++" % (event_get_code_name(type, code), value))
>>>   				else:
>>> -					print "# ------------ %s (%d) ----------" % (event_get_code_name(type, code), value)
>>> +					print("# ------------ %s (%d) ----------" % (event_get_code_name(type, code), value))
>>>   			else:
>>> -				print "# %s / %-20s %d" % ( event_get_type_name(type), event_get_code_name(type, code), value)
>>> +				print("# %s / %-20s %d" % ( event_get_type_name(type), event_get_code_name(type, code), value))
>>>   		else:
>>> -			print line,
>>> +			print(line)
>>>
>>>   def usage(args):
>>> -	print "%s mydev.desc [mydev.events]" % os.path.basename(args[0])
>>> +	print("%s mydev.desc [mydev.events]" % os.path.basename(args[0]))
>>>   	return 1
>>>
>>>
>>> @@ -47,8 +47,8 @@ if __name__ == "__main__":
>>>   	d.describe(sys.stdout)
>>>   	d = None
>>>   	if len(sys.argv) > 2:
>>> -		print "################################"
>>> -		print "#      Waiting for events      #"
>>> -		print "################################"
>>> +		print("################################")
>>> +		print("#      Waiting for events      #")
>>> +		print("################################")
>>>   		with open(sys.argv[2]) as f:
>>>   			convert_events(f.readlines())
>>> diff --git a/src/make-event-names.py b/src/make-event-names.py
>>> index 2092cfd..6581944 100755
>>> --- a/src/make-event-names.py
>>> +++ b/src/make-event-names.py
>>> @@ -42,57 +42,57 @@ blacklist = [
>>>   def print_bits(bits, prefix):
>>>   	if  not hasattr(bits, prefix):
>>>   		return
>>> -	print "static const char * const %s_map[%s_MAX + 1] = {" % (prefix, prefix.upper())
>>> -	print "	[0 ... %s_MAX] = NULL," % prefix.upper()
>>> +	print("static const char * const %s_map[%s_MAX + 1] = {" % (prefix, prefix.upper()))
>>> +	print("	[0 ... %s_MAX] = NULL," % prefix.upper())
>>>   	for val, name in getattr(bits, prefix).items():
>>> -		print "	[%s] = \"%s\"," % (name, name)
>>> -	print "};"
>>> -	print ""
>>> +		print("	[%s] = \"%s\"," % (name, name))
>>> +	print("};")
>>> +	print("")
>>>
>>>   def print_python_bits(bits, prefix):
>>>   	if  not hasattr(bits, prefix):
>>>   		return
>>>
>>> -	print "%s_map = {" % (prefix)
>>> +	print("%s_map = {" % (prefix))
>>>   	for val, name in getattr(bits, prefix).items():
>>> -		print "	%d : \"%s\"," % (val, name)
>>> -	print "}"
>>> -	print "for k, v in %s_map.items():" % (prefix)
>>> -	print "	%s_map[v] = k" % (prefix)
>>> -	print ""
>>> +		print("	%d : \"%s\"," % (val, name))
>>> +	print("}")
>>> +	print("for k, v in %s_map.items():" % (prefix))
>>> +	print("	%s_map[v] = k" % (prefix))
>>> +	print("")
>>>
>>>   def print_map(bits):
>>> -	print "static const char * const * const map[EV_MAX + 1] = {"
>>> -	print "	[0 ... EV_MAX] = NULL,"
>>> +	print("static const char * const * const map[EV_MAX + 1] = {")
>>> +	print("	[0 ... EV_MAX] = NULL,")
>>>
>>>   	for prefix in prefixes:
>>>   		if prefix == "BTN_" or prefix == "EV_" or prefix == "INPUT_PROP_":
>>>   			continue
>>> -		print "	[EV_%s] = %s_map," % (prefix[:-1], prefix[:-1].lower())
>>> +		print("	[EV_%s] = %s_map," % (prefix[:-1], prefix[:-1].lower()))
>>>
>>> -	print "};"
>>> -	print ""
>>> +	print("};")
>>> +	print("")
>>>
>>>   def print_python_map(bits):
>>> -	print "map = {"
>>> +	print("map = {")
>>>
>>>   	for val, name in getattr(bits, "ev").items():
>>>   		name = name[3:]
>>>   		if name == "REP" or name == "PWR"  or name == "FF_STATUS"  or name == "MAX":
>>>   			continue
>>> -		print "	%d : %s_map," % (val, name.lower())
>>> +		print("	%d : %s_map," % (val, name.lower()))
>>>
>>> -	print "}"
>>> -	print ""
>>> +	print("}")
>>> +	print("")
>>>
>>>   def print_mapping_table(bits):
>>> -	print "/* THIS FILE IS GENERATED, DO NOT EDIT */"
>>> -	print ""
>>> -	print "#ifndef EVENT_NAMES_H"
>>> -	print "#define EVENT_NAMES_H"
>>> -	print ""
>>> -	print "#define SYN_MAX 3 /* linux/input.h doesn't define that */"
>>> -	print ""
>>> +	print("/* THIS FILE IS GENERATED, DO NOT EDIT */")
>>> +	print("")
>>> +	print("#ifndef EVENT_NAMES_H")
>>> +	print("#define EVENT_NAMES_H")
>>> +	print("")
>>> +	print("#define SYN_MAX 3 /* linux/input.h doesn't define that */")
>>> +	print("")
>>>
>>>   	for prefix in prefixes:
>>>   		if prefix == "BTN_":
>>> @@ -101,19 +101,19 @@ def print_mapping_table(bits):
>>>
>>>   	print_map(bits)
>>>
>>> -	print "static const char * event_get_type_name(int type) {"
>>> -	print "	return ev_map[type];"
>>> -	print " }"
>>> -	print ""
>>> -	print "static const char * event_get_code_name(int type, int code) {"
>>> -	print "	return map[type] ? map[type][code] : NULL;"
>>> -	print "}"
>>> -	print ""
>>> -	print "#endif /* EVENT_NAMES_H */"
>>> +	print("static const char * event_get_type_name(int type) {")
>>> +	print("	return ev_map[type];")
>>> +	print(" }")
>>> +	print("")
>>> +	print("static const char * event_get_code_name(int type, int code) {")
>>> +	print("	return map[type] ? map[type][code] : NULL;")
>>> +	print("}")
>>> +	print("")
>>> +	print("#endif /* EVENT_NAMES_H */")
>>>
>>>   def print_python_mapping_table(bits):
>>> -	print "# THIS FILE IS GENERATED, DO NOT EDIT"
>>> -	print ""
>>> +	print("# THIS FILE IS GENERATED, DO NOT EDIT")
>>> +	print("")
>>>
>>>   	for prefix in prefixes:
>>>   		if prefix == "BTN_":
>>> @@ -122,15 +122,15 @@ def print_python_mapping_table(bits):
>>>
>>>   	print_python_map(bits)
>>>
>>> -	print "def event_get_type_name(type):"
>>> -	print "	return ev_map[type]"
>>> -	print ""
>>> -	print ""
>>> -	print "def event_get_code_name(type, code):"
>>> -	print "	if map.has_key(type) and map[type].has_key(code):"
>>> -	print "		return map[type][code]"
>>> -	print "	return 'UNKNOWN'"
>>> -	print ""
>>> +	print("def event_get_type_name(type):")
>>> +	print("	return ev_map[type]")
>>> +	print("")
>>> +	print("")
>>> +	print("def event_get_code_name(type, code):")
>>> +	print("	if map.has_key(type) and map[type].has_key(code):")
>>> +	print("		return map[type][code]")
>>> +	print("	return 'UNKNOWN'")
>>> +	print("")
>>>
>>>   def parse_define(bits, line):
>>>   	m = re.match(r"^#define\s+(\w+)\s+(\w+)", line)
>>> --
>>> 1.8.5.2
>>>
>>> _______________________________________________
>>> Input-tools mailing list
>>> Input-tools at lists.freedesktop.org
>>> http://lists.freedesktop.org/mailman/listinfo/input-tools



More information about the Input-tools mailing list