Skip to content

Commit 0dd74a7

Browse files
committed
On Python 3.6 dicts retain insertion order, so, keys are no longer sorted in such cases when printing. Fixes #566
1 parent 2ebc4e7 commit 0dd74a7

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_safe_repr.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Gotten from ptvsd for supporting the format expected there.
66
import sys
7-
from _pydevd_bundle.pydevd_constants import IS_PY2
7+
from _pydevd_bundle.pydevd_constants import IS_PY2, IS_PY36_OR_GREATER
88
import locale
99
from _pydev_bundle import pydev_log
1010

@@ -248,10 +248,15 @@ def _repr_dict(self, obj, level, prefix, suffix,
248248
count = self.maxcollection[level]
249249
yield_comma = False
250250

251-
try:
252-
sorted_keys = sorted(obj)
253-
except Exception:
251+
if IS_PY36_OR_GREATER:
252+
# On Python 3.6 (onwards) dictionaries now keep
253+
# insertion order.
254254
sorted_keys = list(obj)
255+
else:
256+
try:
257+
sorted_keys = sorted(obj)
258+
except Exception:
259+
sorted_keys = list(obj)
255260

256261
for key in sorted_keys:
257262
if yield_comma:

src/debugpy/_vendored/pydevd/tests_python/test_safe_repr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66
from _pydevd_bundle.pydevd_safe_repr import SafeRepr
77
import json
8-
from _pydevd_bundle.pydevd_constants import IS_JYTHON, IS_PY2
8+
from _pydevd_bundle.pydevd_constants import IS_JYTHON, IS_PY2, IS_PY36_OR_GREATER
99

1010
try:
1111
import numpy as np
@@ -400,7 +400,10 @@ def test_sorted(self):
400400
d1['c'] = None
401401
d1['b'] = None
402402
d1['a'] = None
403-
self.assert_saferepr(d1, "{'a': None, 'b': None, 'c': None}")
403+
if IS_PY36_OR_GREATER:
404+
self.assert_saferepr(d1, "{'c': None, 'b': None, 'a': None}")
405+
else:
406+
self.assert_saferepr(d1, "{'a': None, 'b': None, 'c': None}")
404407

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

0 commit comments

Comments
 (0)