Skip to content

Commit

Permalink
fix(espefuse): Fix compatibility with Bitstring>=4
Browse files Browse the repository at this point in the history
bitstring==4 dropped class alias for BitStream. BitString was just an
alias in all supported bitstring versions:
https://github.com/scott-griffiths/bitstring/blob/bitstring-3.1.6/bitstring.py#L4247

Closes #797
  • Loading branch information
dobairoland committed Nov 22, 2022
1 parent c7ca87b commit ee27a64
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
12 changes: 6 additions & 6 deletions espefuse/efuse/base_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import re
import sys

from bitstring import BitArray, BitString, CreationError
from bitstring import BitArray, BitStream, CreationError

import esptool

Expand Down Expand Up @@ -155,14 +155,14 @@ def __init__(self, parent, param, skip_read=False):
self.len = param.len
self.key_purpose_name = param.key_purpose
bit_block_len = self.get_block_len() * 8
self.bitarray = BitString(bit_block_len)
self.bitarray = BitStream(bit_block_len)
self.bitarray.set(0)
self.wr_bitarray = BitString(bit_block_len)
self.wr_bitarray = BitStream(bit_block_len)
self.wr_bitarray.set(0)
self.fail = False
self.num_errors = 0
if self.id == 0:
self.err_bitarray = BitString(bit_block_len)
self.err_bitarray = BitStream(bit_block_len)
self.err_bitarray.set(0)
else:
self.err_bitarray = None
Expand Down Expand Up @@ -336,7 +336,7 @@ def save(self, new_data):
# in bitstring = [N] ... [2][1][0] (to get a correct bitstring
# need to reverse new_data)
# *[x] - means a byte.
data = BitString(bytes=new_data[::-1], length=len(new_data) * 8)
data = BitStream(bytes=new_data[::-1], length=len(new_data) * 8)
if self.parent.debug:
print(
"\twritten : {} ->\n\tto write: {}".format(self.get_bitstring(), data)
Expand Down Expand Up @@ -563,7 +563,7 @@ def __init__(self, parent, param):
field_len = int(re.search(r"\d+", self.efuse_type).group())
if self.efuse_type.startswith("bytes"):
field_len *= 8
self.bitarray = BitString(field_len)
self.bitarray = BitStream(field_len)
self.bit_len = field_len
self.bitarray.set(0)
self.update(self.parent.blocks[self.block].bitarray)
Expand Down
4 changes: 2 additions & 2 deletions espefuse/efuse/base_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import json
import sys

from bitstring import BitString
from bitstring import BitStream

import esptool

Expand Down Expand Up @@ -627,7 +627,7 @@ def burn_bit(esp, efuses, args):
efuses.force_write_always = args.force_write_always
num_block = efuses.get_index_block_by_name(args.block)
block = efuses.blocks[num_block]
data_block = BitString(block.get_block_len() * 8)
data_block = BitStream(block.get_block_len() * 8)
data_block.set(0)
try:
data_block.set(True, args.bit_number)
Expand Down
17 changes: 9 additions & 8 deletions espefuse/efuse/emulate_efuse_controller_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import re

from bitstring import BitString
from bitstring import BitStream


class EmulateEfuseControllerBase(object):
Expand All @@ -26,18 +26,19 @@ def __init__(self, efuse_file=None, debug=False):
self.efuse_file = efuse_file
if self.efuse_file:
try:
self.mem = BitString(
open(self.efuse_file, "a+b"), length=self.REGS.EFUSE_MEM_SIZE * 8
self.mem = BitStream(
bytes=open(self.efuse_file, "rb").read(),
length=self.REGS.EFUSE_MEM_SIZE * 8,
)
except ValueError:
except (ValueError, FileNotFoundError):
# the file is empty or does not fit the length.
self.mem = BitString(length=self.REGS.EFUSE_MEM_SIZE * 8)
self.mem = BitStream(length=self.REGS.EFUSE_MEM_SIZE * 8)
self.mem.set(0)
self.mem.tofile(open(self.efuse_file, "a+b"))
else:
# efuse_file is not provided
# it means we do not want to keep the result of efuse operations
self.mem = BitString(self.REGS.EFUSE_MEM_SIZE * 8)
self.mem = BitStream(self.REGS.EFUSE_MEM_SIZE * 8)
self.mem.set(0)

""" esptool method start >> """
Expand Down Expand Up @@ -159,7 +160,7 @@ def check_wr_protection_area(self, num_blk, wr_data):
# checks fields which have the write protection bit.
# if the write protection bit is set, we need to protect that area from changes.
write_disable_bit = self.read_field("WR_DIS", bitstring=False)
mask_wr_data = BitString(len(wr_data))
mask_wr_data = BitStream(len(wr_data))
mask_wr_data.set(0)
blk = self.Blocks.get(self.Blocks.BLOCKS[num_blk])
if blk.write_disable_bit is not None and write_disable_bit & (
Expand Down Expand Up @@ -206,7 +207,7 @@ def check_rd_protection_area(self):
block.pos = block.length - (
field.word * 32 + field.pos + raw_data.length
)
block.overwrite(BitString(raw_data.length))
block.overwrite(BitStream(raw_data.length))
self.overwrite_mem_from_block(blk, block)

def clean_mem(self):
Expand Down
4 changes: 2 additions & 2 deletions espefuse/efuse/esp32c2/emulate_efuse_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later

from bitstring import BitString
from bitstring import BitStream

import reedsolo

Expand Down Expand Up @@ -138,5 +138,5 @@ def get_read_disable_mask(blk):
block.pos = block.length - (
field.word * 32 + field.pos + raw_data.length
)
block.overwrite(BitString(raw_data.length))
block.overwrite(BitStream(raw_data.length))
self.overwrite_mem_from_block(blk, block)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def find_version(*file_paths):
],
},
install_requires=[
"bitstring>=3.1.6,<4",
"bitstring>=3.1.6",
"cryptography>=2.1.4",
"ecdsa>=0.16.0",
"pyserial>=3.0",
Expand Down
4 changes: 2 additions & 2 deletions test/test_espefuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import tempfile
import time

from bitstring import BitString
from bitstring import BitStream

# Make command line options --port, --reset-port and --chip available
from conftest import arg_chip, arg_port, arg_reset_port
Expand Down Expand Up @@ -115,7 +115,7 @@ def check_data_block_in_log(
self, log, file_path, repeat=1, reverse_order=False, offset=0
):
with open(file_path, "rb") as f:
data = BitString("0x00") * offset + BitString(f)
data = BitStream("0x00") * offset + BitStream(f)
blk = data.readlist(f"{data.len // 8}*uint:8")
blk = blk[::-1] if reverse_order else blk
hex_blk = " ".join(f"{num:02x}" for num in blk)
Expand Down

0 comments on commit ee27a64

Please sign in to comment.