huangbop / python-rest-client

Automatically exported from code.google.com/p/python-rest-client
0 stars 0 forks source link

Default empty headers aren't safely defined in request_*() #12

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Comme python mistake.
It causes a real problem here when the function is called several times 
with headers uninitialized : added headers are kept in next call. Sorry to 
be short on details.

There is a quick fix, but this could be fixed better.

===================================================================
--- restful_lib.py  (revision 12)
+++ restful_lib.py  (working copy)
@@ -79,6 +79,7 @@
         return guessed_mimetype or 'application/octet-stream'

     def request(self, resource, method = "get", args = None, body = None, 
filename=None, headers={}):
+   headers = headers or {} ## prevents old headers to be carried along 
with further requests
         params = None
         path = resource
         headers['User-Agent'] = 'Basic Agent'

Original issue reported on code.google.com by dmarclay...@gmail.com on 8 Mar 2010 at 11:32

GoogleCodeExporter commented 9 years ago
Could you elaborate more on this. I thought headers only lived in the scope of 
a single request(...) call. How are added headers kept in the next call if they 
are 'destroyed' at the end of the previous one. Unless "headers" exists outside 
the request(...) and then there's that whole confusion of locals versus 
pseudo-globals, etc.

Thank you

Original comment by dr.chamb...@gmail.com on 11 Oct 2010 at 6:15

GoogleCodeExporter commented 9 years ago
Hello,

In fact default values are evaluated only once and then live with the function.
See the « important waning » here for further details : 
http://www.network-theory.co.uk/docs/pytut/DefaultArgumentValues.html

In this case, headers are being unexpectedly kept across subsequent requests.

Cheers,

David

Original comment by dmarclay...@gmail.com on 12 Oct 2010 at 10:22