Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improved Python3 compatibility.

Added the option -3 to the test.py script. Now a test can be run explicitly
with the default Python or Python3.
The pycrc code now requires Python2.6 or later.
  • Loading branch information...
commit 6c2bf042b3c03e4f4f4a94a098e20c01c83dc8c7 1 parent 8a0f0be
Thomas Pircher authored
1  .gitignore
View
@@ -1,6 +1,7 @@
.*swp
.svn
*.pyc
+__pycache__
doc/pycrc.1
doc/pycrc.html
test/pycrc_files.tar.gz
2  README
View
@@ -18,7 +18,7 @@ source code generator.
Installation
============
-pycrc requires Python 2.4 or later. Python 3.x is supported.
+pycrc requires Python 2.6 or later. Python 3.x is supported.
This program doesn't need any particular installation. The script can be
called from any directory.
4 crc_symtable.py
View
@@ -1328,7 +1328,7 @@ def __get_table_core_algorithm_ni(self):
loop_core_ni += loop_indent + "tbl_idx = ((crc >> " + shr + ") ^ *data) & $crc_table_mask;" + '\n' + \
loop_indent + "crc = (crc_table[tbl_idx] ^ (crc << " + reg_shift + ")) & $cfg_mask_shifted;" + '\n'
else:
- for i in range (8 / self.opt.TableIdxWidth):
+ for i in range (8 // self.opt.TableIdxWidth):
str_idx = "%s" % (8 - (i + 1) * self.opt.TableIdxWidth)
loop_core_ni += loop_indent + "tbl_idx = (crc >> " + shr + ") ^ (*data >> " + str_idx + ");" + '\n' + \
loop_indent + "crc = crc_table[tbl_idx & $crc_table_mask] ^ (crc << " + reg_shift + ");" + '\n'
@@ -1356,7 +1356,7 @@ def __get_table_core_algorithm_ri(self):
loop_core_ri += loop_indent + "tbl_idx = (" + crc_shifted + " ^ *data) & $crc_table_mask;" + '\n' + \
loop_indent + "crc = (crc_table[tbl_idx] ^ (crc >> $cfg_table_idx_width)) & $cfg_mask_shifted;" + '\n'
else:
- for i in range (8 / self.opt.TableIdxWidth):
+ for i in range (8 // self.opt.TableIdxWidth):
str_idx = "%d" % i
loop_core_ri += loop_indent + "tbl_idx = " + crc_shifted + " ^ (*data >> (" + str_idx + " * $cfg_table_idx_width));" + '\n' + \
loop_indent + "crc = crc_table[tbl_idx & $crc_table_mask] ^ (crc >> $cfg_table_idx_width);" + '\n'
25 pycrc.py
View
@@ -137,12 +137,14 @@ def check_hexstring(opt):
# function crc_file_update
###############################################################################
-def crc_file_update(alg, register, check_str):
+def crc_file_update(alg, register, check_byte_str):
"""
Update the CRC using the bit-by-bit-fast CRC algorithm.
"""
- for i in range(len(check_str)):
- octet = ord(check_str[i])
+ for octet in check_byte_str:
+ if not isinstance(octet, int):
+ # Python 2.x compatibility
+ octet = ord(octet)
if alg.ReflectIn:
octet = alg.reflect(octet, 8)
for j in range(8):
@@ -181,18 +183,11 @@ def check_file(opt):
register = opt.XorIn
else:
register = alg.reflect(opt.XorIn, opt.Width)
- try:
- check_str = in_file.read()
- except IOError:
- sys.stderr.write("%s: error: can't open read %s\n" % (sys.argv[0], opt.CheckFile))
- sys.exit(1)
- while len(check_str):
- register = crc_file_update(alg, register, check_str)
- try:
- check_str = in_file.read()
- except IOError:
- sys.stderr.write("%s: error: can't open read %s\n" % (sys.argv[0], opt.CheckFile))
- sys.exit(1)
+ # Read bytes from the file.
+ check_byte_str = in_file.read()
+ while check_byte_str:
+ register = crc_file_update(alg, register, check_byte_str)
+ check_byte_str = in_file.read()
in_file.close()
if opt.ReflectOut:
4 test/check_files.sh
View
@@ -10,9 +10,9 @@ usage() {
echo >&2 "usage: $0 [OPTIONS]"
echo >&2 ""
echo >&2 "with OPTIONS in"
- echo >&2 " -c check the generated output (default)"
+ echo >&2 " -c check the generated output"
echo >&2 " -g generate the database"
- echo >&2 " -n do cleanup: don't delete the directories with the generated code"
+ echo >&2 " -n no cleanup: don't delete the directories with the generated code"
echo >&2 " -h this help message"
}
14 test/test.py
View
@@ -29,6 +29,7 @@ def __init__(self):
self.CompileMixedArgs = False
self.VariableWidth = False
self.Verbose = False
+ self.Python3 = False
self.Algorithm = copy(self.AllAlgorithms)
# function parse
@@ -44,8 +45,11 @@ def parse(self, argv = None):
model_list = ", ".join(models.getList())
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose",
- action="store_true", dest="verbose", default=False,
+ action="store_true", dest="verbose", default=self.Verbose,
help="print information about the model")
+ parser.add_option("-3", "--python3",
+ action="store_true", dest="python3", default=self.Python3,
+ help="use Python3.x")
parser.add_option("-c", "--compile",
action="store_true", dest="compile", default=self.Compile,
help="test compiled version")
@@ -68,6 +72,7 @@ def parse(self, argv = None):
(options, args) = parser.parse_args(argv)
self.Verbose = options.verbose
+ self.Python3 = options.python3
self.Compile = options.all or options.compile or options.random_parameters
self.RandomParameters = options.all or options.random_parameters
self.CompileMixedArgs = options.all or options.compile_mixed_args
@@ -94,7 +99,7 @@ class CrcTests(object):
# Class constructor
###############################################################################
def __init__(self):
- self.pycrc_bin = "python ../pycrc.py"
+ self.pycrc_bin = "/bin/false"
self.use_algo_bit_by_bit = True
self.use_algo_bit_by_bit_fast = True
self.use_algo_table_driven = True
@@ -531,6 +536,11 @@ def run(self, opt):
self.use_algo_table_driven = "table-driven" in opt.Algorithm
self.verbose = opt.Verbose
+ if opt.Python3:
+ self.pycrc_bin = "python3 ../pycrc.py"
+ else:
+ self.pycrc_bin = "python ../pycrc.py"
+
if not self.__setup_files(opt):
return False
Please sign in to comment.
Something went wrong with that request. Please try again.