Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyHANDLE destructor triggers a SystemError #9

Closed
ghost opened this issue Apr 18, 2003 · 5 comments
Closed

PyHANDLE destructor triggers a SystemError #9

ghost opened this issue Apr 18, 2003 · 5 comments

Comments

@ghost
Copy link

ghost commented Apr 18, 2003

This code is in PyHANDLE.cpp:

/*static*/ void PyHANDLE::deallocFunc(PyObject *ob)
{
// Call virtual method Close
((PyHANDLE *)ob)->Close();
PyErr_Clear(); // can not leave pending exceptions
in destructors.
delete (PyHANDLE *)ob;
}

If this is called because an unrelated exception
occurred, it incorrectly clears the exception.

For example, suppose I have Python code that calls a
Python function f1(). This function successfully
creates a PyHANDLE, stores it in a local varaible, and
then raises an unrelated exception. The caller, f2(),
catches the exception. At this point, the PyHANDLE
still exists because the traceback object has a
reference to it (indirectly). Now suppose f2() throws
an exception. The previous traceback object is now
released (replaced by the new traceback). The PyHANDLE
object is decref'd and freed. PyHANDLE::deallocFunc()
is called. The exception that f2() threw is now cleared.

This results in a SystemError. The attached test case
reproduces it.

I think it should be

if (!((PyHANDLE *)ob)->Close())
PyErr_Clear();

but I'm not sure.

Reported by: jorend

Original Ticket: "pywin32/bugs/9":https://sourceforge.net/p/pywin32/bugs/9

@ghost
Copy link
Author

ghost commented Apr 18, 2003

Test case.

Original comment by: jorend

@ghost
Copy link
Author

ghost commented Apr 18, 2003

  • assigned_to: nobody --> mhammond

Original comment by: jorend

@ghost
Copy link
Author

ghost commented Apr 20, 2003

Logged In: YES
user_id=14198

This is a little nasty. I think the PyErr_Clear call is a
hack, but Python prints a warning in some cases. The
semantics for exceptions during object destruction are hard,
and I will need to look into things a little more - but in
the meantime, please see the new win32\tests\handles.py -
your test case with some additions. These additions
exercise some of these other cases, which will hopefully
allow you to see my dilemma.

I've been meaning to move to unittest, and this gave me the
push I needed - thanks! I will probably move some of the
other "demos" code into tests where it belongs.

Original comment by: mhammond

@ghost
Copy link
Author

ghost commented Apr 22, 2004

  • status: open --> closed-fixed

Original comment by: mhammond

@ghost
Copy link
Author

ghost commented Apr 22, 2004

Logged In: YES
user_id=14198

This has been fixed for a while now, and should be in the
most recent build. win32\test\handles.py should demonstrate
it is fixed.

Original comment by: mhammond

@ghost ghost self-assigned this Oct 24, 2017
@ghost ghost closed this as completed Oct 24, 2017
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

0 participants