Motor: time zone aware error in get using tz_aware=True

79 views
Skip to first unread message

massimiliano cannata

unread,
Jul 8, 2014, 11:27:04 AM7/8/14
to mongod...@googlegroups.com

Hello,
I found that when I use Motor with time zone enabled:

db = motor.MotorClient('localhost', 27017, tz_aware=True)


Using motor.web.GridFSHandler:

class IdGridFSHandler(motor.web.GridFSHandler):

    def get_gridfs_file(self, fs, path):

        # Path is interpreted as _id instead of name.

        # Return a Future MotorGridOut.

        return fs.get(file_id=ObjectId(path))


Result in Browser (Chrome) error due to incompatibility between time zone aware dates:

File "/usr/local/lib/python3.4/dist-packages/motor/web.py", line 133, in get
    if if_since >= modified:
TypeError: can't compare offset-naive and offset-aware datetimes

Motor in fact use email.utils.parsedate which return  non-time aware date tuple while mongodb return a date with time zone:

modified = gridout.upload_date.replace(microsecond=0)
...
...
# Check the If-Modified-Since, and don't send the result if the
# content has not been modified
ims_value = self.request.headers.get("If-Modified-Since")
if ims_value is not None:
    date_tuple = email.utils.parsedate(ims_value)
    if_since = datetime.datetime.fromtimestamp(time.mktime(date_tuple))
    if if_since >= modified:
        self.set_status(304)
        return

Is it a BUG? 
Do you have any suggestion on how to use tz_aware?

Thanks,
Maxi

A. Jesse Jiryu Davis

unread,
Jul 8, 2014, 12:53:56 PM7/8/14
to mongod...@googlegroups.com
Thanks for the bug report, I've opened this ticket to track it:

A. Jesse Jiryu Davis

unread,
Jul 8, 2014, 3:03:21 PM7/8/14
to mongod...@googlegroups.com
I've released Motor 0.3.1 with a fix. Please "pip install --upgrade motor". Thanks again for reporting this.
Reply all
Reply to author
Forward
0 new messages