Skip to content

Commit

Permalink
On Python 3.6 dicts retain insertion order, so, keys are no longer so…
Browse files Browse the repository at this point in the history
…rted in such cases when printing. Fixes #566
  • Loading branch information
fabioz committed Mar 25, 2021
1 parent 2ebc4e7 commit 0dd74a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_safe_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Gotten from ptvsd for supporting the format expected there.
import sys
from _pydevd_bundle.pydevd_constants import IS_PY2
from _pydevd_bundle.pydevd_constants import IS_PY2, IS_PY36_OR_GREATER
import locale
from _pydev_bundle import pydev_log

Expand Down Expand Up @@ -248,10 +248,15 @@ def _repr_dict(self, obj, level, prefix, suffix,
count = self.maxcollection[level]
yield_comma = False

try:
sorted_keys = sorted(obj)
except Exception:
if IS_PY36_OR_GREATER:
# On Python 3.6 (onwards) dictionaries now keep
# insertion order.
sorted_keys = list(obj)
else:
try:
sorted_keys = sorted(obj)
except Exception:
sorted_keys = list(obj)

for key in sorted_keys:
if yield_comma:
Expand Down
7 changes: 5 additions & 2 deletions src/debugpy/_vendored/pydevd/tests_python/test_safe_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from _pydevd_bundle.pydevd_safe_repr import SafeRepr
import json
from _pydevd_bundle.pydevd_constants import IS_JYTHON, IS_PY2
from _pydevd_bundle.pydevd_constants import IS_JYTHON, IS_PY2, IS_PY36_OR_GREATER

try:
import numpy as np
Expand Down Expand Up @@ -400,7 +400,10 @@ def test_sorted(self):
d1['c'] = None
d1['b'] = None
d1['a'] = None
self.assert_saferepr(d1, "{'a': None, 'b': None, 'c': None}")
if IS_PY36_OR_GREATER:
self.assert_saferepr(d1, "{'c': None, 'b': None, 'a': None}")
else:
self.assert_saferepr(d1, "{'a': None, 'b': None, 'c': None}")

@pytest.mark.skipif(sys.version_info < (3, 0), reason='Py3 specific test')
def test_unsortable_keys(self):
Expand Down

0 comments on commit 0dd74a7

Please sign in to comment.