Skip to content

Fix str2dicts mangling lines that have no value - #2246

Open
aad1tyaaaaa wants to merge 2 commits into
apache:trunkfrom
aad1tyaaaaa:fix-str2dicts-key-only
Open

aad1tyaaaaa wants to merge 2 commits into
apache:trunkfrom
aad1tyaaaaa:fix-str2dicts-key-only

Conversation

@aad1tyaaaaa

Copy link
Copy Markdown

Fix str2dicts mangling lines that have no value

Description

libcloud.utils.misc.str2dicts (used by the CloudSigma 1.0 driver to parse API responses) is meant to skip a line that has no space, but the check is if not whitespace: on the result of str.find, which returns -1, not 0, when there is no match. So the skip never runs and the line is split at -1:

>>> from libcloud.utils.misc import str2dicts, dict2str
>>> str2dicts("cpu 1100\nvnc\nram 640")
[{'cpu': '1100', 'vn': 'vnc', 'ram': '640'}]
>>> str2dicts(dict2str({"a": None}))
[{'': 'a'}]

This also means str2dicts(dict2str(d)) doesn't round-trip when d has a None value, since dict2str writes those as bare keys. The fix compares against -1, so such lines are skipped as intended.

Status

  • done, ready for review

Checklist

  • Code linting (black, isort)
  • Documentation (CHANGES.rst entry)
  • Tests: new test_str2dicts_skips_lines_without_value in libcloud/test/compute/test_cloudsigma_v1_0.py fails on trunk and passes with this change; the CloudSigma v1/v2 and utils tests pass.
  • ICLA not required for this small change

I used an AI assistant (Claude Code) to help find this bug and write the fix and test. I reviewed and ran everything myself.

str.find returns -1, not 0, when there is no space, so the check never
skipped such lines and "vnc" became {"vn": "vnc"}.
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.

1 participant