Errno 9] Bad file descriptor

Cameron Simpson cs at zip.com.au
Tue Jul 13 20:32:48 EDT 2010


On 13Jul2010 05:56, joblack <johannes.black at gmail.com> wrote:
| Thanks for the answers so far. It's not my code I'm just curious how
| that could happen:
| 
| Starting point:
| ...
|         self.status['text'] = 'Processing ...'
|         try:
|             cli_main(argv)
|         except Exception, e:
|             self.status['text'] = 'Error: ' + str(e)
|             return
| ...
| cli_main:
| 
|     keypath, inpath, outpath = argv[1:]
| ...
|     with open(inpath, 'rb') as inf:
|         serializer = PDFSerializer(inf, keypath)
|         with open(outpath, 'wb') as outf:
|             filenr = outf.fileno()
|             serializer.dump(outf)
|     return 0
| 
| PDFSerializer.dump:
| 
|     def dump(self, outf):
|         self.outf = outf
| ...


See that you set serializer.outf to the outf you open in cli_main?
Any attempt to use serializer _after_ exiting the "with open(outpath,
'wb') as outf" will use serializer.outf, but the outf is now closed.
And thus its file descriptor is invalid.

BTW, by catching Exception in the starting point code you prevent
yourself seeing exactly which line throws the error. It is usualy a bad
idea to catch broad things like "Exception". It is normally better to
place try/except around very small pieces of code and to catch very
specific things. That way you know exactly what's going wrong and don't
quietly catch all sorts of unplanned stuff.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

When buying and selling are controlled by legislation, the first things
bought and sold are the legislators. - P.J. O'Rourke



More information about the Python-list mailing list