[Pyramid] route_url _query None value behavior

18 views
Skip to first unread message

Jerry

unread,
May 21, 2011, 9:32:05 PM5/21/11
to pylons-devel
Hi,

Pyramid's route_url/route_path behavior with None _query terms doesn't
seem very canonical to me --

(Pdb) request.route_path('home', _query=[('q', None)])
'/home?q=None'

Omitting value is more like it --

(Pdb) request.route_path('home', _query=[('q', '')])
'/home?q='

so is omitting both --

(Pdb) request.route_path('home', _query=[('q', [])])
'/home?'

Chris and other Pyramid maintainers: would you consider adding this
simple check to urlencode() in pyramid/encode.py ? --

89 for (k, v) in query:
90 if k.__class__ is unicode:
91 k = k.encode('utf-8')
92 k = quote_plus(str(k))
93 if hasattr(v, '__iter__'):
94 for x in v:
95 if x.__class__ is unicode:
96 x = x.encode('utf-8')
97 x = quote_plus(str(x))
98 result += '%s%s=%s' % (prefix, k, x)
99 prefix = '&'
100 else:
101 if v.__class__ is unicode:
102 v = v.encode('utf-8')
if v:
103 v = quote_plus(str(v))
104 result += '%s%s=%s' % (prefix, k, v)
else:
result += '%s%s=' % (prefix, k)
105 prefix = '&'

Thanks.

Jerry

Jerry

unread,
May 21, 2011, 9:35:54 PM5/21/11
to pylons-devel
Google group messes with the formatting, which should have been --

else:
if v.__class__ is unicode:
v = v.encode('utf-8')
if v:
v = quote_plus(str(v))
result += '%s%s=%s' % (prefix, k, v)
else:
result += '%s%s=' % (prefix, k)

Jerry

Chris McDonough

unread,
May 21, 2011, 10:39:41 PM5/21/11
to pylons...@googlegroups.com
Don't think this is really right if you consider the desire to be able
to pass integers (like 0), which others have requested before.

What precedent is there to passing the value None being converted to
empty string?

- C

Mike Orr

unread,
May 24, 2011, 12:02:55 PM5/24/11
to pylons...@googlegroups.com
webhelpers.util.update_params deletes any parameter with a value of
None. Mako templates converts None values to ''. Having None convert
to "None" is almost never useful in query parameters, while having
integers and other types converted to strings is useful. So that all
argues for trapping None and either converting it to '' or deleting
the parameter.

> --
> You received this message because you are subscribed to the Google Groups "pylons-devel" group.
> To post to this group, send email to pylons...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-devel...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylons-devel?hl=en.
>
>

--
Mike Orr <slugg...@gmail.com>

Jerry

unread,
May 24, 2011, 9:22:56 PM5/24/11
to pylons-devel
Thanks Mike.

Chris, so how about this to offload some work from the template/helper
--

if v is not None:
v = quote_plus(str(v))
result += '%s%s=%s' % (prefix, k, v)

Jerry
> Mike Orr <sluggos...@gmail.com>

Chris McDonough

unread,
May 26, 2011, 2:35:35 PM5/26/11
to pylons...@googlegroups.com
On Tue, 2011-05-24 at 18:22 -0700, Jerry wrote:
> Thanks Mike.
>
> Chris, so how about this to offload some work from the template/helper
> --
>
> if v is not None:
> v = quote_plus(str(v))
> result += '%s%s=%s' % (prefix, k, v)

That's probably fine. It will need tests and docs. The right place to
submit it is via a github pull request.

- C

Reply all
Reply to author
Forward
0 new messages