Skip to content
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

String dump with correct escaping #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

comodoro
Copy link

This pull request is not complete and there are things that require clarification:

  • It passes tests except for test_bug_148 which I do not understand since it seems to test exactly opposite behavior to the one in the issue

  • It may be easy to add an option to dump to multiline as well as escaping or not escaping all Unicode

When complete should fix #201 and indeed #148, related to #236

Reference used: https://github.com/toml-lang/toml#user-content-string

@roedoejet
Copy link

Any reason why this hasn't been merged already?

@comodoro
Copy link
Author

comodoro commented Mar 1, 2020

As far as I remember, it is not complete for all cases and this PR explores the maintainer's attitude. It was started because of issues that surfaced in the Caster project and was not worked upon because of no response here which resulted in a switch to tomlkit.

@pirate
Copy link

pirate commented Sep 24, 2024

Bump, would be great if this could be revisited/merged, currently you cant dump any strings containing escape sequences without getting an exception:

>>> toml.dumps({
	"reset": "\033[00;00m",
	"lightblue": "\033[01;30m",
	"lightyellow": "\033[01;33m",
	"lightred": "\033[01;35m",
	"red": "\033[01;31m",
	"green": "\033[01;32m",
	"blue": "\033[01;34m",
	"white": "\033[01;37m",
	"black": "\033[01;30m",
})

File ~/test/.venv/lib/python3.11/site-packages/toml/encoder.py:60, in dumps(o, encoder)
     58 if encoder is None:
     59     encoder = TomlEncoder(o.__class__)
---> 60 addtoretval, sections = encoder.dump_sections(o, "")
     61 retval += addtoretval
     62 outer_objs = [id(o)]

File ~/test/.venv/lib/python3.11/site-packages/toml/encoder.py:226, in TomlEncoder.dump_sections(self, o, sup)
    223     else:
    224         if o[section] is not None:
    225             retstr += (qsection + " = " +
--> 226                        unicode(self.dump_value(o[section])) + '\n')
    227 elif self.preserve and isinstance(o[section], InlineTableDict):
    228     retstr += (qsection + " = " +
    229                self.dump_inline_table(o[section]))

File ~/test/.venv/lib/python3.11/site-packages/toml/encoder.py:180, in TomlEncoder.dump_value(self, v)
    178     dump_fn = self.dump_funcs[list]
    179 # Evaluate function (if it exists) else return v
--> 180 return dump_fn(v) if dump_fn is not None else self.dump_funcs[str](v)

File ~/test/.venv/lib/python3.11/site-packages/toml/encoder.py:113, in _dump_str(v)
    111     else:
    112         joiner = "u00"
--> 113     v = [v[0] + joiner + v[1]] + v[2:]
    114 return unicode('"' + v[0] + '"')

IndexError: list index out of range

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect string escaping in TOML encoder
3 participants