time.time() strangeness

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Feb 27 01:16:04 EST 2008


En Tue, 26 Feb 2008 17:37:22 -0200, Nitro <nitro at dr-code.org> escribió:

> today I encountered a very odd situation. I am on Windows Vista and using
> Python 2.5.2. Here's a code snippet to illustrate my problem:
>
> # uncomment the next line to trigger the problem
> # myExtensionModule.CreateDirect3D9Device()
> import time
> for i in range(0,100):
>      print time.time()
>
> With the line commented time.time() returns a changing value which is  
> what
> I expect. However, when I uncomment it and create a Direct3D9 Device
> [1][2] it keeps printing the very same number over and over!

I had a similar problem some time ago. When a badly formatted audio file  
was played (on Windows XP, any player, not from Python), the C function  
ftime(&x) fails and fills x to all 0's from this moment on. ftime is  
supposed never to fail, but it does...
time.time uses ftime on Windows.

A possible workaround (floattime function, in timemodule.c): Change

#if defined(HAVE_FTIME)
		struct timeb t;
		ftime(&t);
		return (double)t.time + (double)t.millitm * (double)0.001;
#else /* !HAVE_FTIME */
		time_t secs;
		time(&secs);
		return (double)secs;
#endif /* !HAVE_FTIME */

to:

		time_t secs;
#if defined(HAVE_FTIME)
		double res;
		struct timeb t;
		ftime(&t);
		res = (double)t.time + (double)t.millitm * (double)0.001;
		if (res>0) return res;
#endif /* !HAVE_FTIME */
		time(&secs);
		return (double)secs;

(untested, I wrote this right now, but basically it's what I did that  
time). Finally the Python version was not patched, we just forbid to use  
that server to play MP3s :)
As it was hard to reproduce the problem, I never got to submit a patch.

> In my project
> I am using twisted which uses time.time() to schedule all calls. Since
> time.time() is completely screwed the whole application breaks.
> I took a look at [3], but I can't see any obivous way how this all
> interacts. Specifically I am not sure which API time.time() uses
> internally (timeGetTime maybe?). Knowing this could probably help me  
> debug
> more.

See timemodule.c, time.time maps to time_time, which calls floattime,  
which on Windows uses ftime.

> I feel like time.time() should not break (unless the vid card
> driver/directx has a major bug). Any idea what might be happening here?
> Replacing time.time() with time.clock() in twisted.python.runtime makes
> the problem disappear. I guess because it uses QueryPerformanceCounter.

Seems like a reasonable patch.

-- 
Gabriel Genellina




More information about the Python-list mailing list