LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2008, 01:33 PM   #1
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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?
 
Old 05-08-2008, 04:54 PM   #2
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
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?
 
Old 05-09-2008, 06:25 AM   #3
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 43
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.
 
Old 05-09-2008, 09:32 AM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 43
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.
 
  


Reply

Tags
python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python: how to import a python file that doesn't end in .py? BrianK Programming 5 04-15-2008 12:35 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
how to delete last number/word of a file and incude file count at the end of the chennaiguy Linux - Newbie 2 02-18-2008 09:08 PM
SUSE 10.1 - Install skips to the end after first CD contillion SUSE / openSUSE 6 11-07-2006 06:27 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration