GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-145968: fix base64.b64decode altchars translation in specific cases by mayeut · Pull Request #145969 · python/cpython Conversation
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| altchars_out = ( | ||
| altchars[0] if altchars[0] not in b'+/' else altchars[1], | ||
| altchars[1] if altchars[1] not in b'+/' else altchars[0], | ||
| ) | ||
| trans_in = bytearray(altchars) | ||
| trans_out = bytearray(b'+/') | ||
| for b, b_out in zip(b'+/', altchars_out): | ||
| if b not in altchars: | ||
| trans_in.append(b) | ||
| trans_out.append(b_out) | ||
| trans = bytes.maketrans(trans_in, trans_out) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It can be simply:
| altchars_out = ( | |
| altchars[0] if altchars[0] not in b'+/' else altchars[1], | |
| altchars[1] if altchars[1] not in b'+/' else altchars[0], | |
| ) | |
| trans_in = bytearray(altchars) | |
| trans_out = bytearray(b'+/') | |
| for b, b_out in zip(b'+/', altchars_out): | |
| if b not in altchars: | |
| trans_in.append(b) | |
| trans_out.append(b_out) | |
| trans = bytes.maketrans(trans_in, trans_out) | |
| trans = bytes.maketrans(altchars + bytes(set(b'+/') - set(altchars)), | |
| b'+/' + bytes(set(altchars) - set(b'+/'))) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I commited the suggestion but after further reflection, the sets are unordered and thus the translation might not be correct when altchars and standard ones do not overlap (although this might be hard to catch in tests ?).
I'll fix that later
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You are right. What about the following?
trans_in = altchars trans_out = b'+/' if b'+' not in altchars and b'-' not in altchars: trans_in += b'+/' trans_out += altchars elif b'+' not in altchars or b'-' not in altchars: trans_in += bytes(set(b'+/') - set(altchars)) trans_out += bytes(set(altchars) - set(b'+/'))
You can also return your original code. #145981 also fixes this issue, so this code can be replaced, but your tests will remain.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Co-authored-by: Serhiy Storchaka <[email protected]>
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
LGTM. 👍
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…c cases (pythonGH-145969) When altchars overlaps with the standard ones, the translation does not always yield to the expected outcome.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.