time.gmtime()导致armhf平台上的OverflowError [英] time.gmtime() causes OverflowError on armhf platform

查看:250
本文介绍了time.gmtime()导致armhf平台上的OverflowError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络服务器(CherryPy)在一个Cubox(armhf平台)上运行,一旦启动了我,我会收到以下错误:

  [14 / Aug / 2015:09:33:40] HTTP追溯(最近的最后一次呼叫):
文件(...)/ lib / python3.4 / site-packages / cherrypy / _cprequest.py,第661行,回复
self.hooks.run('before_request_body')
文件(...)/ lib / python3.4 / site-packages / cherrypy / _cprequest。 py,行114,在运行
raise exc
文件(...)/ lib / python3.4 / site-packages / cherrypy / _cprequest.py,行104,在运行
hook()
文件(...)/ lib / python3.4 / site-packages / cherrypy / _cprequest.py,第63行,__call__
return self.callback(** self.kwargs)
文件(...)/ lib / python3.4 / site-packages / cherrypy / lib / sessions.py,第901行,init
httponly = httponly)
文件(...)/ lib / python3.4 / site-packages / cherrypy / lib / sessions.py,第951行,set_response_cookie
cookie [name] ['expires'] = httputil。 HTTPDate(e)
文件(...)/ lib / python3.4 / site-packages / cherrypy / _cpcompat.py,第278行,在HTTPDate
中返回formatdate(timeval,usegmt = True)
文件/usr/lib/python3.4/email/utils.py,第177行,格式化
now = time.gmtime(timeval)
溢出错误:时间戳超出范围的平台time_t

我不知道我是否正确理解问题,我不知道是否可以修复我。据我所知,Traceback是由CherryPy造成的。此错误导致 500内部服务器错误,并且不会加载页面。



正如评论中所提到的插入一个打印。我没有看到任何特别的东西。这是启动服务器并尝试加载页面的输出:

  1439551125.1483066 
1439551132.639804
4593151132.6458025
1439551132.723468
1439551132.7210276
1439551132.7268708
1439551132.735934
1439551132.741787
1439551132.7452564
4593151132.750907
4593151132.762612
4593151132.749376
4593151132.731232
4593151132.754474
4593151132.763546
453151132.8183882
4593151132.828029
1439551132.8379567
4593151132.856025
1439551132.8734775
1439551132.8554301
1439551132.879614
4593151132.884698
4593151132.890394
1439551132.8971672
4593151132.902081
4593151132.908171
1439551132.931757
4593151132.944052
1439551132.9759347
1439551132.9714596
4593151132.987068
4593151132.985899
1439551132.9926524
1439551133.0088623
4593151133.013047
1439551133.0280995
4593151133.040709
4593151 133.029601
1439551133.0500746
4593151133.057341
1439551133.0749385
4593151133.081711
1439551133.1032782
4593151133.115171
1439551133.1194305
1439551133.1354048
4593151133.143136
4593151133.151044
1439551133.1612003
4593151133.16934
453151133.1827784
4593151133.19687
1439551133.201899
4593151133.209947
1439551133.271833
4593151133.277573
1439551133.3090906
4593151133.312978
1439551133.3408027
4593151133.344741
1439551133.3722978
4593151133.376283
1439551133.4031894
4593151133.407124
1439551133.434834
4593151133.439074

我不知道哪些值导致错误。我猜这是在前面有4个?在Windows机器上 time.gmtime(4593151133.439074)返回一个包含2115年的结构。



在Cubox上当启动一个python shell并进入 time.gmtime(4593151133.439074)我可以重现错误。但我不知道这些价值观来自哪里。



编辑



我找到了返回我的CherryPy中的文件和行导致2115年的浮点数。文件session.py中的第949 - 951行:

  if timeout: 
e = time.time()+(timeout * 60)
cookie [name] ['expires'] = httputil.HTTPDate(e)

为什么我得到这么高的超时,我不知道。

解决方案>

要解决armhf平台上C gmtime()的范围限制,您可以使用显式公式从POSIX时间戳获取UTC时间作为文档建议

 >>>从datetime import datetime,timedelta 
>>> datetime(1970,1,1)+ timedelta(seconds = 4593151133.439074)
datetime.datetime(2115,7,21,11,18,53,439074)
>>> datetime.utcfromtimestamp(4593151133.439074)#在内部调用gmtime()
datetime.datetime(2115,7,21,11,18,53,439073)#非正确时区的几乎相同的结果


I have a webserver (CherryPy) running with on a Cubox (armhf platform) and upon starting the wever i get the following error:

[14/Aug/2015:09:33:40] HTTP Traceback (most recent call last):
  File "(...)/lib/python3.4/site-packages/cherrypy/_cprequest.py", line 661, in respond
    self.hooks.run('before_request_body')
  File "(...)/lib/python3.4/site-packages/cherrypy/_cprequest.py", line 114, in run
    raise exc
  File "(...)/lib/python3.4/site-packages/cherrypy/_cprequest.py", line 104, in run
    hook()
  File "(...)/lib/python3.4/site-packages/cherrypy/_cprequest.py", line 63, in __call__
    return self.callback(**self.kwargs)
  File "(...)/lib/python3.4/site-packages/cherrypy/lib/sessions.py", line 901, in init
    httponly=httponly)
  File "(...)/lib/python3.4/site-packages/cherrypy/lib/sessions.py", line 951, in set_response_cookie
    cookie[name]['expires'] = httputil.HTTPDate(e)
  File "(...)/lib/python3.4/site-packages/cherrypy/_cpcompat.py", line 278, in HTTPDate
    return formatdate(timeval, usegmt=True)
  File "/usr/lib/python3.4/email/utils.py", line 177, in formatdate
    now = time.gmtime(timeval)
OverflowError: timestamp out of range for platform time_t

I'm not sure whether I understand the problem correctly and I am not sure if it can be fixed by me. As far as I can tell by the Traceback it is caused by CherryPy. This error causes a 500 Internal Server Error and won't load the page.

As asked in the comments I inserted a print. I don't see any thing special. This is the output of starting the server and once trying to load the page:

1439551125.1483066
1439551132.639804
4593151132.6458025
1439551132.723468
1439551132.7210276
1439551132.7268708
1439551132.7359934
1439551132.741787
1439551132.7452564
4593151132.750907
4593151132.762612
4593151132.749376
4593151132.731232
4593151132.754474
4593151132.763546
1439551132.8183882
4593151132.828029
1439551132.8379567
4593151132.856025
1439551132.8734775
1439551132.8554301
1439551132.879614
4593151132.884698
4593151132.890394
1439551132.8971672
4593151132.902081
4593151132.908171
1439551132.931757
4593151132.944052
1439551132.9759347
1439551132.9714596
4593151132.987068
4593151132.985899
1439551132.9926524
1439551133.0088623
4593151133.013047
1439551133.0280995
4593151133.040709
4593151133.029601
1439551133.0500746
4593151133.057341
1439551133.0749385
4593151133.081711
1439551133.1032782
4593151133.115171
1439551133.1194305
1439551133.1354048
4593151133.143136
4593151133.151044
1439551133.1612003
4593151133.16934
1439551133.1827784
4593151133.19687
1439551133.201899
4593151133.209947
1439551133.271833
4593151133.277573
1439551133.3090906
4593151133.312978
1439551133.3408027
4593151133.344741
1439551133.3722978
4593151133.376283
1439551133.4031894
4593151133.407124
1439551133.434834
4593151133.439074

I am not sure which of these values causes the error. I am guessing it's the one with 4 in front? On a windows machine time.gmtime(4593151133.439074) returns a struct which contains the year 2115.

On the Cubox when starting a python shell and entering time.gmtime(4593151133.439074) I can reproduce the error. But I don't know where these values come from.

EDIT

I have found the file and line in CherryPy that returns me the floating numbers which lead to the year 2115. It is line 949 - 951 in the file session.py:

if timeout:
    e = time.time() + (timeout * 60)
    cookie[name]['expires'] = httputil.HTTPDate(e)

Why I get such a high timeout, I don't know.

解决方案

To workaround the range limitation for C gmtime() on armhf platform, you could use the explicit formula to get the UTC time from a POSIX timestamp as the docs suggest:

>>> from datetime import datetime, timedelta
>>> datetime(1970, 1, 1) + timedelta(seconds=4593151133.439074)
datetime.datetime(2115, 7, 21, 11, 18, 53, 439074)
>>> datetime.utcfromtimestamp(4593151133.439074) # calls gmtime() internally
datetime.datetime(2115, 7, 21, 11, 18, 53, 439073) # almost same result on non-"right" timezones

这篇关于time.gmtime()导致armhf平台上的OverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆