morsel['max-age'] may be a str. Should it be converted to float?

Example:

here are Set-Cookie list:

``` python
sclst = ['dwac_bcIBMiaagZmkYaaadeYtg11eVR=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA%3D|dw-only|||CNY|false|Asia%2FShanghai|true; Path=/',
 'sid=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA; Path=/',
 'geoLocation=CN; Path=/',
 'dwpersonalization_fae107a9dd0fc32ed99532ec1977f31f=bc8sEiaagZqRsaaadk8XoNTL8h20150506; Expires=Sun, 14-Jun-2015 10:37:07 GMT; Path=/',
 'dwanonymous_fae107a9dd0fc32ed99532ec1977f31f=abjpA8kng31LjPp8ZEERDT4XVg; Version=1; Comment="Demandware anonymous cookie for site Sites-abercrombie_cn-Site"; Max-Age=15552000; Expires=Tue, 10-Nov-2015 10:37:07 GMT; Path=/',
 'myStore=91156; Path=/',
 'AF_PREF=en_CN; Path=/',
 'dwsid=MiHJy3KYZKDcN0lZg4HS1zSpj1VV4s_tFu39ar0KXNpAx9aX8X2LlvZQ9m5fOOknb6QXtmmukHcOjmivYf31hg==; path=/; HttpOnly']
```

``` python
    for sc in sclst:
        C = Cookie.SimpleCookie(sc)
        for morsel in C.values():
            cookie = requests.cookies.morsel_to_cookie(morsel)
            cookiejar.set_cookie(cookie)
```

Then, exception occured

``` shell
  File "/Library/Python/2.7/site-packages/requests/cookies.py", line 402, in morsel_to_cookie
    expires = time.time() + morsel['max-age']
TypeError: unsupported operand type(s) for +: 'float' and 'str'
```

As Cookie.SimpleCookie is in STL, should `morsel_to_cookie` check the type of `max-age`? On the other hand, if **max-age** can not be converted to float, it's illegal obviously.


I think you're using `morsel_to_cookie` incorrectly. It expects a `Cookie.Morsel`. Did you try that to see if it works?


It's a `Cookie.Morsel` object.

here's the test code from real world:

``` python
import Cookie
import requests

cookiejar = requests.cookies.RequestsCookieJar()
sclst = ['dwac_bcIBMiaagZmkYaaadeYtg11eVR=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA%3D|dw-only|||CNY|false|Asia%2FShanghai|true; Path=/',
 'sid=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA; Path=/',
 'geoLocation=CN; Path=/',
 'dwpersonalization_fae107a9dd0fc32ed99532ec1977f31f=bc8sEiaagZqRsaaadk8XoNTL8h20150506; Expires=Sun, 14-Jun-2015 10:37:07 GMT; Path=/',
 'dwanonymous_fae107a9dd0fc32ed99532ec1977f31f=abjpA8kng31LjPp8ZEERDT4XVg; Version=1; Comment="Demandware anonymous cookie for site Sites-abercrombie_cn-Site"; Max-Age=15552000; Expires=Tue, 10-Nov-2015 10:37:07 GMT; Path=/',
 'myStore=91156; Path=/',
 'AF_PREF=en_CN; Path=/',
 'dwsid=MiHJy3KYZKDcN0lZg4HS1zSpj1VV4s_tFu39ar0KXNpAx9aX8X2LlvZQ9m5fOOknb6QXtmmukHcOjmivYf31hg==; path=/; HttpOnly']

for sc in sclst:
    C = Cookie.SimpleCookie(sc)
    for morsel in C.values():
        print type(morsel), morsel
        cookie = requests.cookies.morsel_to_cookie(morsel)
        cookiejar.set_cookie(cookie)
```

Returns:

``` python
<class 'Cookie.Morsel'> Set-Cookie: dwac_bcIBMiaagZmkYaaadeYtg11eVR=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA%3D|dw-only|||CNY|false|Asia%2FShanghai|true; Path=/
<class 'Cookie.Morsel'> Set-Cookie: sid=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA; Path=/
<class 'Cookie.Morsel'> Set-Cookie: geoLocation=CN; Path=/
<class 'Cookie.Morsel'> Set-Cookie: dwpersonalization_fae107a9dd0fc32ed99532ec1977f31f=bc8sEiaagZqRsaaadk8XoNTL8h20150506; expires=Sun, 14-Jun-2015 10:37:07 GMT; Path=/
<class 'Cookie.Morsel'> Set-Cookie: dwanonymous_fae107a9dd0fc32ed99532ec1977f31f=abjpA8kng31LjPp8ZEERDT4XVg; Comment=Demandware anonymous cookie for site Sites-abercrombie_cn-Site; expires=Tue, 10-Nov-2015 10:37:07 GMT; Max-Age=15552000; Path=/; Version=1
Traceback (most recent call last):
  File "test.py", line 18, in <module>
    cookie = requests.cookies.morsel_to_cookie(morsel)
  File "/Library/Python/2.7/site-packages/requests-2.5.1-py2.7.egg/requests/cookies.py", line 393, in morsel_to_cookie
    expires = time.time() + morsel['max-age']
TypeError: unsupported operand type(s) for +: 'float' and 'str'
```


I'm failing to understand why this doesn't happen when we handle cookies on a response with `max-age` set, but I guess it isn't that big of a deal. This is a small enough change. I would just like a comment explaining why it's necessary and maybe referencing this issue as `kennethreitz/requests#2602`


That is, assuming, @Lukasa has no objections


I have no particular objection to this. =)


Hoping to retrigger CI


So this breaks an existing test that guarded against a different problem: http://ci.kennethreitz.org/job/requests-pr/665/PYTHON=2.7/console 


Codes updated.

According to [RFC6265](https://www.ietf.org/rfc/rfc2616.txt),

```
5.2.2.  The Max-Age Attribute

  If the attribute-name case-insensitively matches the string "Max-
  Age", the user agent MUST process the cookie-av as follows.

  If the first character of the attribute-value is not a DIGIT or a "-"
  character, ignore the cookie-av.**

  If the remainder of attribute-value contains a non-DIGIT character,
  ignore the cookie-av.

  Let delta-seconds be the attribute-value converted to an integer.

  If delta-seconds is less than or equal to zero (0), let expiry-time
  be the earliest representable date and time.  Otherwise, let the
  expiry-time be the current date and time plus delta-seconds seconds.

  Append an attribute to the cookie-attribute-list with an attribute-
  name of Max-Age and an attribute-value of expiry-time.
```

So, if `max-age` value is not positive or negative numbers, we should ignore its affection.

:-o **but according to unittest, it is supposed to be raised a TypeError exception.**


LGTM, but I'm going to wait for @sigmavirus24 on this one.


Hi @ly0 

I pulled down your PR and squashed those three commits with the same exact message. The fix is on master as of https://github.com/kennethreitz/requests/commit/ab1f493c8b6f82cbf80f8554b5fbbd02f2b2a363

Thanks!

:sparkles: :cake: :sparkles: