-
Notifications
You must be signed in to change notification settings - Fork 247
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
reversed() doesn't work on intbv #195
Comments
From the Python documentation:
intbv has the len() and getitem() methods, but no reversed() method, so Python is doing the work for us. I modified my local _intbv.py to show what's going on: else:
i = int(key)
bres = bool((self._val >> i) & 0x1)
print('intbv getitem - index', i, bres)
return bres `>>> from myhdl import *
BTW: IMHO the first sequence is the incorrect one ;) (as it starts with bit 7 and not bit 0) |
It looks like the To make this clear to users, maybe an error should be raised if an |
intbv does have an iter() method: # iterator method
def __iter__(self):
if not self._nrbits:
raise TypeError("Cannot iterate over unsized intbv")
return iter([self[i] for i in range(self._nrbits-1, -1, -1)]) The complete description for reversed() from the Python documentation (I forgot the first line):
I added one more printout to the _intbv.py code, this shows what's going on underneath, and where the error lies:
And as I suspected, the first iteration, which makes use of the supplied iter() method, is incorrect - according to Python rules that you start with element 0 first. |
@josyb thanks for the clarification, guess I should have looked at the source before commenting :) It looks like the reversed order is intentional |
@cfelton It may well have been intentional, but IMO it is wrong. And counter-intuitive. |
@josyb I agree, this should be recorded as a known limitation and a future enhancement will be to implemented As far as deciding which directions the iterations should occur that is a conversation that will need to take place with @jandecaluwe (and others?). Guess you can throw it out on gitter and see what the consensus is. My intention of including the original commit/change was to show that it isn't a bug but a difference in opinion on the expected behavior. The later take more effort to resolve :( I couldn't extract from the comment why the order was changed. Also, I assume that the iterations are not convertible? Is that correct? I believe our informal process is to track enhancements on discourse.myhdl.org and bugs with github. My inclination is to add some documentation on |
@cfelton Yes, let's continue on discourse. |
Hello Devs, I did search on discourse but couldn't find the rest of the discussion there. Can you please direct me to it? |
@tokencolour This discussion has been transferred to discourse yet. |
Trying to Fix myhdl#195. Implementation of __reversed__ method which goes opposite of the direction __iter__ method chooses.
Trying to Fix myhdl#195 The added tests surprisingly fail.
Restarting discussion here. I think a decision should be made on this issue can be closed and the PR assocated can be merged or denied. The problem, I believe, is that the iterator delivers the vector from a sized intbv starting with the MSB down to the LSB. Whereas most people would naturally expect it from LSB to MSB. The additional issue is that the reversed order was the same as a straight iter (though arguably that actually made it correct) This is just my opinion on the topic, I respect that this may not match other peoples: As a Hardware person I would expect it to arrive LSB to MSB and as a python person I would expect an array to start from 0. This
Should be the same as this:
Which currently it is not. There is a comment about if it was changed that it may break exisiting code. I feel very mixed about this, I respect that this may be being used in the field and a fix would break it. Bugs are fixed all the time, and this is a bug! However, I would actually be surprised if this is being used in the field right now if it's current operation is in reverse order, I imagine if someone tried to use it and found the current operation they would have rewritten it to loop on As a reference, apart from the tests that explictly test it in the pytest suite, it does not apppear any where else in the myhdl tests. Is there any recorded reason why it is being done the way it is being done? |
I declared an intbv and I could iterate through the bits in normal order. But if I used reversed() on the intbv to iterate through the bits in the opposite order, I got the same result and no error message.
The text was updated successfully, but these errors were encountered: