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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions toml-test
Submodule toml-test added at f910e1
67 changes: 35 additions & 32 deletions toml/encoder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import functools
import re
import sys
from decimal import Decimal
Expand Down Expand Up @@ -70,37 +71,38 @@ def dumps(o, encoder=None):
sections = newsections
return retval


def _dump_str(v):
def _dump_str(v, escape_unicode=True, multiline=False):
if sys.version_info < (3,) and hasattr(v, 'decode') and isinstance(v, str):
v = v.decode('utf-8')
v = "%r" % v
if v[0] == 'u':
v = v[1:]
singlequote = v.startswith("'")
if singlequote or v.startswith('"'):
v = v[1:-1]
if singlequote:
v = v.replace("\\'", "'")
v = v.replace('"', '\\"')
v = v.split("\\x")
while len(v) > 1:
i = -1
if not v[0]:
v = v[1:]
v[0] = v[0].replace("\\\\", "\\")
# No, I don't know why != works and == breaks
joinx = v[0][i] != "\\"
while v[0][:i] and v[0][i] == "\\":
joinx = not joinx
i -= 1
if joinx:
joiner = "x"
else:
joiner = "u00"
v = [v[0] + joiner + v[1]] + v[2:]
return unicode('"' + v[0] + '"')

else:
v = unicode(v)
out = ''
quote = '"""' if len(v.splitlines()) > 1 and multiline else '"'
for line in v.splitlines():
for char in line:
c = ord(char)
if (escape_unicode and c > 127) or c <= 0x1f or c == 0x7f:
h = hex(c)[2:]
if len(h) < 2:
h = '0' + h
out += '\\x'
if c > 255 and len(h) < 4:
h = '0' * (4 - len(h)) + h
out += '\\u'
if c > 65536 and len(h) < 8:
h = '0' * (8 - len(h)) + h
out += '\\U'
out += h
else:
if char == '\\' or (char == '"' and quote != '"""'):
out += '\\'
out += char
out += '\n'
if quote == '"""':
out = '\n' + out
else:
out = out[:-1]
return unicode('%s%s%s' % (quote, out, quote))

def _dump_float(v):
return "{0:.16}".format(v).replace("e+0", "e+").replace("e-0", "e-")
Expand All @@ -116,12 +118,13 @@ def _dump_time(v):

class TomlEncoder(object):

def __init__(self, _dict=dict, preserve=False):
def __init__(self, _dict=dict, preserve=False, escape_unicode=True, multiline=False):
self._dict = _dict
self.preserve = preserve
dump_str = functools.partial(_dump_str, escape_unicode=escape_unicode, multiline=multiline)
self.dump_funcs = {
str: _dump_str,
unicode: _dump_str,
str: dump_str,
unicode: dump_str,
list: self.dump_list,
bool: lambda v: unicode(v).lower(),
int: lambda v: v,
Expand Down