try: ... except EOFError: ... idiom

James T. Dennis jadestar at idiom.com
Sun Dec 24 18:05:17 EST 2000


 In the "Source code management" thread I noticed a
 code snippet that used an unusual exception handling
 idiom:

gtalvola at my-deja.com

>      import marshal
>      import win32pipe
>      stream = win32pipe.popen('p4 -G changes', 'r')
>      changes = []
>      try:
>          while 1:
>              changes.append(marshal.load(stream))
>      except EOFError:
>          for c in changes:
>              print c['change'], c['desc']

 How would we write that without the try: ... except: idiom?

 I gather that Python has no bottom tested loop
 (do: ... until... or do: ... while) construct.

 Could we do something like:

	while 1:
		if stream.eof():
			break
		changes.append(marshal.load(stream)

 Is there a <<popen object>>.eof() method?  Is that 
 try: ... except: idiom EOFError *the* generally accepted
 way to process file/streams under Python?  Why isn't there
 some sort of eof() function or object method?

 Also: how would one do non-blocking I/O on pipes and popen()
 streams?




More information about the Python-list mailing list