bpo-33578: Add getstate/setstate for CJK codec#6984
Conversation
This comment has been minimized.
This comment has been minimized.
|
(Added GitHub name to bugs.python.org. CLA is signed already) |
There was a problem hiding this comment.
Is it possible to refactor all error paths under one "error" label section and substitute these with gotosas indicated here so there is only one exit point from the function?
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
8b5ee96 to
db1a055
Compare
|
Thanks for the review @pablogsal, I've made the changes. Refactoring to use an error label helped catch a missing decref 👍 |
db1a055 to
6f9869d
Compare
There was a problem hiding this comment.
This test is leaking a file descriptor:
ResourceWarning: unclosed file <_io.TextIOWrapper name='@test_22392_tmp' mode='r' encoding='jisx0213'>
There was a problem hiding this comment.
Ah, missed the last .close, thanks.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
6f9869d to
0af0cc7
Compare
There was a problem hiding this comment.
Please use "euc_jis_2004" instead of "jisx0213" alias.
"JIS X 0213:2004" defines charset. There are three encoding for it; "ISO-2022-JP-2004", "EUC-JIS-2004", and "Shift-JIS 2004".
Additionally, there is an old version of it; "JIS X 0213:2000".
I don't know why "jisx0213" alias for "euc_jis_2004" was added, but it seems not predictable alias.
There was a problem hiding this comment.
Thanks, I've changed it to use euc_jis_2004. Also replaced euc-jp with euc_jp.
0af0cc7 to
44727e7
Compare
be4dc14 to
3c18d61
Compare
|
Need to fix the following warnings: and Travis has picked up a checksum mismatch: Update: done |
3c18d61 to
f60f50c
Compare
There was a problem hiding this comment.
You can use S for better error message..
https://docs.python.org/3/library/codecs.html#codecs.IncrementalDecoder.getstate
There was a problem hiding this comment.
PyArg_ParseTuple will raise TypeError. So this check may be redundant.
There was a problem hiding this comment.
PyArg_ParseTuple is enough but the error message is not very user friendly:
>>> import codecs
>>> decoder = codecs.getincrementaldecoder('euc_jp')()
>>> decoder.setstate(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: new style getargs format but argument is not a tuple
There was a problem hiding this comment.
Then, you can use ArgumentClinic again. object(subclass_of='&PyTuple_Type').
There was a problem hiding this comment.
Thanks, that works nicely :)
>>> decoder.setstate(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: setstate() argument must be tuple, not int
There was a problem hiding this comment.
Split } and else { in two lines.
There was a problem hiding this comment.
You need to check buffersize <= MAXDECPENDING
There was a problem hiding this comment.
And test cases for possible wrong inputs are needed too.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
There was a problem hiding this comment.
There is make_tuple() in this file. It can be used instead.
There was a problem hiding this comment.
This struct is used from only two place in single source file.
Move this to the file.
c97199f to
edf0b4e
Compare
|
Changes to address feedback from @doerwalter have been pushed.
|
There was a problem hiding this comment.
Struct layout is different by compiler.
To build portable integer, you need to use just bytearray:
unsigned char state[1 + MAXPENDING*4 + 8];
...
state[0] = peindingsize;
memcpy(state+1, pendingbuffer, pendingsize);
int pos = 1 + pendingsize;
memcpy(state+pos, self->state, 8);
There was a problem hiding this comment.
Thanks, @methane. state should now be independent of x32/64, endianness, and compiler.
Latest commit changes:
- Replace
MultibyteIncrementalEncoderStatewith byte array - Update
test_multibytecodecto match new byte array format (difference:pendingsizemoved to the first byte + onlypendingsizebytes stored in buffer position)
There was a problem hiding this comment.
Also added test_getstate_returns_expected_value to test_multibytecodec so that the consistency of the state byte array structure is validated by the test suite.
e43fb6f to
b36149f
Compare
|
@methane: you approved the change, but you didn't merge it. Do you want another core dev to approve the change, or did you just forget to merge it? |
|
@doerwalter Would you review this again before merging this? |
|
|
||
| #define STATE_OFFSET 0 | ||
|
|
||
| /* |
There was a problem hiding this comment.
What's the meaning of this #define?
There was a problem hiding this comment.
I felt c[STATE_OFFSET] was more explicit than c[0], but don't mind inlining it if you prefer. Alternatively, STATE_POSITION may be a better name, as I wanted to get across that only byte 0 is relevant in state->c for the CN codec.
Perhaps even CN_STATE_POSITION?
There was a problem hiding this comment.
OK, I get it: c[8] is the merged version of the previous union, so it serves two purposes: it's a character array and a (very) small integer. IMHO this (and how it's used) should be documented where MultibyteCodec_State is defined.
There was a problem hiding this comment.
If I were to document how I see MultibyteCodec_State now, I would say something along the lines of:
A union that provides 8 bytes of state for multibyte codecs. Codecs
are free to use this how they want. Note: if you need to add a new
member to this union, ensure that its byte order is independent of CPU
endianness so that the return value of `getstate` doesn't differ
between little and big endian CPUs.
Should I add this as a comment next to the union?
There was a problem hiding this comment.
Sounds good to me. However MultibyteCodec_State no longer is a union, "if you need to add a new member to this union" should be phrased differently.
And STATE_OFFSET would have a comment like: "Codecs that have to store a small integer in MultibyteCodec_State store it at the following offset".
And yes, multibytecodec.h is a little light on documentation, but that's probably another patch.
There was a problem hiding this comment.
I've added comments next to STATE_OFFSET (now CN_STATE_OFFSET for clarity) and MultibyteCodec_State. Please let me know if the explanations make sense.
There was a problem hiding this comment.
Looks good to me, but I'd like @methane to make the final decision about merging this PR.
b36149f to
4aea0e1
Compare
|
|
|
|
I'm looking into the buildbot failures to see if they're related.
If I understand correctly, the s390x systems are big endian, so there is probably a problem in the handling of endianness. I will look into emulating a big endian machine locally in a few hours from now to debug this. Update: I managed to recreate locally and the problem seems to be in |
|
I've opened a PR (#10290) to address the issues found by the big-endian build bots. |
This implements getstate and setstate for the cjkcodecs multibyte incremental encoders/decoders, primarily to fix issues with seek/tell.
The encoder getstate/setstate is slightly tricky as the "state" is pending bytes + MultibyteCodec_State but only an integer can be returned. The approach I've taken is to encode this data into a long, similar to how .tell() encodes a "cookie_type" as a long.
https://bugs.python.org/issue33578