Skip to content

Commit 218db85

Browse files
committed
Restored Line as str subclass.
Use 'slots' to avoid '__dict__'.
1 parent a098e69 commit 218db85

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

Lib/configparser.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -560,20 +560,15 @@ def __init__(self):
560560
self.errors = list()
561561

562562

563-
class _LineParser:
563+
class _Line(str):
564+
__slots__ = 'clean', 'has_comments'
564565

565-
def __init__(self, comments):
566-
self.comments = comments
566+
def __new__(cls, val, *args, **kwargs):
567+
return super().__new__(cls, val)
567568

568-
@property
569-
def value(self):
570-
return self._value
571-
572-
@value.setter
573-
def value(self, text):
574-
self._value = text
575-
trimmed = text.strip()
576-
self.clean = self.comments.strip(trimmed)
569+
def __init__(self, val, comments):
570+
trimmed = val.strip()
571+
self.clean = comments.strip(trimmed)
577572
self.has_comments = trimmed != self.clean
578573

579574

@@ -594,6 +589,9 @@ def __init__(self, full_prefixes, inline_prefixes):
594589
def strip(self, text):
595590
return self.pattern.sub('', text).rstrip()
596591

592+
def load(self, text):
593+
return _Line(text, self)
594+
597595

598596
class RawConfigParser(MutableMapping):
599597
"""ConfigParser that does not do interpolation."""
@@ -1063,9 +1061,8 @@ def _read(self, fp, fpname):
10631061

10641062
def _read_inner(self, fp, fpname):
10651063
st = _ReadState()
1066-
line = _LineParser(self._comments)
10671064

1068-
for st.lineno, line.value in enumerate(fp, start=1):
1065+
for st.lineno, line in enumerate(map(self._comments.load, fp), start=1):
10691066
if not line.clean:
10701067
if self._empty_lines_in_values:
10711068
# add empty line to the value, but only if there was no
@@ -1080,7 +1077,7 @@ def _read_inner(self, fp, fpname):
10801077
st.indent_level = sys.maxsize
10811078
continue
10821079

1083-
first_nonspace = self.NONSPACECRE.search(line.value)
1080+
first_nonspace = self.NONSPACECRE.search(line)
10841081
st.cur_indent_level = first_nonspace.start() if first_nonspace else 0
10851082

10861083
if self._handle_continuation_line(st, line, fpname):
@@ -1096,7 +1093,7 @@ def _handle_continuation_line(self, st, line, fpname):
10961093
st.cur_indent_level > st.indent_level)
10971094
if is_continue:
10981095
if st.cursect[st.optname] is None:
1099-
raise MultilineContinuationError(fpname, st.lineno, line.value)
1096+
raise MultilineContinuationError(fpname, st.lineno, line)
11001097
st.cursect[st.optname].append(line.clean)
11011098
return is_continue
11021099

@@ -1110,7 +1107,7 @@ def _handle_rest(self, st, line, fpname):
11101107
mo = self.SECTCRE.match(line.clean)
11111108

11121109
if not mo and st.cursect is None:
1113-
raise MissingSectionHeaderError(fpname, st.lineno, line.value)
1110+
raise MissingSectionHeaderError(fpname, st.lineno, line)
11141111

11151112
self._handle_header(st, mo.group('header'), fpname) if mo else self._handle_option(st, line, fpname)
11161113

@@ -1142,12 +1139,12 @@ def _handle_option(self, st, line, fpname):
11421139
# exception but keep going. the exception will be
11431140
# raised at the end of the file and will contain a
11441141
# list of all bogus lines
1145-
st.errors.append(ParsingError(fpname, st.lineno, line.value))
1142+
st.errors.append(ParsingError(fpname, st.lineno, line))
11461143
return
11471144

11481145
st.optname, vi, optval = mo.group('option', 'vi', 'value')
11491146
if not st.optname:
1150-
st.errors.append(ParsingError(fpname, st.lineno, line.value))
1147+
st.errors.append(ParsingError(fpname, st.lineno, line))
11511148
st.optname = self.optionxform(st.optname.rstrip())
11521149
if (self._strict and
11531150
(st.sectname, st.optname) in st.elements_added):

0 commit comments

Comments
 (0)