This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: segfault raising an arbitrary object as an exception
Type: crash Stage: resolved
Components: Versions: Python 3.2, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Trundle, georg.brandl, michael.foord, ncoghlan, pitrou, python-dev, santoso.wijaya
Priority: normal Keywords: patch

Created on 2011-03-21 21:59 by michael.foord, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue11627.patch Trundle, 2011-03-21 22:57 review
issue11627_2.patch Trundle, 2011-03-22 01:24 review
issue11627_3.patch Trundle, 2011-03-22 22:41 review
issue11627_py27.patch Trundle, 2011-03-22 22:41 review
Messages (15)
msg131687 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-03-21 21:59
Python 3.2 (r32:88452, Feb 20 2011, 10:19:59) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(Exception):
...  def __new__(*args):
...   return object()
... 
>>> try:
...  raise Foo
... except Exception as e:
...  print ('got it', e)
... 
Bus error
msg131688 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-03-21 22:02
Personally I don't think this should be valid at all (it should ideally be an error at the raise point). It is the kind of thing that causes difficulties for the other implementations trying to match CPython behaviour (this code works in Python 2.7 - you can catch the object()).
msg131689 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-03-21 22:11
Have you tried 3.1?
msg131690 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2011-03-21 22:13
This:

raise type('',(Exception,),{'__new__':lambda *a:object()}) 

Segfaults 3.2 but not 3.1.
msg131692 - (view) Author: Santoso Wijaya (santoso.wijaya) * Date: 2011-03-21 22:26
Oddly, this works:

C:\Users\santa>C:\python32\python.exe
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(Exception):
...     def __new__(*args):
...             obj = object()
...             print('Returning {}'.format(repr(obj)))
...             return obj
...
>>> try:
...     raise Foo
... except Exception as e:
...     print('Got it: {}'.format(repr(e)))
...
Returning <object object at 0x00000000022D26D0>
Returning <object object at 0x000000000254B8E0>
Got it: <object object at 0x000000000254B8E0>
>>> ^Z


But this does not:

C:\Users\santa>C:\python32\python.exe
Python 3.2 (r32:88445, Feb 20 2011, 21:30:00) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(Exception):
...     def __new__(*args):
...             obj = object()
...             print('Returning', repr(obj))
...             return obj
...
>>> try:
...     raise Foo
... except Exception as e:
...     print('Got it:', repr(e))
...
Returning <object object at 0x00000000022F36D0>
Returning <object object at 0x00000000022F38B0>

crash!
msg131693 - (view) Author: Santoso Wijaya (santoso.wijaya) * Date: 2011-03-21 22:27
Also, why is the print() in __new__ executed twice?
msg131694 - (view) Author: Andreas Stührk (Trundle) * Date: 2011-03-21 22:31
On Mon, Mar 21, 2011 at 10:27 PM, Santoso Wijaya <report@bugs.python.org> wrote:
>
> Santoso Wijaya <santoso.wijaya@gmail.com> added the comment:
>
> Also, why is the print() in __new__ executed twice?

Because `PyErr_NormalizeException()` is called twice: First time when
the exceptions is raised, and then a second time when the exception is
caught. Because the previous call didn't instantiate an instance of a
exception, the second call will (try to) create a new exception
instance.
msg131701 - (view) Author: Andreas Stührk (Trundle) * Date: 2011-03-21 22:57
Attached is a patch. I'm not too happy about the error message though, I think it's more confusing than helpful.
msg131704 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-03-21 23:08
Le lundi 21 mars 2011 à 22:57 +0000, Andreas Stührk a écrit :
> Andreas Stührk <andy-python@hammerhartes.de> added the comment:
> 
> Attached is a patch. I'm not too happy about the error message though,
> I think it's more confusing than helpful.

You could try something more explicit, such as
"calling %s() should have returned an instance of BaseException, not %s"
% (type, Py_TYPE(value))

By the way, you have a tab character in your patch. Please only use
spaces for indentation.
msg131716 - (view) Author: Andreas Stührk (Trundle) * Date: 2011-03-22 01:23
> Antoine Pitrou <pitrou@free.fr> added the comment:
> You could try something more explicit, such as
> "calling %s() should have returned an instance of BaseException, not %s"
> % (type, Py_TYPE(value))

Thanks, updated the patch.

> By the way, you have a tab character in your patch. Please only use
> spaces for indentation.

I am terribly sorry for that. I always realise after the first changed
line that my editor is in the wrong C mode.
msg131740 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-03-22 11:49
Note that this test code:

        def raise_():
            raise MyException
        self.assertRaises(TypeError, raise_)

can be simplified to:

        with self.assertRaises(TypeError):
            raise MyException

in all currently maintained branches.
msg131799 - (view) Author: Andreas Stührk (Trundle) * Date: 2011-03-22 22:41
Thanks Nick, that is indeed much nicer. I updated the patch.

I also created a patch for 2.7, in case anyone thinks it's a good idea to fix it there, too.
msg131842 - (view) Author: Andreas Stührk (Trundle) * Date: 2011-03-23 02:33
Another thing that can happen is that `__new__()` does return an instance of BaseException, but that the return value is not an instance of the expected class.

Example:

class MyException(OSError):
    def __new__(*args):
        return Exception()

try:
    raise MyException
except OSError as e:
    print(isinstance(e, OSError))

That would print "False". Currently, the patch doesn't check for that, should it do so?

Also, the error message for the 2.7 patch doesn't include that old-style classes are allowed, hence the patch needs to be updated if it should go into 2.7.
msg140460 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-07-15 19:07
New changeset 8d05f697acd4 by Benjamin Peterson in branch '3.2':
catch nasty exception classes with __new__ that doesn't return a exception (closes #11627)
http://hg.python.org/cpython/rev/8d05f697acd4

New changeset bc1fbd6f667a by Benjamin Peterson in branch 'default':
merge 3.2 (#11627)
http://hg.python.org/cpython/rev/bc1fbd6f667a
msg140461 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-07-15 19:10
New changeset 45b1ae1ef318 by Benjamin Peterson in branch '2.7':
port 8d05f697acd4 (#11627)
http://hg.python.org/cpython/rev/45b1ae1ef318
History
Date User Action Args
2022-04-11 14:57:15adminsetgithub: 55836
2011-07-15 19:10:55python-devsetmessages: + msg140461
2011-07-15 19:07:03python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg140460

resolution: fixed
stage: resolved
2011-03-23 09:32:01georg.brandlsetnosy: + georg.brandl
2011-03-23 02:33:38Trundlesetnosy: ncoghlan, pitrou, michael.foord, Trundle, santoso.wijaya
messages: + msg131842
2011-03-22 22:41:43Trundlesetfiles: + issue11627_py27.patch
nosy: ncoghlan, pitrou, michael.foord, Trundle, santoso.wijaya
2011-03-22 22:41:11Trundlesetfiles: + issue11627_3.patch
nosy: ncoghlan, pitrou, michael.foord, Trundle, santoso.wijaya
messages: + msg131799
2011-03-22 11:49:06ncoghlansetnosy: + ncoghlan
messages: + msg131740
2011-03-22 01:24:37Trundlesetfiles: + issue11627_2.patch
nosy: pitrou, michael.foord, Trundle, santoso.wijaya
2011-03-22 01:23:43Trundlesetnosy: pitrou, michael.foord, Trundle, santoso.wijaya
messages: + msg131716
2011-03-21 23:08:18pitrousetnosy: pitrou, michael.foord, Trundle, santoso.wijaya
messages: + msg131704
2011-03-21 22:57:04Trundlesetfiles: + issue11627.patch

messages: + msg131701
keywords: + patch
nosy: pitrou, michael.foord, Trundle, santoso.wijaya
2011-03-21 22:31:38Trundlesetnosy: pitrou, michael.foord, Trundle, santoso.wijaya
messages: + msg131694
2011-03-21 22:27:18santoso.wijayasetnosy: pitrou, michael.foord, Trundle, santoso.wijaya
messages: + msg131693
2011-03-21 22:26:39santoso.wijayasetnosy: + santoso.wijaya
messages: + msg131692
2011-03-21 22:13:05michael.foordsetnosy: pitrou, michael.foord, Trundle
messages: + msg131690
2011-03-21 22:11:10pitrousetnosy: + pitrou
messages: + msg131689
2011-03-21 22:04:40Trundlesetnosy: + Trundle
2011-03-21 22:02:31michael.foordsetmessages: + msg131688
2011-03-21 21:59:21michael.foordcreate