LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Python File#readline skips to end of file (http://www.linuxquestions.org/questions/programming-9/python-file-readline-skips-to-end-of-file-640868/)

taylor_venable 05-08-2008 01:33 PM

Python File#readline skips to end of file
 
Hello all, I'm having a problem reading line-at-a-time from a file in Python. Basically, I've got some code like this:
Code:

    while not self.commentStartRegex.search(line):
        print ("# %06d > " % self.handle.tell()) + line[:80].rstrip()
        time.sleep(1)
        line = self.handle.readline()

Where self.commentStartRegex = re.compile(r'^\s*/\*\*'), and which is parsing some input code:
Code:

  final ArrayList<Method> methods = Something.getInstance().lookupSomething(someClass, function);
  if(null == methods || methods.size() == 0)
    throw new NotImplementedException(someClass.toString() + " does not implement the function: " + function);
  return methods;
}

/**
 * This is a comment.

When I run it I get something more or less like this:
Code:

# 004367 >  final ArrayList<Method> methods = Something.getInstance().lookupSomething(someCl
# 004412 >  if(null == methods || methods.size() == 0)
# 004521 >    throw new NotImplementedException(someClass.toString() + " does not implemen
# 004539 >  return methods;
# 004542 > }
# 004543 >
# 013261 >
# 013261 >
... [keeps going ad infinitum]

So it seems that this code is somehow jumping to the end of the file. I don't understand why that is. Plus, there's nothing odd in the file, just printing characters, tabs, and Unix newlines. Does anybody have any ideas why this could be happening?

rocket357 05-08-2008 04:54 PM

Is this part of a bigger, perhaps threaded application?

time.sleep(#) gives up the GIL and could potentially allow a reference to the open file to be read outside of this code...though it seems it'd be exceptionally intermittent if that was the case.

You got me stumped on this one...I've tried threading and all...can't replicate the problem.

Is this code inside a bigger block of code that loops?

taylor_venable 05-09-2008 06:25 AM

No, it's not threaded, which is why I find this really odd. I threw the sleep in there so I could read the output before it suddenly started flying by, so it doesn't change the outcome. And it happens repeatedly, namely every single time the program runs. It's not file-dependent, either, it happens in other source code that contains the same kind of scenario. I'm going to investigate it closer this morning, try to recreate it with as small a chunk of code as possible, but right now it very much seems like that regex matches, then the pointer in the file gets set to the end, then the condition in the loop somehow fails and it keeps repeating. I don't understand it, hopefully some playing around in ipython will yield some clues.

taylor_venable 05-09-2008 09:32 AM

OK, now I feel stupid. It was another regex later on that I *thought* I had shown to be correct which was causing (using a similar modus operandi) the file to be read until the end. <sigh> I guess select() wasn't broken after all. :)


All times are GMT -5. The time now is 04:52 AM.