Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to get rid of those padding byte in list(ih)? #55

Open
jfongattw opened this issue Jun 10, 2021 · 8 comments
Open

Is it possible to get rid of those padding byte in list(ih)? #55

jfongattw opened this issue Jun 10, 2021 · 8 comments

Comments

@jfongattw
Copy link

from intelhex import IntelHex
ih = IntelHex('pimp_my_xor.hex')
ih.minaddr()
256
ihx = ih[0x100:0x103]
ihx.tobinstr()
b'\x89\xc2\x8d'
list(ihx)
[255, 255, 255, 255, 255, 255, 255, ..., 255, 255, 137, 194, 141]

@fernandez85
Copy link
Contributor

this is by design I guess
tobinstr is a function of IntelHex
list is constructor here, which allows iterable object as parameter by default and IntelHex has getItem implemented and it makes iterable
there could be iter (Python 3.x only) and/or next (Python 2.x compatybility) implemented, but I don't know if this creator(/s) intention to do so

neverthelees you could use list(intelhex.tobinarray()) instead

@jfongattw
Copy link
Author

fernandez85, I am totally agree with your point, but can't resist the temptation of doing some experiment on it:-)

After referring to timgeb's answer (On point 6: iter wins) to this question:
https://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable
I added the following method in class IntelHex:

def __iter__(self):
    return iter(self.tobinstr())

Here is the result:

from intelhex import IntelHex
ih = IntelHex('pimp_my_xor.hex')
ih.minaddr()
256
ihx = ih[0x100:0x103]
list(ihx)
[137, 194, 141]
for i in ihx:
... print(i)
...
137
194
141

Not sure if it will cause any side effect:-)

@fernandez85
Copy link
Contributor

fernandez85 commented Jun 10, 2021

I guess not.
tobinarray would be better, instead of tobinstr

And now I see we don't have to deal with backward compatibility for Python 2.x

@bialix
Copy link
Member

bialix commented Jun 10, 2021 via email

@The-42
Copy link
Collaborator

The-42 commented Jun 10, 2021

I don't think list(ih) is necessarily such a bad idea. With #54 something had to happen as suffocating the interpreter wasn't an option (as __getitem__ would always return either padding or an actual byte, list(ih) crashed when running out of memory). Implementing rather than forbidding list(ih) seemed like the better approach to me, so now list conversion already works. If no byte has ever been set, the returned list is empty. Otherwise the list length corresponds to the highest address of the ih object, filled with padding where necessary. Not perfect for all possible usages, but I can see use cases for that.

@bialix
Copy link
Member

bialix commented Jun 11, 2021 via email

@jfongattw
Copy link
Author

Unfortunately, the IntelHex object behaved a little strange at this moment.

len(ihx)
3
ihx._buf
{1: 137, 2: 194, 3: 141}
list(ihx)
[255, 137, 194, 141]

@bialix
Copy link
Member

bialix commented Jun 11, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants