views:

43

answers:

1

I have not had much luck hunting for finding a good explanation of what invalid argument errors are and what would cause them.

My current sample I am working with is

import sys
mylog="mylog.log"
sys.stdout = open(mylog,'w')
#lots of code
#.
#.
#.
#End of lots of code
from time import clock
print "blablabla",clock()

I receive an IOError Invalid Argument error on the clock line. I have also tried

print "blablabla\t%s"%clock()

Any information about this error would be great help. Those lines work perfectly fine on short runs, it just after running the code for a while it breaks. I have tried to setting the buffer size to something low like 45-100 lines.

+1  A: 

I can't reproduce this exact problem on my own computer, so I can't give specific advice, but here is some general commentary on how to debug this sort of thing.

When you see "Invalid argument" in an IOError or OSError exception from python, that means the interpreter tried to make a system call, which failed and set errno to the code EINVAL. (Tangentially, python really shouldn't print the numeric values for errno codes - the symbolic names are standardized but the numbers aren't.) The first thing you need to do is find out which system call it was, and the easiest way to do that is run your program under the strace utility, like this:

$ strace -f -o strace.log python yourscript.py [arguments...]

Wait for it to fail, then search the file strace.log for "-1 E" (exactly that string). You will find something like this:

times({tms_utime=162, tms_stime=123, tms_cutime=0, tms_cstime=0}) = 1718279979
write(1, "2.85\n", 5)                   = -1 EINVAL (Invalid argument)

You then read the man page for the system call that failed ("man 2 write" in this case) and look for the errno code name (EINVAL in this case), and see what it says has gone wrong.

In this case I strongly suspect you have found a bug in either the Python interpreter or the operating system. "Invalid argument" means what it says - one of the input arguments to the system call had an invalid value. You're not doing anything tricky in your script, so either the interpreter is messing up its system calls, or the kernel misunderstood what the interpreter wanted.

Zack
I am doing many things in my script. I am running those type of print statements over and over many times. What happens before that print is attempt to run a specific ATA command on a separate drive and print what happened. Your solution is great on Linux, but unfortunately I am working with Python 2.5 on a Windows 7 machine.
Tim McJilton
Also side question, how do you suggest avoiding such an error?
Tim McJilton
It would have been helpful if you had mentioned the operating system and Python version in use, within your question. There *are* equivalents to strace for Windows - I'd probably try Microsoft's Process Monitor utility first (http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx).I would guess that your problem is actually with the ATA command you're trying to run, and the IOError is getting attached to the clock() call by mistake, but without seeing that part of the code it's hard to tell.
Zack
Sorry I was more interested in the error than the specific answer when initially posting it. My apologies for forgetting to attach the python version and OS. Would a seperate drive that the code running this ATA command in C throw an error that would get caught by python? Also sometimes it errors out when it is not around the ATA command. Anyways thanks again! I really do appreciate your help!
Tim McJilton
I don't know what you mean by "... a separate drive that the code running this ATA command in C ..." It's just that simple, commonly used things like `clock()` and `print` are far less likely to be responsible for system call failures than complicated, rarely used things like ATA direct command execution.I'd really need to see the whole program to get any further here.
Zack
Sorry for my horrid grammar on that. My python command is running the ata command directly in a C program compiled as a .dll . This ata command is being sent to an external hard-drive. That being said, I cannot hand out the program. I am running process monitor and see what specific read or write is throwing this error.
Tim McJilton
Can you show the python code that invokes the dll?
Zack
Unfortunately I cannot. Though I think the error has to do with running it on a server. Running it on the desktop local drive seems to make it work so far. Thanks to using the process monitor thanks!
Tim McJilton