toml icon indicating copy to clipboard operation
toml copied to clipboard

more and more backslashes when repeatedly dump and load

Open Difocd opened this issue 1 year ago • 1 comments

When I dump something contains backslash to file and then load it, there are double backslashes. Like the below:

import toml

with open('test.toml', 'w', encoding='utf-8') as f:
    toml.dump({"c:\\test": {}}, f)

for _ in range(5):
    with open('test.toml', encoding='utf-8') as f:
        contents = toml.load(f)
    print(contents)
    with open('test.toml', 'w', encoding='utf-8') as f:
        toml.dump(contents, f)

This will print:

{'c:\\\\test': {}}
{'c:\\\\\\\\test': {}}
{'c:\\\\\\\\\\\\\\\\test': {}}
{'c:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\test': {}}
{'c:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\test': {}}

I don't know if this is a bug or just a feature. If this is a feature, how can I avoid this?

Difocd avatar Jun 04 '24 07:06 Difocd

I've just bumped into this--or something much like it--myself.

Specifically, it looks to me like TOML, when presented with a backslash (whether alone or escaped), outputs two backslashes in its place.

For example, the following program:

import toml

mew = "cat = 'this is a \ test'"
result = toml.loads(mew)
print (result)

produces the following output on my machine: {'cat': 'this is a \\ test'}

Note that the single backslash has become a double backslash.

TOML version: 1.10.2, I believe OS: Ubuntu Linux 22.04.5

ArsThaumaturgis avatar Sep 23 '24 15:09 ArsThaumaturgis