Repository navigation
fix: quote strings and keys that would not round-trip, escape backslashes - #2
rahul05ranjan wants to merge 1 commit into
Conversation
…shes
The datafile spec says a string may only be written unquoted if it
cannot be interpreted as a boolean, null, or a number, but the encoder
emitted any word without quotes, whitespace, or brackets verbatim:
'true', '2', '1,2', '#c' and 'a=b' all round-tripped to different
values or failed to re-parse. Object keys were worse: they were
written completely raw, so a ' b' key silently came back as 'b'.
Quoted strings also didn't escape backslashes, so 'a\\b' re-parsed
as 'a\x08b'.
Mirror the grammar's bareword, numword, and punct rules to decide
when a word can safely be written unquoted (with an allow_numwords
switch for the Encoder, matching loads()), quote everything else,
and escape backslashes in encode_quoted_string(). Also fix the
'Invalid key f{obj}' typo in the TypeError raised for unencodable
keys.
Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
|
Urk, I appreciate the effort, but please don't spend any time testing this project. While there are interesting things in pyfloyd, I consider it incomplete and dead and would not encourage anyone to use it. I hope to extract the worthwhile bits and publish them in a different form soon. I should update the README to reflect this state. |
|
Understood, thanks for letting me know — I won't spend further time on pyfloyd, and sorry for the noise. Both PRs are yours to close or keep as you see fit; if you'd rather not have them in the tracker, just close them, no explanation needed. The fixes were genuinely fun to find, for what it's worth — the datafile round-trip quirk made for a nice study in spec-vs-implementation drift. Good luck extracting the worthwhile bits into whatever comes next; if that lands somewhere active, the same inputs (a datafile pretty-printer that re-parses to the same object) will still apply. |
|
I've finally gotten around to looking at the details of the change here. The bugs are real and the idea is good, but the regexps are pretty involved, and I'm not wild about that. I'm not sure how I would implement this differently, though. Given what I wrote earlier about this being a dead project, I'm not going to land this for now, but I'll leave it open in case I change my mind. I'll also definitely carry over the issue into my new project, and think about how I want to handle the issue there, so your work is definitely valuable. Thanks again! |
|
Thanks for the detailed look! Understood on the regexps — that was the tradeoff I wrestled with too. Really glad the analysis carries over to the new project. Happy to rework the approach if you ever change your mind, but no expectations here — cheers! |
dumps()output doesn't always load back to the same object. Four examples, all on currentmain:The spec in
docs/datafiles.mdonly allows a string to be written unquoted if it "cannot be interpreted as a boolean value, null, or a number", but the encoder's bare-word check only looked for whitespace, brackets, and quotes — not for keywords, numbers, or the grammar's other punctuation (,=#/). Object keys were emitted completely raw (_encode_strreturned the key as-is foras_key=True), andencode_quoted_string()never escaped backslashes, so any string containing one re-parsed with escape processing applied a second time.This change:
barewordrule (no punctuation/whitespace, not exactlytrue/false/null, no leading number) in_bare_word_re,_numword_remirror ofnumber+ word characters, used when the newEncoder(allow_numwords=...)flag is set, sodumps('123foo', allow_numwords=True)still emits123fooand round-trips underloads(..., allow_numwords=True)(previously this worked only by accident of the loose regex; the flag was accepted and dropped),encode_string()path as values, so non-bareword keys get quoted, andencode_quoted_string()output.Also fixes the stray
fin theInvalid key f{obj}TypeError raised for unencodable keys (it rendered asInvalid key f(1, 2)).Verification (system Python 3.13.5, in-process):
tests/datafile/encode_test.py:test_str_backslash_is_escaped,test_barewords_that_reparse_as_other_types_are_quoted,test_barewords_with_punctuation_are_quoted,test_keys_that_are_not_barewords_are_quoted, atest_roundtripbattery over 40+ adversarial objects (keys/values with spaces, quotes, backslashes, keywords, number-like and numword strings), and the key-error message. All fail on unfixedmain(6 failures) and pass with this change.loads→dumps→loadscomparingrepr): 106 round-trip drifts onmain, 0 with this change; the only remaining fuzz hits are the rawValueError: could not convert string to floatclass from digit-less exponents like2.5e, which is the separate parse-side crash fixed in fix: report invalid number tokens as DatafileError instead of ValueError #1.python3 -m pytest tests/datafile -k "not Module and not Script and not Tool": 46 passed, 40 deselected (the deselected classes spawnpython -m pyfloyd.datafilesubprocesses that need an installed package rather than aPYTHONPATHcheckout; CI should cover them).