Python Forum
Strange behavior of parse_qsl when parameter value is '+'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange behavior of parse_qsl when parameter value is '+'
#1
I am seeing unexpected behavior from the parse_qsl function when the query string parameter value is just '+'.

My Python version is 3.11. I am calling parse_qsl to parse query strings from URLs. It works as expected for normal parameter values, but returns an unexpected result when the value is '+'.

Here are some examples of what it returns for different queries:
from urllib.parse import parse_qsl

parse_qsl('username=')  # []
parse_qsl('username= ')  # [('username', ' ')]
parse_qsl('username=1+2')  # [('username', '1 2')]
parse_qsl('username=123')  # [('username', '123')] 
parse_qsl('username=123&username=234')  # [('username', '123'), ('username', '234')]
However, when the value is just '+', it returns the tuple [('username', ' ')]:
parse_qsl('username=+')  # [('username', ' ')]
This seems strange to me, as '+' on its own does not represent a space. I would expect it to return [('username', '+')] or perhaps not parse it at all.

I have searched the Python and urllib documentation but did not find any mention of this specific behavior. Can anyone explain why parse_qsl returns a space in this case, and if it is expected behavior defined somewhere? Or is this a bug in the implementation?

Any insight would be appreciated. Please let me know if you need any other context or details from me. I want to understand this parsing behavior better.
Reply


Messages In This Thread
Strange behavior of parse_qsl when parameter value is '+' - by dingning - Dec-28-2023, 10:04 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020