Skip to content

bpo-31455: avoid calling "PyObject_GetAttrString()" with a live exception set #3545

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

Merged
merged 5 commits into from
Sep 14, 2017

Conversation

scoder
Copy link
Contributor

@scoder scoder commented Sep 13, 2017

@serhiy-storchaka
Copy link
Member

Please open an issue on the bug tracker. This isn't a trivial change.

I think that it would be better to silence only AttributeError.

@scoder scoder changed the title Avoid calling "PyObject_GetAttrString()" with a live exception set. bpo-31455: avoid calling "PyObject_GetAttrString()" with a live exception set Sep 13, 2017
@scoder
Copy link
Contributor Author

scoder commented Sep 13, 2017

Found as part of #3279, which started showing side-effects due to this bug.

@scoder
Copy link
Contributor Author

scoder commented Sep 13, 2017

PR updated, test and News entry added.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I left just few nitpicks.


ET.XMLParser(target=RaisingBuilder(what=AttributeError))
for event in ('start', 'data', 'end', 'comment', 'pi'):
ET.XMLParser(target=RaisingBuilder(event, what=AttributeError))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Close the parser explicitly. Just for the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. (Requires parsing something, though.)

@@ -2400,6 +2400,29 @@ def _check_sample1_element(self, e):
self.assertEqual(child.tail, 'tail')
self.assertEqual(child.attrib, {})

def test_exceptions(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't XMLParserTest be better place? In any case I think it is better to place this method at the end of the class, after testing normal cases.

And maybe use more specific method name? "test_builder_attribute_errors" or something like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I kept it in the test class because the exceptions are related to the behaviour of the TreeBuilder.

@@ -3259,6 +3259,17 @@ xmlparser_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return (PyObject *)self;
}

static int
ignore_attribute_error(PyObject *value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this { on new line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -3313,14 +3324,26 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *html,
self->target = target;

self->handle_start = PyObject_GetAttrString(target, "start");
if (ignore_attribute_error(self->handle_start))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New PEP 7 rules require braces even around simple if body.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. (Sad in this case, given the relation between "active" code and error handling code.)

@scoder
Copy link
Contributor Author

scoder commented Sep 14, 2017

PR updated.

@serhiy-storchaka serhiy-storchaka added needs backport to 2.7 type-bug An unexpected behavior, bug, or error labels Sep 14, 2017
@serhiy-storchaka serhiy-storchaka merged commit c8d8e15 into python:master Sep 14, 2017
@miss-islington
Copy link
Contributor

Thanks @scoder for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @scoder and @serhiy-storchaka, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 14, 2017
…pythonGH-3545)

* Avoid calling "PyObject_GetAttrString()" (and potentially executing user code) with a live exception set.

* Ignore only AttributeError on attribute lookups in ElementTree.XMLParser() and propagate all other exceptions.
(cherry picked from commit c8d8e15)
@bedevere-bot
Copy link

GH-3585 is a backport of this pull request to the 3.6 branch.

serhiy-storchaka pushed a commit that referenced this pull request Sep 14, 2017
…GH-3545) (#3585)

* Avoid calling "PyObject_GetAttrString()" (and potentially executing user code) with a live exception set.

* Ignore only AttributeError on attribute lookups in ElementTree.XMLParser() and propagate all other exceptions.
(cherry picked from commit c8d8e15)
@miss-islington
Copy link
Contributor

Thanks @scoder for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Sorry, @scoder and @serhiy-storchaka, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker c8d8e15bfc24abeeaaf3d8be9073276b0c011cdf 2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants