Skip to content

dumps() does not quote empty string values, producing output that cannot be parsed back #17

@anthonyoteri

Description

@anthonyoteri

Bug

dumps() serializes an empty string value as k = \n (nothing after the =). The parser cannot recover the empty string from this output — the token after = is effectively absent, causing incorrect parsing or a ParserError.

The round-trip loads(dumps(data)) == data fails for any dict containing an empty string value.

Reproducer

import structprop
out = structprop.dumps({"k": ""})
print(repr(out))          # 'k = \n'
back = structprop.loads(out)
print(back)               # {'k': <next key's value or ParserError>}

Root cause

_escape() only checks for space and tab characters. An empty string contains neither, so it is returned as-is, producing a bare empty token.

Fix

_escape should return '""' for empty strings:

def _escape(k):
    if not k:
        return '""'
    for ch in _ESCAPE_CHARACTERS:
        if ch in k:
            return '"%s"' % (k,)
    return k

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions