Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 0 additions & 40 deletions Lib/idlelib/idle_test/test_pyparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,6 @@
from idlelib import pyparse


class StringTranslatePseudoMappingTest(unittest.TestCase):

@classmethod
def setUpClass(cls):
whitespace_chars = ' \t\n\r'
cls.preserve_dict = {ord(c): ord(c) for c in whitespace_chars}
cls.default = ord('x')
cls.mapping = pyparse.StringTranslatePseudoMapping(
cls.preserve_dict, default_value=ord('x'))

@classmethod
def tearDownClass(cls):
del cls.preserve_dict, cls.default, cls.mapping

def test__init__(self):
m = self.mapping
self.assertEqual(m._non_defaults, self.preserve_dict)
self.assertEqual(m._default_value, self.default)

def test__get_item__(self):
self.assertEqual(self.mapping[ord('\t')], ord('\t'))
self.assertEqual(self.mapping[ord('a')], self.default)

def test__len__(self):
self.assertEqual(len(self.mapping), len(self.preserve_dict))

def test__iter__(self):
count = 0
for key, value in self.mapping.items():
self.assertIn(key, self.preserve_dict)
count += 1
self.assertEqual(count, len(self.mapping))

def test_get(self):
self.assertEqual(self.mapping.get(ord('\t')), ord('\t'))
self.assertEqual(self.mapping.get('a'), self.default)
# Default is a parameter, but it isn't used.
self.assertEqual(self.mapping.get('a', default=500), self.default)


class PyParseTest(unittest.TestCase):

@classmethod
Expand Down
47 changes: 2 additions & 45 deletions Lib/idlelib/pyparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
_closere - line that must be followed by dedent.
_chew_ordinaryre - non-special characters.
"""
from collections.abc import Mapping
from collections import defaultdict
import re
import sys

Expand Down Expand Up @@ -101,48 +101,6 @@
""", re.VERBOSE).match


class StringTranslatePseudoMapping(Mapping):
r"""Utility class to be used with str.translate()

This Mapping class wraps a given dict. When a value for a key is
requested via __getitem__() or get(), the key is looked up in the
given dict. If found there, the value from the dict is returned.
Otherwise, the default value given upon initialization is returned.

This allows using str.translate() to make some replacements, and to
replace all characters for which no replacement was specified with
a given character instead of leaving them as-is.

For example, to replace everything except whitespace with 'x':

>>> whitespace_chars = ' \t\n\r'
>>> preserve_dict = {ord(c): ord(c) for c in whitespace_chars}
>>> mapping = StringTranslatePseudoMapping(preserve_dict, ord('x'))
>>> text = "a + b\tc\nd"
>>> text.translate(mapping)
'x x x\tx\nx'
"""
def __init__(self, non_defaults, default_value):
self._non_defaults = non_defaults
self._default_value = default_value

def _get(key, _get=non_defaults.get, _default=default_value):
return _get(key, _default)
self._get = _get

def __getitem__(self, item):
return self._get(item)

def __len__(self):
return len(self._non_defaults)

def __iter__(self):
return iter(self._non_defaults)

def get(self, key, default=None):
return self._get(key)


class Parser:

def __init__(self, indentwidth, tabwidth):
Expand Down Expand Up @@ -228,11 +186,10 @@ def set_lo(self, lo):
# brackets to '(', close brackets to ')' while preserving quotes,
# backslashes, newlines and hashes. This is to be passed to
# str.translate() in _study1().
_tran = {}
_tran = defaultdict(lambda: 'x')
_tran.update((ord(c), ord('(')) for c in "({[")
_tran.update((ord(c), ord(')')) for c in ")}]")
_tran.update((ord(c), ord(c)) for c in "\"'\\\n#")
_tran = StringTranslatePseudoMapping(_tran, default_value=ord('x'))

def _study1(self):
"""Find the line numbers of non-continuation lines.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Replace StringTranslatePseudoMapping with a defaultdict.