Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.7] bpo-18533: Avoid RuntimeError from repr() of recursive dictview [GH-4823] #5357

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 1 commit into from
Feb 26, 2018

Conversation

bennorth
Copy link
Contributor

@bennorth bennorth commented Jan 27, 2018

As suggested in a message on BPO, this is a backport of #4823 to the 2.7 branch. Compared to the changes introduced in #4823, this PR:

  • Works with the existing explicit seq_str in dictview_repr().
  • Uses PyString... (instead of PyUnicode...).
  • Sets up the test-case dictionaries to contain viewvalues() and viewitems() (instead of values() and items()).
  • Checks for RuntimeError (instead of RecursionError).
  • Updates the news entry accordingly.

I kept the test on OrderedDict even though 2.7 behaves correctly already; please let me know if it would be cleaner to omit this part.

https://bugs.python.org/issue18533

@matrixise
Copy link
Member

I have executed the tests with

./python -m test -R 3:3

and there is no reference leaks.

seq_str = PyObject_Repr(seq);
if (seq_str == NULL) {
Py_DECREF(seq);
return NULL;
goto Done_seq;
Copy link
Member

Choose a reason for hiding this comment

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

Py_DECREF(seq) could be called just after PyObject_Repr(). This will simplify the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for suggestion; that is indeed cleaner. I've just force-pushed a fresh commit including that change.

…on#4823)

dictview_repr(): Use a Py_ReprEnter() / Py_ReprLeave() pair to check
for recursion, and produce "..." if so.

test_recursive_repr(): Check for a string rather than a
RuntimeError.  (Test cannot be any tighter as contents are
implementation-dependent.)

test_deeply_nested_repr(): Add new test, replacing the original
test_recursive_repr().  It checks that a RuntimeError is raised in
the case of a non-recursive but deeply nested structure.

OrderedDictTests: Add new test case, to test behavior on OrderedDict
instances containing their own viewvalues() or viewitems().  This test
passes without the patch to dictview_repr(), but it failed in (at
least some versions of) Python 3, so including it here for
completeness.

(cherry picked from commit d7773d9)
@bennorth bennorth force-pushed the backport-d7773d9-2.7 branch from 5b4fdb5 to a27a0d4 Compare February 26, 2018 21:33
@bedevere-bot
Copy link

@serhiy-storchaka: Please replace # with GH- in the commit message next time. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants