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

Skip to content

Commit 2a37f9f

Browse files
committed
Allow spaces in section names.
Do not expose the __name__ when reporting the list of options available for a section since that is for internal use. This closes SourceForge bug #115357. Additionally, define InterpolationDepthError and MAX_INTERPOLATION_DEPTH. The exception is raised by get*() when value interpolation cannot be completed within the defined recursion limit. The constant is only informative; changing it will not affect the allowed depth. Fix the exit from get() so that None is not returned if the depth is met or exceeded; either return the value of raise InterpolationDepthError.
1 parent 6372fe1 commit 2a37f9f

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

Lib/ConfigParser.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191

9292
DEFAULTSECT = "DEFAULT"
9393

94+
MAX_INTERPOLATION_DEPTH = 10
95+
9496

9597

9698
# exception classes
@@ -130,15 +132,16 @@ def __init__(self, reference, option, section, rawval):
130132
self.option = option
131133
self.section = section
132134

133-
class MissingSectionHeaderError(Error):
134-
def __init__(self, filename, lineno, line):
135-
Error.__init__(
136-
self,
137-
'File contains no section headers.\nfile: %s, line: %d\n%s' %
138-
(filename, lineno, line))
139-
self.filename = filename
140-
self.lineno = lineno
141-
self.line = line
135+
class InterpolationDepthError(Error):
136+
def __init__(self, option, section, rawval):
137+
Error.__init__(self,
138+
"Value interpolation too deeply recursive:\n"
139+
"\tsection: [%s]\n"
140+
"\toption : %s\n"
141+
"\trawval : %s\n"
142+
% (section, option, rawval))
143+
self.option = option
144+
self.section = section
142145

143146
class ParsingError(Error):
144147
def __init__(self, filename):
@@ -150,6 +153,16 @@ def append(self, lineno, line):
150153
self.errors.append((lineno, line))
151154
self._msg = self._msg + '\n\t[line %2d]: %s' % (lineno, line)
152155

156+
class MissingSectionHeaderError(ParsingError):
157+
def __init__(self, filename, lineno, line):
158+
Error.__init__(
159+
self,
160+
'File contains no section headers.\nfile: %s, line: %d\n%s' %
161+
(filename, lineno, line))
162+
self.filename = filename
163+
self.lineno = lineno
164+
self.line = line
165+
153166

154167

155168
class ConfigParser:
@@ -183,7 +196,7 @@ def has_section(self, section):
183196
184197
The DEFAULT section is not acknowledged.
185198
"""
186-
return self.__sections.has_key(section)
199+
return section in self.sections()
187200

188201
def options(self, section):
189202
"""Return a list of option names for the given section name."""
@@ -192,15 +205,13 @@ def options(self, section):
192205
except KeyError:
193206
raise NoSectionError(section)
194207
opts.update(self.__defaults)
208+
if opts.has_key('__name__'):
209+
del opts['__name__']
195210
return opts.keys()
196211

197212
def has_option(self, section, option):
198213
"""Return whether the given section has the given option."""
199-
try:
200-
opts = self.__sections[section]
201-
except KeyError:
202-
raise NoSectionError(section)
203-
return opts.has_key(option)
214+
return option in self.options(section)
204215

205216
def read(self, filenames):
206217
"""Read and parse a filename or a list of filenames.
@@ -266,10 +277,11 @@ def get(self, section, option, raw=0, vars=None):
266277
rawval = d[option]
267278
except KeyError:
268279
raise NoOptionError(option, section)
269-
# do the string interpolation
280+
270281
if raw:
271282
return rawval
272283

284+
# do the string interpolation
273285
value = rawval # Make it a pretty variable name
274286
depth = 0
275287
while depth < 10: # Loop through this until it's done
@@ -280,7 +292,10 @@ def get(self, section, option, raw=0, vars=None):
280292
except KeyError, key:
281293
raise InterpolationError(key, option, section, rawval)
282294
else:
283-
return value
295+
break
296+
if value.find("%(") >= 0:
297+
raise InterpolationDepthError(option, section, rawval)
298+
return value
284299

285300
def __get(self, section, conv, option):
286301
return conv(self.get(section, option))
@@ -365,7 +380,7 @@ def remove_section(self, section):
365380
# of \w, _ is allowed in section header names.
366381
SECTCRE = re.compile(
367382
r'\[' # [
368-
r'(?P<header>[-\w_.*,(){}]+)' # a lot of stuff found by IvL
383+
r'(?P<header>[-\w_.*,(){} ]+)' # a lot of stuff found by IvL
369384
r'\]' # ]
370385
)
371386
OPTCRE = re.compile(

0 commit comments

Comments
 (0)