Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Understanding and dealing with an exception

27 views
Skip to first unread message

Chris Angelico

unread,
Oct 14, 2012, 12:31:05 AM10/14/12
to pytho...@python.org
On Sun, Oct 14, 2012 at 3:23 PM, Vincent Davis <vin...@vincentdavis.net> wrote:
> OverflowError: Python int too large to convert to C long
> line 266, in _maketile
> bytecount = read(channels * ysize * 2)

Is the file over 2GB? Might be a limitation, more than a bug, and one
that could possibly be raised by using a 64-bit build.

Alternatively, you could deem them invalid for exceeding your file
size limit (either before passing to PIL, or on catching this
exception).

ChrisA

Chris Angelico

unread,
Oct 14, 2012, 12:49:36 AM10/14/12
to pytho...@python.org
On Sun, Oct 14, 2012 at 3:36 PM, Vincent Davis <vin...@vincentdavis.net> wrote:
> Oops, I was going to make note of the file size. 1.2MB

Then I'd definitely declare the file bad; I don't know what the valid
ranges for channels and ysize are, but my reading of that is that your
file's completely corrupt, maybe even malicious. PIL probably ought to
check these things, so there may be a tracker issue coming from this,
but I'd be inclined to declare any thrown exception as meaning it's a
bad file. Call it "failed a security check" perhaps.

ChrisA

Chris Angelico

unread,
Oct 14, 2012, 1:09:44 AM10/14/12
to pytho...@python.org
On Sun, Oct 14, 2012 at 4:01 PM, Vincent Davis <vin...@vincentdavis.net> wrote:
> I can open it is and all looks good using Pixelmator (I don't have Photoshop
> installed). I don't think there is anything wrong with the image.
>
> Part of my question is a result of being new to actually using exceptions in
> my programs and dealing with the exceptions is a primary part of what I need
> to do with this program. When I get an exception that seems to be an issue
> with PIL (i.e. not my program or a problem with the image) I am not sure
> what the "right" or conventional way to deal with it is.

Ah, okay. Sorry, I don't know PIL at all, and was just reading the
error message itself. It does seem that you're trying to read more
than 1<<31 bytes from a 1.2MB file, though you could confirm that by
looking at the source code - the file and line as in the traceback.

ChrisA

Mark Lawrence

unread,
Oct 14, 2012, 4:20:37 AM10/14/12
to pytho...@python.org
On 14/10/2012 05:23, Vincent Davis wrote:
> I am working on a script to find bad image files. I am using PIL
> and specifically image.verify() I have a set of known to be bad image files
> to test. I also what to be able to test any file for example a .txt and
> deal with the exception.
> Currently my code is basically
>
> try:
> im = Image.open(ifile)
> try:
> print(im.verify())
> except:
> print('Pil image.verify() failed: ' + afile)
> except IOError:
> print('PIL cannot identify image file: ' + afile)
> except:
> print(ifile)
> print("Unexpected error doing PIL.Image.open():", sys.exc_info()[0])
> raise

[snip]

>
> Vincent
>

You've already had some advice so I'll just point out that a bare except
is a bad idea as you wouldn't even be able to catch a user interrupt.
Try (groan!) catching StandardError instead.

--
Cheers.

Mark Lawrence.

Terry Reedy

unread,
Oct 14, 2012, 6:06:16 AM10/14/12
to pytho...@python.org
On 10/14/2012 4:20 AM, Mark Lawrence wrote:

> You've already had some advice so I'll just point out that a bare except
> is a bad idea as you wouldn't even be able to catch a user interrupt.
> Try (groan!) catching StandardError instead.

There are some bare except:s in the stdlib, that adding another is
frowned on and removing one is smiled upon.

However:
>>> StandardError
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
StandardError
NameError: name 'StandardError' is not defined

Try:
>>> Exception
<class 'Exception'>

This catches everything except a few things like Keyboard Interrupt that
you normally should not catch.

>>> BaseException
<class 'BaseException'>

This catches everything, but signals that doing so is probably intentional.

--
Terry Jan Reedy

Mark Lawrence

unread,
Oct 14, 2012, 7:17:29 AM10/14/12
to pytho...@python.org
White Man type with forked fingers?

c:\Users\Mark\Cash\Python>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> StandardError
<type 'exceptions.StandardError'>

Perhaps not.

c:\Users\Mark\Cash\Python>py -3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> StandardError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'StandardError' is not defined

Down to this http://www.python.org/dev/peps/pep-3151/ or was it done
earlier?

--
Cheers.

Mark Lawrence.

MRAB

unread,
Oct 14, 2012, 11:13:59 AM10/14/12
to pytho...@python.org
On 2012-10-14 05:23, Vincent Davis wrote:
> I am working on a script to find bad image files. I am using PIL
> and specifically image.verify() I have a set of known to be bad image
> files to test. I also what to be able to test any file for example a
> .txt and deal with the exception.
> Currently my code is basically
>
> try:
> im = Image.open(ifile)
> try:
> print(im.verify())
> except:
> print('Pil image.verify() failed: ' + afile)
> except IOError:
> print('PIL cannot identify image file: ' + afile)
> except:
> print(ifile)
> print("Unexpected error doing PIL.Image.open():", sys.exc_info()[0])
> raise
>
[snip]
I notice that you have both "ifile" and "afile". Is that correct?

0 new messages