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

Fixes #422. Currently this new test_bug_148 will be failed. Think TDD. #423

Open
wants to merge 4 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
8 changes: 8 additions & 0 deletions tests/decoding_test.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@ECHO OFF
REM https://stackoverflow.com/a/4580120/20785734
setlocal
set PYTHONPATH=%~dp0
python tests/decoding_test.py
endlocal


8 changes: 8 additions & 0 deletions tests/decoding_test2.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@ECHO OFF
REM https://stackoverflow.com/a/4580120/20785734
setlocal
set PYTHONPATH=%~dp0
py -2 tests/decoding_test.py
endlocal


8 changes: 8 additions & 0 deletions tests/decoding_test3.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@ECHO OFF
REM https://stackoverflow.com/a/4580120/20785734
setlocal
set PYTHONPATH=%~dp0
py -3 tests/decoding_test.py
endlocal


14 changes: 11 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@


def test_bug_148():
assert 'a = "\\u0064"\n' == toml.dumps({'a': '\\x64'})
assert 'a = "\\\\x64"\n' == toml.dumps({'a': '\\\\x64'})
assert 'a = "\\\\\\u0064"\n' == toml.dumps({'a': '\\\\\\x64'})
assert r'a = "\\x64"' + '\n' == toml.dumps({'a': r'\x64'})
assert r'a = "\\\\x64"' + '\n' == toml.dumps({'a': r'\\x64'})
assert r'a = "\\\\\\x64"' + '\n' == toml.dumps({'a': r'\\\x64'})


def test_bug_144():
Expand Down Expand Up @@ -232,6 +232,14 @@ def test_commutativity():
assert o == toml.loads(toml.dumps(o))


@pytest.mark.skipif(sys.platform == 'win32', reason = '''This test's expected result
is a hardcoded POSIX file
path. str(pathlib.Path(..))
returns a path formatted
according to the local
platform. So this test
will always fail on
Windows''')
def test_pathlib():
if (3, 4) <= sys.version_info:
import pathlib
Expand Down
43 changes: 24 additions & 19 deletions toml/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,40 @@ def dumps(o, encoder=None):
return retval


_Python_escaped_hex = re.compile(r"""(?<!\\) # not preceded by a backslash
(?P<literal_backslashes>(\\\\)*) # pairs only of literal backslashes
# (in a normal string literal 4 are
# needed to match 1; in a raw string
# literal 4 will match 2 backslashes)
(\\x) # Matches a Python Hex escape prefix in a repr string
# (used for extended-ASCII
# chars, e.g. repr('\xad'))
""",
flags = re.VERBOSE)

def _Python_escaped_hex_to_escaped_toml(m):
return m.group('literal_backslashes') + '\\u00'


def _dump_str(v):
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 = "%r" % v # basically v = repr(v)
if v[0] == 'u': # ditch any Python 2 unicode-literal-string's u prefix
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] + '"')

v = re.sub(_Python_escaped_hex, _Python_escaped_hex_to_escaped_toml, v)

return '"%s"' % v # They're not as popular as double quoted strings, but
# TOML also supports single quoted literal strings.
# However these cannot contain escapes, and the repr
# above might have introduced escapes.


def _dump_float(v):
Expand Down