|
A thought: should this be aligned with the C tokenizer? If so, we can add tests to compare python tokenizer and the internal C tokenizer. |
Sorry, something went wrong.
|
It should be aligned with the c tokenizer, but there are some tokens that differ. For example the c tokenizer returns lbrace and rbrace ({ and }) tokens while the python one just returns an OP token. Matching tests sound a good idea to make sure they are both aligned. I can add it :). |
Sorry, something went wrong.
|
Not sure about matching tests. There are many and often very slight differences between the implementations of the C tokenizer and the Python tokenize module and that's something we've been okay with for a long time. I wonder whether writing those tests is unnecessary effort. |
Sorry, something went wrong.
There was a problem hiding this comment.
Thanks @mgmacias95 for working on this! I just had a first look at it and it looks great in general.
However, I think that the regex for matching f-strings is going to fail for nested strings that use the same quote. Since this was something that has been explicitly allowed in the PEP and also somewhat "advertised", I feel that most people would expect the tokenize module to support that as well, especially since we're putting in the work to support the new tokens.
Am I missing something? Is that maybe handled a different way? How do others feel about not supporting nested strings with the same quotation?
Sorry, something went wrong.
|
Thanks @mgmacias95 for working on this! I just had a first look at it and it looks great in general. However, I think that the regex for matching f-strings is going to fail for nested strings that use the same quote. Since this was something that has been explicitly allowed in the PEP and also somewhat "advertised", I feel that most people would expect the tokenize module to support that as well, especially since we're putting in the work to support the new tokens. Am I missing something? Is that maybe handled a different way? How do others feel about not supporting nested strings with the same quotation? Sorry for the lack of context, let me explain the plan:
Not sure about matching tests. I don't think this is possible as both tokenizers are incompatible. One of them emits tokens that the other does not and they also emit different tokens (the Python tokenizer emits OP for operators and the C tokenizer emits generic tokens). Also, there are tokens that are not emitted in the C tokenizer (such as encoding). We would loose more time trying to match them and find differences than just treating them separately. What do you think? |
Sorry, something went wrong.
It makes sense We would lose more time trying to match them and find differences than just treating them separately. I see the point here. I agree |
Sorry, something went wrong.
|
The plan makes sense! Thanks for the thorough explanation @pablogsal! |
Sorry, something went wrong.
|
Ok, we are switching directions. Turns out the handling I described was even more difficult because we need to also factor in code that knows how to handle the scaped {{ and }} and this would be even more difficult if we also need to parse these to identify the entire f-string code (with nested quotes) to post-process it later. We did some prototypes and it was quite verbose and unmaintainable and that's on top of the current tokenizer. So we had an idea: what if we could reuse the c tokenizer as it already knows how to handle this?. The problem as I said before is that the C tokenizer emits tokens in a different fashion and it doesn't even bother with some others (like COMMENT or NL tokens). So what we have decided is to teach the C tokenizer to optionally emit these tokens and adapt it so we can mimic the output of the Python tokenizer as much as possible. There are some very minor things that the Python tokenizer does that are quite odd (like emitting DEDENT tokens with line numbers that do not exist at the end) that we have decided not to replicate but otherwise this is a HUGE win because:
|
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit f1a5090 🤖 If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
Sorry, something went wrong.
|
Quick question @pablogsal: Do we still maintain the untokenize(tokenize($src)) == $src guarantee with this switch (with the alternative mode enabled in the C tokenizer)? Stuff like unnecessary DEDENTs might be relevant to the pretty code generation phase here although I am not super sure (just a hunch): Lines 204 to 207 in d78c3bc |
Sorry, something went wrong.
|
Quick question @pablogsal: Do we still maintain the untokenize(tokenize($src)) == $src guarantee with this switch (with the alternative mode enabled in the C tokenizer)? I think we stil do (the test pass and we spent a huge amount of time making sure they do with all the files, which was non-trivial). I may still be missing something though. |
Sorry, something went wrong.
| self.assertEqual(tokens, expected_tokens, | ||
| "bytes not decoded with encoding") | ||
|
|
||
| def test__tokenize_does_not_decode_with_encoding_none(self): |
There was a problem hiding this comment.
This is being removed because it was testing the _tokenize implementation that doesn't exist anymore and is not public
Sorry, something went wrong.
|
🤖 New build scheduled with the buildbot fleet by @pablogsal for commit 7fb58b0 🤖 If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again. |
Sorry, something went wrong.
|
what if we could reuse the c tokenizer as it already knows how to handle this? +1 for this. Just like I have mentioned here pablogsal#67 (comment) |
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM! Great job! 🚀
Sorry, something went wrong.
|
Looks like this change broke IDLE: |
Sorry, something went wrong.
|
It's possible that the tokenizer changes here introduced some parsing-related test failures in Sphinx: sphinx-doc/sphinx#11436 - we've begun looking into the cause. (it may turn out to be a problem to resolve on the Sphinx side; I'm not yet sure whether it suggests a bug in cPython, but thought it'd be worthwhile to mention) |
Sorry, something went wrong.
|
Would it be possible to give us a self-contained reproducer? With that we may be able to indicate if this is an expected change or a bug. |
Sorry, something went wrong.
|
Also, could you please open an issue for this once you have your reproducer? |
Sorry, something went wrong.
|
Yep, sure thing @pablogsal (on both counts: a repro case and a bugreport to go along with it). I'll send those along when available (or will similarly confirm if it turns out to be a non-issue). |
Sorry, something went wrong.
|
Thanks to @mgmacias95's explanation, I believe that the updated tokenizer is working as-expected. There is some code in Sphinx to handle what I'd consider a quirk of the previous tokenizer, and that code isn't compatible with the updated (improved, I think) representation of dedent tokens. (apologies for the distraction, and thank you for the help) |
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.