| 1 |
import collections |
| 2 |
import configparser |
| 3 |
import io |
| 4 |
import os |
| 5 |
import textwrap |
| 6 |
import unittest |
| 7 |
|
| 8 |
from test import support |
| 9 |
from test.support import os_helper |
| 10 |
|
| 11 |
|
| 12 |
class SortedDict(collections.UserDict): |
| 13 |
|
| 14 |
def items(self): |
| 15 |
return sorted(self.data.items()) |
| 16 |
|
| 17 |
def keys(self): |
| 18 |
return sorted(self.data.keys()) |
| 19 |
|
| 20 |
def values(self): |
| 21 |
return [i[1] for i in self.items()] |
| 22 |
|
| 23 |
def iteritems(self): |
| 24 |
return iter(self.items()) |
| 25 |
|
| 26 |
def iterkeys(self): |
| 27 |
return iter(self.keys()) |
| 28 |
|
| 29 |
def itervalues(self): |
| 30 |
return iter(self.values()) |
| 31 |
|
| 32 |
__iter__ = iterkeys |
| 33 |
|
| 34 |
|
| 35 |
class CfgParserTestCaseClass: |
| 36 |
allow_no_value = False |
| 37 |
delimiters = ('=', ':') |
| 38 |
comment_prefixes = (';', '#') |
| 39 |
inline_comment_prefixes = (';', '#') |
| 40 |
empty_lines_in_values = True |
| 41 |
dict_type = configparser._default_dict |
| 42 |
strict = False |
| 43 |
default_section = configparser.DEFAULTSECT |
| 44 |
interpolation = configparser._UNSET |
| 45 |
|
| 46 |
def newconfig(self, defaults=None): |
| 47 |
arguments = dict( |
| 48 |
defaults=defaults, |
| 49 |
allow_no_value=self.allow_no_value, |
| 50 |
delimiters=self.delimiters, |
| 51 |
comment_prefixes=self.comment_prefixes, |
| 52 |
inline_comment_prefixes=self.inline_comment_prefixes, |
| 53 |
empty_lines_in_values=self.empty_lines_in_values, |
| 54 |
dict_type=self.dict_type, |
| 55 |
strict=self.strict, |
| 56 |
default_section=self.default_section, |
| 57 |
interpolation=self.interpolation, |
| 58 |
) |
| 59 |
instance = self.config_class(**arguments) |
| 60 |
return instance |
| 61 |
|
| 62 |
def fromstring(self, string, defaults=None): |
| 63 |
cf = self.newconfig(defaults) |
| 64 |
cf.read_string(string) |
| 65 |
return cf |
| 66 |
|
| 67 |
|
| 68 |
class BasicTestCase(CfgParserTestCaseClass): |
| 69 |
|
| 70 |
def basic_test(self, cf): |
| 71 |
E = ['Commented Bar', |
| 72 |
'Foo Bar', |
| 73 |
'Internationalized Stuff', |
| 74 |
'Long Line', |
| 75 |
'Section\\with$weird%characters[\t', |
| 76 |
'Spaces', |
| 77 |
'Spacey Bar', |
| 78 |
'Spacey Bar From The Beginning', |
| 79 |
'Types', |
| 80 |
'This One Has A ] In It', |
| 81 |
] |
| 82 |
|
| 83 |
if self.allow_no_value: |
| 84 |
E.append('NoValue') |
| 85 |
E.sort() |
| 86 |
F = [('baz', 'qwe'), ('foo', 'bar3')] |
| 87 |
|
| 88 |
# API access |
| 89 |
L = cf.sections() |
| 90 |
L.sort() |
| 91 |
eq = self.assertEqual |
| 92 |
eq(L, E) |
| 93 |
L = cf.items('Spacey Bar From The Beginning') |
| 94 |
L.sort() |
| 95 |
eq(L, F) |
| 96 |
|
| 97 |
# mapping access |
| 98 |
L = [section for section in cf] |
| 99 |
L.sort() |
| 100 |
E.append(self.default_section) |
| 101 |
E.sort() |
| 102 |
eq(L, E) |
| 103 |
L = cf['Spacey Bar From The Beginning'].items() |
| 104 |
L = sorted(list(L)) |
| 105 |
eq(L, F) |
| 106 |
L = cf.items() |
| 107 |
L = sorted(list(L)) |
| 108 |
self.assertEqual(len(L), len(E)) |
| 109 |
for name, section in L: |
| 110 |
eq(name, section.name) |
| 111 |
eq(cf.defaults(), cf[self.default_section]) |
| 112 |
|
| 113 |
# The use of spaces in the section names serves as a |
| 114 |
# regression test for SourceForge bug #583248: |
| 115 |
# https://bugs.python.org/issue583248 |
| 116 |
|
| 117 |
# API access |
| 118 |
eq(cf.get('Foo Bar', 'foo'), 'bar1') |
| 119 |
eq(cf.get('Spacey Bar', 'foo'), 'bar2') |
| 120 |
eq(cf.get('Spacey Bar From The Beginning', 'foo'), 'bar3') |
| 121 |
eq(cf.get('Spacey Bar From The Beginning', 'baz'), 'qwe') |
| 122 |
eq(cf.get('Commented Bar', 'foo'), 'bar4') |
| 123 |
eq(cf.get('Commented Bar', 'baz'), 'qwe') |
| 124 |
eq(cf.get('Spaces', 'key with spaces'), 'value') |
| 125 |
eq(cf.get('Spaces', 'another with spaces'), 'splat!') |
| 126 |
eq(cf.getint('Types', 'int'), 42) |
| 127 |
eq(cf.get('Types', 'int'), "42") |
| 128 |
self.assertAlmostEqual(cf.getfloat('Types', 'float'), 0.44) |
| 129 |
eq(cf.get('Types', 'float'), "0.44") |
| 130 |
eq(cf.getboolean('Types', 'boolean'), False) |
| 131 |
eq(cf.get('Types', '123'), 'strange but acceptable') |
| 132 |
eq(cf.get('This One Has A ] In It', 'forks'), 'spoons') |
| 133 |
if self.allow_no_value: |
| 134 |
eq(cf.get('NoValue', 'option-without-value'), None) |
| 135 |
|
| 136 |
# test vars= and fallback= |
| 137 |
eq(cf.get('Foo Bar', 'foo', fallback='baz'), 'bar1') |
| 138 |
eq(cf.get('Foo Bar', 'foo', vars={'foo': 'baz'}), 'baz') |
| 139 |
with self.assertRaises(configparser.NoSectionError): |
| 140 |
cf.get('No Such Foo Bar', 'foo') |
| 141 |
with self.assertRaises(configparser.NoOptionError): |
| 142 |
cf.get('Foo Bar', 'no-such-foo') |
| 143 |
eq(cf.get('No Such Foo Bar', 'foo', fallback='baz'), 'baz') |
| 144 |
eq(cf.get('Foo Bar', 'no-such-foo', fallback='baz'), 'baz') |
| 145 |
eq(cf.get('Spacey Bar', 'foo', fallback=None), 'bar2') |
| 146 |
eq(cf.get('No Such Spacey Bar', 'foo', fallback=None), None) |
| 147 |
eq(cf.getint('Types', 'int', fallback=18), 42) |
| 148 |
eq(cf.getint('Types', 'no-such-int', fallback=18), 18) |
| 149 |
eq(cf.getint('Types', 'no-such-int', fallback="18"), "18") # sic! |
| 150 |
with self.assertRaises(configparser.NoOptionError): |
| 151 |
cf.getint('Types', 'no-such-int') |
| 152 |
self.assertAlmostEqual(cf.getfloat('Types', 'float', |
| 153 |
fallback=0.0), 0.44) |
| 154 |
self.assertAlmostEqual(cf.getfloat('Types', 'no-such-float', |
| 155 |
fallback=0.0), 0.0) |
| 156 |
eq(cf.getfloat('Types', 'no-such-float', fallback="0.0"), "0.0") # sic! |
| 157 |
with self.assertRaises(configparser.NoOptionError): |
| 158 |
cf.getfloat('Types', 'no-such-float') |
| 159 |
eq(cf.getboolean('Types', 'boolean', fallback=True), False) |
| 160 |
eq(cf.getboolean('Types', 'no-such-boolean', fallback="yes"), |
| 161 |
"yes") # sic! |
| 162 |
eq(cf.getboolean('Types', 'no-such-boolean', fallback=True), True) |
| 163 |
with self.assertRaises(configparser.NoOptionError): |
| 164 |
cf.getboolean('Types', 'no-such-boolean') |
| 165 |
eq(cf.getboolean('No Such Types', 'boolean', fallback=True), True) |
| 166 |
if self.allow_no_value: |
| 167 |
eq(cf.get('NoValue', 'option-without-value', fallback=False), None) |
| 168 |
eq(cf.get('NoValue', 'no-such-option-without-value', |
| 169 |
fallback=False), False) |
| 170 |
|
| 171 |
# mapping access |
| 172 |
eq(cf['Foo Bar']['foo'], 'bar1') |
| 173 |
eq(cf['Spacey Bar']['foo'], 'bar2') |
| 174 |
section = cf['Spacey Bar From The Beginning'] |
| 175 |
eq(section.name, 'Spacey Bar From The Beginning') |
| 176 |
self.assertIs(section.parser, cf) |
| 177 |
with self.assertRaises(AttributeError): |
| 178 |
section.name = 'Name is read-only' |
| 179 |
with self.assertRaises(AttributeError): |
| 180 |
section.parser = 'Parser is read-only' |
| 181 |
eq(section['foo'], 'bar3') |
| 182 |
eq(section['baz'], 'qwe') |
| 183 |
eq(cf['Commented Bar']['foo'], 'bar4') |
| 184 |
eq(cf['Commented Bar']['baz'], 'qwe') |
| 185 |
eq(cf['Spaces']['key with spaces'], 'value') |
| 186 |
eq(cf['Spaces']['another with spaces'], 'splat!') |
| 187 |
eq(cf['Long Line']['foo'], |
| 188 |
'this line is much, much longer than my editor\nlikes it.') |
| 189 |
if self.allow_no_value: |
| 190 |
eq(cf['NoValue']['option-without-value'], None) |
| 191 |
# test vars= and fallback= |
| 192 |
eq(cf['Foo Bar'].get('foo', 'baz'), 'bar1') |
| 193 |
eq(cf['Foo Bar'].get('foo', fallback='baz'), 'bar1') |
| 194 |
eq(cf['Foo Bar'].get('foo', vars={'foo': 'baz'}), 'baz') |
| 195 |
with self.assertRaises(KeyError): |
| 196 |
cf['No Such Foo Bar']['foo'] |
| 197 |
with self.assertRaises(KeyError): |
| 198 |
cf['Foo Bar']['no-such-foo'] |
| 199 |
with self.assertRaises(KeyError): |
| 200 |
cf['No Such Foo Bar'].get('foo', fallback='baz') |
| 201 |
eq(cf['Foo Bar'].get('no-such-foo', 'baz'), 'baz') |
| 202 |
eq(cf['Foo Bar'].get('no-such-foo', fallback='baz'), 'baz') |
| 203 |
eq(cf['Foo Bar'].get('no-such-foo'), None) |
| 204 |
eq(cf['Spacey Bar'].get('foo', None), 'bar2') |
| 205 |
eq(cf['Spacey Bar'].get('foo', fallback=None), 'bar2') |
| 206 |
with self.assertRaises(KeyError): |
| 207 |
cf['No Such Spacey Bar'].get('foo', None) |
| 208 |
eq(cf['Types'].getint('int', 18), 42) |
| 209 |
eq(cf['Types'].getint('int', fallback=18), 42) |
| 210 |
eq(cf['Types'].getint('no-such-int', 18), 18) |
| 211 |
eq(cf['Types'].getint('no-such-int', fallback=18), 18) |
| 212 |
eq(cf['Types'].getint('no-such-int', "18"), "18") # sic! |
| 213 |
eq(cf['Types'].getint('no-such-int', fallback="18"), "18") # sic! |
| 214 |
eq(cf['Types'].getint('no-such-int'), None) |
| 215 |
self.assertAlmostEqual(cf['Types'].getfloat('float', 0.0), 0.44) |
| 216 |
self.assertAlmostEqual(cf['Types'].getfloat('float', |
| 217 |
fallback=0.0), 0.44) |
| 218 |
self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', 0.0), 0.0) |
| 219 |
self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', |
| 220 |
fallback=0.0), 0.0) |
| 221 |
eq(cf['Types'].getfloat('no-such-float', "0.0"), "0.0") # sic! |
| 222 |
eq(cf['Types'].getfloat('no-such-float', fallback="0.0"), "0.0") # sic! |
| 223 |
eq(cf['Types'].getfloat('no-such-float'), None) |
| 224 |
eq(cf['Types'].getboolean('boolean', True), False) |
| 225 |
eq(cf['Types'].getboolean('boolean', fallback=True), False) |
| 226 |
eq(cf['Types'].getboolean('no-such-boolean', "yes"), "yes") # sic! |
| 227 |
eq(cf['Types'].getboolean('no-such-boolean', fallback="yes"), |
| 228 |
"yes") # sic! |
| 229 |
eq(cf['Types'].getboolean('no-such-boolean', True), True) |
| 230 |
eq(cf['Types'].getboolean('no-such-boolean', fallback=True), True) |
| 231 |
eq(cf['Types'].getboolean('no-such-boolean'), None) |
| 232 |
if self.allow_no_value: |
| 233 |
eq(cf['NoValue'].get('option-without-value', False), None) |
| 234 |
eq(cf['NoValue'].get('option-without-value', fallback=False), None) |
| 235 |
eq(cf['NoValue'].get('no-such-option-without-value', False), False) |
| 236 |
eq(cf['NoValue'].get('no-such-option-without-value', |
| 237 |
fallback=False), False) |
| 238 |
|
| 239 |
# Make sure the right things happen for remove_section() and |
| 240 |
# remove_option(); added to include check for SourceForge bug #123324. |
| 241 |
|
| 242 |
cf[self.default_section]['this_value'] = '1' |
| 243 |
cf[self.default_section]['that_value'] = '2' |
| 244 |
|
| 245 |
# API access |
| 246 |
self.assertTrue(cf.remove_section('Spaces')) |
| 247 |
self.assertFalse(cf.has_option('Spaces', 'key with spaces')) |
| 248 |
self.assertFalse(cf.remove_section('Spaces')) |
| 249 |
self.assertFalse(cf.remove_section(self.default_section)) |
| 250 |
self.assertTrue(cf.remove_option('Foo Bar', 'foo'), |
| 251 |
"remove_option() failed to report existence of option") |
| 252 |
self.assertFalse(cf.has_option('Foo Bar', 'foo'), |
| 253 |
"remove_option() failed to remove option") |
| 254 |
self.assertFalse(cf.remove_option('Foo Bar', 'foo'), |
| 255 |
"remove_option() failed to report non-existence of option" |
| 256 |
" that was removed") |
| 257 |
self.assertTrue(cf.has_option('Foo Bar', 'this_value')) |
| 258 |
self.assertFalse(cf.remove_option('Foo Bar', 'this_value')) |
| 259 |
self.assertTrue(cf.remove_option(self.default_section, 'this_value')) |
| 260 |
self.assertFalse(cf.has_option('Foo Bar', 'this_value')) |
| 261 |
self.assertFalse(cf.remove_option(self.default_section, 'this_value')) |
| 262 |
|
| 263 |
with self.assertRaises(configparser.NoSectionError) as cm: |
| 264 |
cf.remove_option('No Such Section', 'foo') |
| 265 |
self.assertEqual(cm.exception.args, ('No Such Section',)) |
| 266 |
|
| 267 |
eq(cf.get('Long Line', 'foo'), |
| 268 |
'this line is much, much longer than my editor\nlikes it.') |
| 269 |
|
| 270 |
# mapping access |
| 271 |
del cf['Types'] |
| 272 |
self.assertFalse('Types' in cf) |
| 273 |
with self.assertRaises(KeyError): |
| 274 |
del cf['Types'] |
| 275 |
with self.assertRaises(ValueError): |
| 276 |
del cf[self.default_section] |
| 277 |
del cf['Spacey Bar']['foo'] |
| 278 |
self.assertFalse('foo' in cf['Spacey Bar']) |
| 279 |
with self.assertRaises(KeyError): |
| 280 |
del cf['Spacey Bar']['foo'] |
| 281 |
self.assertTrue('that_value' in cf['Spacey Bar']) |
| 282 |
with self.assertRaises(KeyError): |
| 283 |
del cf['Spacey Bar']['that_value'] |
| 284 |
del cf[self.default_section]['that_value'] |
| 285 |
self.assertFalse('that_value' in cf['Spacey Bar']) |
| 286 |
with self.assertRaises(KeyError): |
| 287 |
del cf[self.default_section]['that_value'] |
| 288 |
with self.assertRaises(KeyError): |
| 289 |
del cf['No Such Section']['foo'] |
| 290 |
|
| 291 |
# Don't add new asserts below in this method as most of the options |
| 292 |
# and sections are now removed. |
| 293 |
|
| 294 |
def test_basic(self): |
| 295 |
config_string = """\ |
| 296 |
[Foo Bar] |
| 297 |
foo{0[0]}bar1 |
| 298 |
[Spacey Bar] |
| 299 |
foo {0[0]} bar2 |
| 300 |
[Spacey Bar From The Beginning] |
| 301 |
foo {0[0]} bar3 |
| 302 |
baz {0[0]} qwe |
| 303 |
[Commented Bar] |
| 304 |
foo{0[1]} bar4 {1[1]} comment |
| 305 |
baz{0[0]}qwe {1[0]}another one |
| 306 |
[Long Line] |
| 307 |
foo{0[1]} this line is much, much longer than my editor |
| 308 |
likes it. |
| 309 |
[Section\\with$weird%characters[\t] |
| 310 |
[Internationalized Stuff] |
| 311 |
foo[bg]{0[1]} Bulgarian |
| 312 |
foo{0[0]}Default |
| 313 |
foo[en]{0[0]}English |
| 314 |
foo[de]{0[0]}Deutsch |
| 315 |
[Spaces] |
| 316 |
key with spaces {0[1]} value |
| 317 |
another with spaces {0[0]} splat! |
| 318 |
[Types] |
| 319 |
int {0[1]} 42 |
| 320 |
float {0[0]} 0.44 |
| 321 |
boolean {0[0]} NO |
| 322 |
123 {0[1]} strange but acceptable |
| 323 |
[This One Has A ] In It] |
| 324 |
forks {0[0]} spoons |
| 325 |
""".format(self.delimiters, self.comment_prefixes) |
| 326 |
if self.allow_no_value: |
| 327 |
config_string += ( |
| 328 |
"[NoValue]\n" |
| 329 |
"option-without-value\n" |
| 330 |
) |
| 331 |
cf = self.fromstring(config_string) |
| 332 |
self.basic_test(cf) |
| 333 |
if self.strict: |
| 334 |
with self.assertRaises(configparser.DuplicateOptionError): |
| 335 |
cf.read_string(textwrap.dedent("""\ |
| 336 |
[Duplicate Options Here] |
| 337 |
option {0[0]} with a value |
| 338 |
option {0[1]} with another value |
| 339 |
""".format(self.delimiters))) |
| 340 |
with self.assertRaises(configparser.DuplicateSectionError): |
| 341 |
cf.read_string(textwrap.dedent("""\ |
| 342 |
[And Now For Something] |
| 343 |
completely different {0[0]} True |
| 344 |
[And Now For Something] |
| 345 |
the larch {0[1]} 1 |
| 346 |
""".format(self.delimiters))) |
| 347 |
else: |
| 348 |
cf.read_string(textwrap.dedent("""\ |
| 349 |
[Duplicate Options Here] |
| 350 |
option {0[0]} with a value |
| 351 |
option {0[1]} with another value |
| 352 |
""".format(self.delimiters))) |
| 353 |
|
| 354 |
cf.read_string(textwrap.dedent("""\ |
| 355 |
[And Now For Something] |
| 356 |
completely different {0[0]} True |
| 357 |
[And Now For Something] |
| 358 |
the larch {0[1]} 1 |
| 359 |
""".format(self.delimiters))) |
| 360 |
|
| 361 |
def test_basic_from_dict(self): |
| 362 |
config = { |
| 363 |
"Foo Bar": { |
| 364 |
"foo": "bar1", |
| 365 |
}, |
| 366 |
"Spacey Bar": { |
| 367 |
"foo": "bar2", |
| 368 |
}, |
| 369 |
"Spacey Bar From The Beginning": { |
| 370 |
"foo": "bar3", |
| 371 |
"baz": "qwe", |
| 372 |
}, |
| 373 |
"Commented Bar": { |
| 374 |
"foo": "bar4", |
| 375 |
"baz": "qwe", |
| 376 |
}, |
| 377 |
"Long Line": { |
| 378 |
"foo": "this line is much, much longer than my editor\nlikes " |
| 379 |
"it.", |
| 380 |
}, |
| 381 |
"Section\\with$weird%characters[\t": { |
| 382 |
}, |
| 383 |
"Internationalized Stuff": { |
| 384 |
"foo[bg]": "Bulgarian", |
| 385 |
"foo": "Default", |
| 386 |
"foo[en]": "English", |
| 387 |
"foo[de]": "Deutsch", |
| 388 |
}, |
| 389 |
"Spaces": { |
| 390 |
"key with spaces": "value", |
| 391 |
"another with spaces": "splat!", |
| 392 |
}, |
| 393 |
"Types": { |
| 394 |
"int": 42, |
| 395 |
"float": 0.44, |
| 396 |
"boolean": False, |
| 397 |
123: "strange but acceptable", |
| 398 |
}, |
| 399 |
"This One Has A ] In It": { |
| 400 |
"forks": "spoons" |
| 401 |
}, |
| 402 |
} |
| 403 |
if self.allow_no_value: |
| 404 |
config.update({ |
| 405 |
"NoValue": { |
| 406 |
"option-without-value": None, |
| 407 |
} |
| 408 |
}) |
| 409 |
cf = self.newconfig() |
| 410 |
cf.read_dict(config) |
| 411 |
self.basic_test(cf) |
| 412 |
if self.strict: |
| 413 |
with self.assertRaises(configparser.DuplicateSectionError): |
| 414 |
cf.read_dict({ |
| 415 |
'1': {'key': 'value'}, |
| 416 |
1: {'key2': 'value2'}, |
| 417 |
}) |
| 418 |
with self.assertRaises(configparser.DuplicateOptionError): |
| 419 |
cf.read_dict({ |
| 420 |
"Duplicate Options Here": { |
| 421 |
'option': 'with a value', |
| 422 |
'OPTION': 'with another value', |
| 423 |
}, |
| 424 |
}) |
| 425 |
else: |
| 426 |
cf.read_dict({ |
| 427 |
'section': {'key': 'value'}, |
| 428 |
'SECTION': {'key2': 'value2'}, |
| 429 |
}) |
| 430 |
cf.read_dict({ |
| 431 |
"Duplicate Options Here": { |
| 432 |
'option': 'with a value', |
| 433 |
'OPTION': 'with another value', |
| 434 |
}, |
| 435 |
}) |
| 436 |
|
| 437 |
def test_case_sensitivity(self): |
| 438 |
cf = self.newconfig() |
| 439 |
cf.add_section("A") |
| 440 |
cf.add_section("a") |
| 441 |
cf.add_section("B") |
| 442 |
L = cf.sections() |
| 443 |
L.sort() |
| 444 |
eq = self.assertEqual |
| 445 |
eq(L, ["A", "B", "a"]) |
| 446 |
cf.set("a", "B", "value") |
| 447 |
eq(cf.options("a"), ["b"]) |
| 448 |
eq(cf.get("a", "b"), "value", |
| 449 |
"could not locate option, expecting case-insensitive option names") |
| 450 |
with self.assertRaises(configparser.NoSectionError): |
| 451 |
# section names are case-sensitive |
| 452 |
cf.set("b", "A", "value") |
| 453 |
self.assertTrue(cf.has_option("a", "b")) |
| 454 |
self.assertFalse(cf.has_option("b", "b")) |
| 455 |
cf.set("A", "A-B", "A-B value") |
| 456 |
for opt in ("a-b", "A-b", "a-B", "A-B"): |
| 457 |
self.assertTrue( |
| 458 |
cf.has_option("A", opt), |
| 459 |
"has_option() returned false for option which should exist") |
| 460 |
eq(cf.options("A"), ["a-b"]) |
| 461 |
eq(cf.options("a"), ["b"]) |
| 462 |
cf.remove_option("a", "B") |
| 463 |
eq(cf.options("a"), []) |
| 464 |
|
| 465 |
# SF bug #432369: |
| 466 |
cf = self.fromstring( |
| 467 |
"[MySection]\nOption{} first line \n\tsecond line \n".format( |
| 468 |
self.delimiters[0])) |
| 469 |
eq(cf.options("MySection"), ["option"]) |
| 470 |
eq(cf.get("MySection", "Option"), "first line\nsecond line") |
| 471 |
|
| 472 |
# SF bug #561822: |
| 473 |
cf = self.fromstring("[section]\n" |
| 474 |
"nekey{}nevalue\n".format(self.delimiters[0]), |
| 475 |
defaults={"key":"value"}) |
| 476 |
self.assertTrue(cf.has_option("section", "Key")) |
| 477 |
|
| 478 |
|
| 479 |
def test_case_sensitivity_mapping_access(self): |
| 480 |
cf = self.newconfig() |
| 481 |
cf["A"] = {} |
| 482 |
cf["a"] = {"B": "value"} |
| 483 |
cf["B"] = {} |
| 484 |
L = [section for section in cf] |
| 485 |
L.sort() |
| 486 |
eq = self.assertEqual |
| 487 |
elem_eq = self.assertCountEqual |
| 488 |
eq(L, sorted(["A", "B", self.default_section, "a"])) |
| 489 |
eq(cf["a"].keys(), {"b"}) |
| 490 |
eq(cf["a"]["b"], "value", |
| 491 |
"could not locate option, expecting case-insensitive option names") |
| 492 |
with self.assertRaises(KeyError): |
| 493 |
# section names are case-sensitive |
| 494 |
cf["b"]["A"] = "value" |
| 495 |
self.assertTrue("b" in cf["a"]) |
| 496 |
cf["A"]["A-B"] = "A-B value" |
| 497 |
for opt in ("a-b", "A-b", "a-B", "A-B"): |
| 498 |
self.assertTrue( |
| 499 |
opt in cf["A"], |
| 500 |
"has_option() returned false for option which should exist") |
| 501 |
eq(cf["A"].keys(), {"a-b"}) |
| 502 |
eq(cf["a"].keys(), {"b"}) |
| 503 |
del cf["a"]["B"] |
| 504 |
elem_eq(cf["a"].keys(), {}) |
| 505 |
|
| 506 |
# SF bug #432369: |
| 507 |
cf = self.fromstring( |
| 508 |
"[MySection]\nOption{} first line \n\tsecond line \n".format( |
| 509 |
self.delimiters[0])) |
| 510 |
eq(cf["MySection"].keys(), {"option"}) |
| 511 |
eq(cf["MySection"]["Option"], "first line\nsecond line") |
| 512 |
|
| 513 |
# SF bug #561822: |
| 514 |
cf = self.fromstring("[section]\n" |
| 515 |
"nekey{}nevalue\n".format(self.delimiters[0]), |
| 516 |
defaults={"key":"value"}) |
| 517 |
self.assertTrue("Key" in cf["section"]) |
| 518 |
|
| 519 |
def test_default_case_sensitivity(self): |
| 520 |
cf = self.newconfig({"foo": "Bar"}) |
| 521 |
self.assertEqual( |
| 522 |
cf.get(self.default_section, "Foo"), "Bar", |
| 523 |
"could not locate option, expecting case-insensitive option names") |
| 524 |
cf = self.newconfig({"Foo": "Bar"}) |
| 525 |
self.assertEqual( |
| 526 |
cf.get(self.default_section, "Foo"), "Bar", |
| 527 |
"could not locate option, expecting case-insensitive defaults") |
| 528 |
|
| 529 |
def test_parse_errors(self): |
| 530 |
cf = self.newconfig() |
| 531 |
self.parse_error(cf, configparser.ParsingError, |
| 532 |
"[Foo]\n" |
| 533 |
"{}val-without-opt-name\n".format(self.delimiters[0])) |
| 534 |
self.parse_error(cf, configparser.ParsingError, |
| 535 |
"[Foo]\n" |
| 536 |
"{}val-without-opt-name\n".format(self.delimiters[1])) |
| 537 |
e = self.parse_error(cf, configparser.MissingSectionHeaderError, |
| 538 |
"No Section!\n") |
| 539 |
self.assertEqual(e.args, ('<???>', 1, "No Section!\n")) |
| 540 |
if not self.allow_no_value: |
| 541 |
e = self.parse_error(cf, configparser.ParsingError, |
| 542 |
"[Foo]\n wrong-indent\n") |
| 543 |
self.assertEqual(e.args, ('<???>',)) |
| 544 |
# read_file on a real file |
| 545 |
tricky = support.findfile("cfgparser.3", subdir="configdata") |
| 546 |
if self.delimiters[0] == '=': |
| 547 |
error = configparser.ParsingError |
| 548 |
expected = (tricky,) |
| 549 |
else: |
| 550 |
error = configparser.MissingSectionHeaderError |
| 551 |
expected = (tricky, 1, |
| 552 |
' # INI with as many tricky parts as possible\n') |
| 553 |
with open(tricky, encoding='utf-8') as f: |
| 554 |
e = self.parse_error(cf, error, f) |
| 555 |
self.assertEqual(e.args, expected) |
| 556 |
|
| 557 |
def parse_error(self, cf, exc, src): |
| 558 |
if hasattr(src, 'readline'): |
| 559 |
sio = src |
| 560 |
else: |
| 561 |
sio = io.StringIO(src) |
| 562 |
with self.assertRaises(exc) as cm: |
| 563 |
cf.read_file(sio) |
| 564 |
return cm.exception |
| 565 |
|
| 566 |
def test_query_errors(self): |
| 567 |
cf = self.newconfig() |
| 568 |
self.assertEqual(cf.sections(), [], |
| 569 |
"new ConfigParser should have no defined sections") |
| 570 |
self.assertFalse(cf.has_section("Foo"), |
| 571 |
"new ConfigParser should have no acknowledged " |
| 572 |
"sections") |
| 573 |
with self.assertRaises(configparser.NoSectionError): |
| 574 |
cf.options("Foo") |
| 575 |
with self.assertRaises(configparser.NoSectionError): |
| 576 |
cf.set("foo", "bar", "value") |
| 577 |
e = self.get_error(cf, configparser.NoSectionError, "foo", "bar") |
| 578 |
self.assertEqual(e.args, ("foo",)) |
| 579 |
cf.add_section("foo") |
| 580 |
e = self.get_error(cf, configparser.NoOptionError, "foo", "bar") |
| 581 |
self.assertEqual(e.args, ("bar", "foo")) |
| 582 |
|
| 583 |
def get_error(self, cf, exc, section, option): |
| 584 |
try: |
| 585 |
cf.get(section, option) |
| 586 |
except exc as e: |
| 587 |
return e |
| 588 |
else: |
| 589 |
self.fail("expected exception type %s.%s" |
| 590 |
% (exc.__module__, exc.__qualname__)) |
| 591 |
|
| 592 |
def test_boolean(self): |
| 593 |
cf = self.fromstring( |
| 594 |
"[BOOLTEST]\n" |
| 595 |
"T1{equals}1\n" |
| 596 |
"T2{equals}TRUE\n" |
| 597 |
"T3{equals}True\n" |
| 598 |
"T4{equals}oN\n" |
| 599 |
"T5{equals}yes\n" |
| 600 |
"F1{equals}0\n" |
| 601 |
"F2{equals}FALSE\n" |
| 602 |
"F3{equals}False\n" |
| 603 |
"F4{equals}oFF\n" |
| 604 |
"F5{equals}nO\n" |
| 605 |
"E1{equals}2\n" |
| 606 |
"E2{equals}foo\n" |
| 607 |
"E3{equals}-1\n" |
| 608 |
"E4{equals}0.1\n" |
| 609 |
"E5{equals}FALSE AND MORE".format(equals=self.delimiters[0]) |
| 610 |
) |
| 611 |
for x in range(1, 5): |
| 612 |
self.assertTrue(cf.getboolean('BOOLTEST', 't%d' % x)) |
| 613 |
self.assertFalse(cf.getboolean('BOOLTEST', 'f%d' % x)) |
| 614 |
self.assertRaises(ValueError, |
| 615 |
cf.getboolean, 'BOOLTEST', 'e%d' % x) |
| 616 |
|
| 617 |
def test_weird_errors(self): |
| 618 |
cf = self.newconfig() |
| 619 |
cf.add_section("Foo") |
| 620 |
with self.assertRaises(configparser.DuplicateSectionError) as cm: |
| 621 |
cf.add_section("Foo") |
| 622 |
e = cm.exception |
| 623 |
self.assertEqual(str(e), "Section 'Foo' already exists") |
| 624 |
self.assertEqual(e.args, ("Foo", None, None)) |
| 625 |
|
| 626 |
if self.strict: |
| 627 |
with self.assertRaises(configparser.DuplicateSectionError) as cm: |
| 628 |
cf.read_string(textwrap.dedent("""\ |
| 629 |
[Foo] |
| 630 |
will this be added{equals}True |
| 631 |
[Bar] |
| 632 |
what about this{equals}True |
| 633 |
[Foo] |
| 634 |
oops{equals}this won't |
| 635 |
""".format(equals=self.delimiters[0])), source='<foo-bar>') |
| 636 |
e = cm.exception |
| 637 |
self.assertEqual(str(e), "While reading from '<foo-bar>' " |
| 638 |
"[line 5]: section 'Foo' already exists") |
| 639 |
self.assertEqual(e.args, ("Foo", '<foo-bar>', 5)) |
| 640 |
|
| 641 |
with self.assertRaises(configparser.DuplicateOptionError) as cm: |
| 642 |
cf.read_dict({'Bar': {'opt': 'val', 'OPT': 'is really `opt`'}}) |
| 643 |
e = cm.exception |
| 644 |
self.assertEqual(str(e), "While reading from '<dict>': option " |
| 645 |
"'opt' in section 'Bar' already exists") |
| 646 |
self.assertEqual(e.args, ("Bar", "opt", "<dict>", None)) |
| 647 |
|
| 648 |
def test_get_after_duplicate_option_error(self): |
| 649 |
cf = self.newconfig() |
| 650 |
ini = textwrap.dedent("""\ |
| 651 |
[Foo] |
| 652 |
x{equals}1 |
| 653 |
y{equals}2 |
| 654 |
y{equals}3 |
| 655 |
""".format(equals=self.delimiters[0])) |
| 656 |
if self.strict: |
| 657 |
with self.assertRaises(configparser.DuplicateOptionError): |
| 658 |
cf.read_string(ini) |
| 659 |
else: |
| 660 |
cf.read_string(ini) |
| 661 |
self.assertEqual(cf.get('Foo', 'x'), '1') |
| 662 |
|
| 663 |
def test_write(self): |
| 664 |
config_string = ( |
| 665 |
"[Long Line]\n" |
| 666 |
"foo{0[0]} this line is much, much longer than my editor\n" |
| 667 |
" likes it.\n" |
| 668 |
"[{default_section}]\n" |
| 669 |
"foo{0[1]} another very\n" |
| 670 |
" long line\n" |
| 671 |
"[Long Line - With Comments!]\n" |
| 672 |
"test {0[1]} we {comment} can\n" |
| 673 |
" also {comment} place\n" |
| 674 |
" comments {comment} in\n" |
| 675 |
" multiline {comment} values" |
| 676 |
"\n".format(self.delimiters, comment=self.comment_prefixes[0], |
| 677 |
default_section=self.default_section) |
| 678 |
) |
| 679 |
if self.allow_no_value: |
| 680 |
config_string += ( |
| 681 |
"[Valueless]\n" |
| 682 |
"option-without-value\n" |
| 683 |
) |
| 684 |
|
| 685 |
cf = self.fromstring(config_string) |
| 686 |
for space_around_delimiters in (True, False): |
| 687 |
output = io.StringIO() |
| 688 |
cf.write(output, space_around_delimiters=space_around_delimiters) |
| 689 |
delimiter = self.delimiters[0] |
| 690 |
if space_around_delimiters: |
| 691 |
delimiter = " {} ".format(delimiter) |
| 692 |
expect_string = ( |
| 693 |
"[{default_section}]\n" |
| 694 |
"foo{equals}another very\n" |
| 695 |
"\tlong line\n" |
| 696 |
"\n" |
| 697 |
"[Long Line]\n" |
| 698 |
"foo{equals}this line is much, much longer than my editor\n" |
| 699 |
"\tlikes it.\n" |
| 700 |
"\n" |
| 701 |
"[Long Line - With Comments!]\n" |
| 702 |
"test{equals}we\n" |
| 703 |
"\talso\n" |
| 704 |
"\tcomments\n" |
| 705 |
"\tmultiline\n" |
| 706 |
"\n".format(equals=delimiter, |
| 707 |
default_section=self.default_section) |
| 708 |
) |
| 709 |
if self.allow_no_value: |
| 710 |
expect_string += ( |
| 711 |
"[Valueless]\n" |
| 712 |
"option-without-value\n" |
| 713 |
"\n" |
| 714 |
) |
| 715 |
self.assertEqual(output.getvalue(), expect_string) |
| 716 |
|
| 717 |
def test_set_string_types(self): |
| 718 |
cf = self.fromstring("[sect]\n" |
| 719 |
"option1{eq}foo\n".format(eq=self.delimiters[0])) |
| 720 |
# Check that we don't get an exception when setting values in |
| 721 |
# an existing section using strings: |
| 722 |
class mystr(str): |
| 723 |
pass |
| 724 |
cf.set("sect", "option1", "splat") |
| 725 |
cf.set("sect", "option1", mystr("splat")) |
| 726 |
cf.set("sect", "option2", "splat") |
| 727 |
cf.set("sect", "option2", mystr("splat")) |
| 728 |
cf.set("sect", "option1", "splat") |
| 729 |
cf.set("sect", "option2", "splat") |
| 730 |
|
| 731 |
def test_read_returns_file_list(self): |
| 732 |
if self.delimiters[0] != '=': |
| 733 |
self.skipTest('incompatible format') |
| 734 |
file1 = support.findfile("cfgparser.1", subdir="configdata") |
| 735 |
# check when we pass a mix of readable and non-readable files: |
| 736 |
cf = self.newconfig() |
| 737 |
parsed_files = cf.read([file1, "nonexistent-file"], encoding="utf-8") |
| 738 |
self.assertEqual(parsed_files, [file1]) |
| 739 |
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") |
| 740 |
# check when we pass only a filename: |
| 741 |
cf = self.newconfig() |
| 742 |
parsed_files = cf.read(file1, encoding="utf-8") |
| 743 |
self.assertEqual(parsed_files, [file1]) |
| 744 |
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") |
| 745 |
# check when we pass only a Path object: |
| 746 |
cf = self.newconfig() |
| 747 |
parsed_files = cf.read(os_helper.FakePath(file1), encoding="utf-8") |
| 748 |
self.assertEqual(parsed_files, [file1]) |
| 749 |
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") |
| 750 |
# check when we passed both a filename and a Path object: |
| 751 |
cf = self.newconfig() |
| 752 |
parsed_files = cf.read([os_helper.FakePath(file1), file1], encoding="utf-8") |
| 753 |
self.assertEqual(parsed_files, [file1, file1]) |
| 754 |
self.assertEqual(cf.get("Foo Bar", "foo"), "newbar") |
| 755 |
# check when we pass only missing files: |
| 756 |
cf = self.newconfig() |
| 757 |
parsed_files = cf.read(["nonexistent-file"], encoding="utf-8") |
| 758 |
self.assertEqual(parsed_files, []) |
| 759 |
# check when we pass no files: |
| 760 |
cf = self.newconfig() |
| 761 |
parsed_files = cf.read([], encoding="utf-8") |
| 762 |
self.assertEqual(parsed_files, []) |
| 763 |
|
| 764 |
def test_read_returns_file_list_with_bytestring_path(self): |
| 765 |
if self.delimiters[0] != '=': |
| 766 |
self.skipTest('incompatible format') |
| 767 |
file1_bytestring = support.findfile("cfgparser.1", subdir="configdata").encode() |
| 768 |
# check when passing an existing bytestring path |
| 769 |
cf = self.newconfig() |
| 770 |
parsed_files = cf.read(file1_bytestring, encoding="utf-8") |
| 771 |
self.assertEqual(parsed_files, [file1_bytestring]) |
| 772 |
# check when passing an non-existing bytestring path |
| 773 |
cf = self.newconfig() |
| 774 |
parsed_files = cf.read(b'nonexistent-file', encoding="utf-8") |
| 775 |
self.assertEqual(parsed_files, []) |
| 776 |
# check when passing both an existing and non-existing bytestring path |
| 777 |
cf = self.newconfig() |
| 778 |
parsed_files = cf.read([file1_bytestring, b'nonexistent-file'], encoding="utf-8") |
| 779 |
self.assertEqual(parsed_files, [file1_bytestring]) |
| 780 |
|
| 781 |
# shared by subclasses |
| 782 |
def get_interpolation_config(self): |
| 783 |
return self.fromstring( |
| 784 |
"[Foo]\n" |
| 785 |
"bar{equals}something %(with1)s interpolation (1 step)\n" |
| 786 |
"bar9{equals}something %(with9)s lots of interpolation (9 steps)\n" |
| 787 |
"bar10{equals}something %(with10)s lots of interpolation (10 steps)\n" |
| 788 |
"bar11{equals}something %(with11)s lots of interpolation (11 steps)\n" |
| 789 |
"with11{equals}%(with10)s\n" |
| 790 |
"with10{equals}%(with9)s\n" |
| 791 |
"with9{equals}%(with8)s\n" |
| 792 |
"with8{equals}%(With7)s\n" |
| 793 |
"with7{equals}%(WITH6)s\n" |
| 794 |
"with6{equals}%(with5)s\n" |
| 795 |
"With5{equals}%(with4)s\n" |
| 796 |
"WITH4{equals}%(with3)s\n" |
| 797 |
"with3{equals}%(with2)s\n" |
| 798 |
"with2{equals}%(with1)s\n" |
| 799 |
"with1{equals}with\n" |
| 800 |
"\n" |
| 801 |
"[Mutual Recursion]\n" |
| 802 |
"foo{equals}%(bar)s\n" |
| 803 |
"bar{equals}%(foo)s\n" |
| 804 |
"\n" |
| 805 |
"[Interpolation Error]\n" |
| 806 |
# no definition for 'reference' |
| 807 |
"name{equals}%(reference)s\n".format(equals=self.delimiters[0])) |
| 808 |
|
| 809 |
def check_items_config(self, expected): |
| 810 |
cf = self.fromstring(""" |
| 811 |
[section] |
| 812 |
name {0[0]} %(value)s |
| 813 |
key{0[1]} |%(name)s| |
| 814 |
getdefault{0[1]} |%(default)s| |
| 815 |
""".format(self.delimiters), defaults={"default": "<default>"}) |
| 816 |
L = list(cf.items("section", vars={'value': 'value'})) |
| 817 |
L.sort() |
| 818 |
self.assertEqual(L, expected) |
| 819 |
with self.assertRaises(configparser.NoSectionError): |
| 820 |
cf.items("no such section") |
| 821 |
|
| 822 |
def test_popitem(self): |
| 823 |
cf = self.fromstring(""" |
| 824 |
[section1] |
| 825 |
name1 {0[0]} value1 |
| 826 |
[section2] |
| 827 |
name2 {0[0]} value2 |
| 828 |
[section3] |
| 829 |
name3 {0[0]} value3 |
| 830 |
""".format(self.delimiters), defaults={"default": "<default>"}) |
| 831 |
self.assertEqual(cf.popitem()[0], 'section1') |
| 832 |
self.assertEqual(cf.popitem()[0], 'section2') |
| 833 |
self.assertEqual(cf.popitem()[0], 'section3') |
| 834 |
with self.assertRaises(KeyError): |
| 835 |
cf.popitem() |
| 836 |
|
| 837 |
def test_clear(self): |
| 838 |
cf = self.newconfig({"foo": "Bar"}) |
| 839 |
self.assertEqual( |
| 840 |
cf.get(self.default_section, "Foo"), "Bar", |
| 841 |
"could not locate option, expecting case-insensitive option names") |
| 842 |
cf['zing'] = {'option1': 'value1', 'option2': 'value2'} |
| 843 |
self.assertEqual(cf.sections(), ['zing']) |
| 844 |
self.assertEqual(set(cf['zing'].keys()), {'option1', 'option2', 'foo'}) |
| 845 |
cf.clear() |
| 846 |
self.assertEqual(set(cf.sections()), set()) |
| 847 |
self.assertEqual(set(cf[self.default_section].keys()), {'foo'}) |
| 848 |
|
| 849 |
def test_setitem(self): |
| 850 |
cf = self.fromstring(""" |
| 851 |
[section1] |
| 852 |
name1 {0[0]} value1 |
| 853 |
[section2] |
| 854 |
name2 {0[0]} value2 |
| 855 |
[section3] |
| 856 |
name3 {0[0]} value3 |
| 857 |
""".format(self.delimiters), defaults={"nameD": "valueD"}) |
| 858 |
self.assertEqual(set(cf['section1'].keys()), {'name1', 'named'}) |
| 859 |
self.assertEqual(set(cf['section2'].keys()), {'name2', 'named'}) |
| 860 |
self.assertEqual(set(cf['section3'].keys()), {'name3', 'named'}) |
| 861 |
self.assertEqual(cf['section1']['name1'], 'value1') |
| 862 |
self.assertEqual(cf['section2']['name2'], 'value2') |
| 863 |
self.assertEqual(cf['section3']['name3'], 'value3') |
| 864 |
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
| 865 |
cf['section2'] = {'name22': 'value22'} |
| 866 |
self.assertEqual(set(cf['section2'].keys()), {'name22', 'named'}) |
| 867 |
self.assertEqual(cf['section2']['name22'], 'value22') |
| 868 |
self.assertNotIn('name2', cf['section2']) |
| 869 |
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
| 870 |
cf['section3'] = {} |
| 871 |
self.assertEqual(set(cf['section3'].keys()), {'named'}) |
| 872 |
self.assertNotIn('name3', cf['section3']) |
| 873 |
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
| 874 |
# For bpo-32108, assigning default_section to itself. |
| 875 |
cf[self.default_section] = cf[self.default_section] |
| 876 |
self.assertNotEqual(set(cf[self.default_section].keys()), set()) |
| 877 |
cf[self.default_section] = {} |
| 878 |
self.assertEqual(set(cf[self.default_section].keys()), set()) |
| 879 |
self.assertEqual(set(cf['section1'].keys()), {'name1'}) |
| 880 |
self.assertEqual(set(cf['section2'].keys()), {'name22'}) |
| 881 |
self.assertEqual(set(cf['section3'].keys()), set()) |
| 882 |
self.assertEqual(cf.sections(), ['section1', 'section2', 'section3']) |
| 883 |
# For bpo-32108, assigning section to itself. |
| 884 |
cf['section2'] = cf['section2'] |
| 885 |
self.assertEqual(set(cf['section2'].keys()), {'name22'}) |
| 886 |
|
| 887 |
def test_invalid_multiline_value(self): |
| 888 |
if self.allow_no_value: |
| 889 |
self.skipTest('if no_value is allowed, ParsingError is not raised') |
| 890 |
|
| 891 |
invalid = textwrap.dedent("""\ |
| 892 |
[DEFAULT] |
| 893 |
test {0} test |
| 894 |
invalid""".format(self.delimiters[0]) |
| 895 |
) |
| 896 |
cf = self.newconfig() |
| 897 |
with self.assertRaises(configparser.ParsingError): |
| 898 |
cf.read_string(invalid) |
| 899 |
self.assertEqual(cf.get('DEFAULT', 'test'), 'test') |
| 900 |
self.assertEqual(cf['DEFAULT']['test'], 'test') |
| 901 |
|
| 902 |
|
| 903 |
class StrictTestCase(BasicTestCase, unittest.TestCase): |
| 904 |
config_class = configparser.RawConfigParser |
| 905 |
strict = True |
| 906 |
|
| 907 |
|
| 908 |
class ConfigParserTestCase(BasicTestCase, unittest.TestCase): |
| 909 |
config_class = configparser.ConfigParser |
| 910 |
|
| 911 |
def test_interpolation(self): |
| 912 |
cf = self.get_interpolation_config() |
| 913 |
eq = self.assertEqual |
| 914 |
eq(cf.get("Foo", "bar"), "something with interpolation (1 step)") |
| 915 |
eq(cf.get("Foo", "bar9"), |
| 916 |
"something with lots of interpolation (9 steps)") |
| 917 |
eq(cf.get("Foo", "bar10"), |
| 918 |
"something with lots of interpolation (10 steps)") |
| 919 |
e = self.get_error(cf, configparser.InterpolationDepthError, "Foo", "bar11") |
| 920 |
if self.interpolation == configparser._UNSET: |
| 921 |
self.assertEqual(e.args, ("bar11", "Foo", |
| 922 |
"something %(with11)s lots of interpolation (11 steps)")) |
| 923 |
|
| 924 |
def test_interpolation_missing_value(self): |
| 925 |
cf = self.get_interpolation_config() |
| 926 |
e = self.get_error(cf, configparser.InterpolationMissingOptionError, |
| 927 |
"Interpolation Error", "name") |
| 928 |
self.assertEqual(e.reference, "reference") |
| 929 |
self.assertEqual(e.section, "Interpolation Error") |
| 930 |
self.assertEqual(e.option, "name") |
| 931 |
if self.interpolation == configparser._UNSET: |
| 932 |
self.assertEqual(e.args, ('name', 'Interpolation Error', |
| 933 |
'%(reference)s', 'reference')) |
| 934 |
|
| 935 |
def test_items(self): |
| 936 |
self.check_items_config([('default', '<default>'), |
| 937 |
('getdefault', '|<default>|'), |
| 938 |
('key', '|value|'), |
| 939 |
('name', 'value')]) |
| 940 |
|
| 941 |
def test_safe_interpolation(self): |
| 942 |
# See https://bugs.python.org/issue511737 |
| 943 |
cf = self.fromstring("[section]\n" |
| 944 |
"option1{eq}xxx\n" |
| 945 |
"option2{eq}%(option1)s/xxx\n" |
| 946 |
"ok{eq}%(option1)s/%%s\n" |
| 947 |
"not_ok{eq}%(option2)s/%%s".format( |
| 948 |
eq=self.delimiters[0])) |
| 949 |
self.assertEqual(cf.get("section", "ok"), "xxx/%s") |
| 950 |
if self.interpolation == configparser._UNSET: |
| 951 |
self.assertEqual(cf.get("section", "not_ok"), "xxx/xxx/%s") |
| 952 |
|
| 953 |
def test_set_malformatted_interpolation(self): |
| 954 |
cf = self.fromstring("[sect]\n" |
| 955 |
"option1{eq}foo\n".format(eq=self.delimiters[0])) |
| 956 |
|
| 957 |
self.assertEqual(cf.get('sect', "option1"), "foo") |
| 958 |
|
| 959 |
self.assertRaises(ValueError, cf.set, "sect", "option1", "%foo") |
| 960 |
self.assertRaises(ValueError, cf.set, "sect", "option1", "foo%") |
| 961 |
self.assertRaises(ValueError, cf.set, "sect", "option1", "f%oo") |
| 962 |
|
| 963 |
self.assertEqual(cf.get('sect', "option1"), "foo") |
| 964 |
|
| 965 |
# bug #5741: double percents are *not* malformed |
| 966 |
cf.set("sect", "option2", "foo%%bar") |
| 967 |
self.assertEqual(cf.get("sect", "option2"), "foo%bar") |
| 968 |
|
| 969 |
def test_set_nonstring_types(self): |
| 970 |
cf = self.fromstring("[sect]\n" |
| 971 |
"option1{eq}foo\n".format(eq=self.delimiters[0])) |
| 972 |
# Check that we get a TypeError when setting non-string values |
| 973 |
# in an existing section: |
| 974 |
self.assertRaises(TypeError, cf.set, "sect", "option1", 1) |
| 975 |
self.assertRaises(TypeError, cf.set, "sect", "option1", 1.0) |
| 976 |
self.assertRaises(TypeError, cf.set, "sect", "option1", object()) |
| 977 |
self.assertRaises(TypeError, cf.set, "sect", "option2", 1) |
| 978 |
self.assertRaises(TypeError, cf.set, "sect", "option2", 1.0) |
| 979 |
self.assertRaises(TypeError, cf.set, "sect", "option2", object()) |
| 980 |
self.assertRaises(TypeError, cf.set, "sect", 123, "invalid opt name!") |
| 981 |
self.assertRaises(TypeError, cf.add_section, 123) |
| 982 |
|
| 983 |
def test_add_section_default(self): |
| 984 |
cf = self.newconfig() |
| 985 |
self.assertRaises(ValueError, cf.add_section, self.default_section) |
| 986 |
|
| 987 |
def test_defaults_keyword(self): |
| 988 |
"""bpo-23835 fix for ConfigParser""" |
| 989 |
cf = self.newconfig(defaults={1: 2.5}) |
| 990 |
self.assertEqual(cf[self.default_section]['1'], '2.5') |
| 991 |
self.assertAlmostEqual(cf[self.default_section].getfloat('1'), 2.5) |
| 992 |
cf = self.newconfig(defaults={"A": 5.25}) |
| 993 |
self.assertEqual(cf[self.default_section]['a'], '5.25') |
| 994 |
self.assertAlmostEqual(cf[self.default_section].getfloat('a'), 5.25) |
| 995 |
|
| 996 |
|
| 997 |
class ConfigParserTestCaseNoInterpolation(BasicTestCase, unittest.TestCase): |
| 998 |
config_class = configparser.ConfigParser |
| 999 |
interpolation = None |
| 1000 |
ini = textwrap.dedent(""" |
| 1001 |
[numbers] |
| 1002 |
one = 1 |
| 1003 |
two = %(one)s * 2 |
| 1004 |
three = ${common:one} * 3 |
| 1005 |
|
| 1006 |
[hexen] |
| 1007 |
sixteen = ${numbers:two} * 8 |
| 1008 |
""").strip() |
| 1009 |
|
| 1010 |
def assertMatchesIni(self, cf): |
| 1011 |
self.assertEqual(cf['numbers']['one'], '1') |
| 1012 |
self.assertEqual(cf['numbers']['two'], '%(one)s * 2') |
| 1013 |
self.assertEqual(cf['numbers']['three'], '${common:one} * 3') |
| 1014 |
self.assertEqual(cf['hexen']['sixteen'], '${numbers:two} * 8') |
| 1015 |
|
| 1016 |
def test_no_interpolation(self): |
| 1017 |
cf = self.fromstring(self.ini) |
| 1018 |
self.assertMatchesIni(cf) |
| 1019 |
|
| 1020 |
def test_empty_case(self): |
| 1021 |
cf = self.newconfig() |
| 1022 |
self.assertIsNone(cf.read_string("")) |
| 1023 |
|
| 1024 |
def test_none_as_default_interpolation(self): |
| 1025 |
class CustomConfigParser(configparser.ConfigParser): |
| 1026 |
_DEFAULT_INTERPOLATION = None |
| 1027 |
|
| 1028 |
cf = CustomConfigParser() |
| 1029 |
cf.read_string(self.ini) |
| 1030 |
self.assertMatchesIni(cf) |
| 1031 |
|
| 1032 |
class ConfigParserTestCaseInvalidInterpolationType(unittest.TestCase): |
| 1033 |
def test_error_on_wrong_type_for_interpolation(self): |
| 1034 |
for value in [configparser.ExtendedInterpolation, 42, "a string"]: |
| 1035 |
with self.subTest(value=value): |
| 1036 |
with self.assertRaises(TypeError): |
| 1037 |
configparser.ConfigParser(interpolation=value) |
| 1038 |
|
| 1039 |
|
| 1040 |
class ConfigParserTestCaseNonStandardDelimiters(ConfigParserTestCase): |
| 1041 |
delimiters = (':=', '$') |
| 1042 |
comment_prefixes = ('//', '"') |
| 1043 |
inline_comment_prefixes = ('//', '"') |
| 1044 |
|
| 1045 |
|
| 1046 |
class ConfigParserTestCaseNonStandardDefaultSection(ConfigParserTestCase): |
| 1047 |
default_section = 'general' |
| 1048 |
|
| 1049 |
|
| 1050 |
class MultilineValuesTestCase(BasicTestCase, unittest.TestCase): |
| 1051 |
config_class = configparser.ConfigParser |
| 1052 |
wonderful_spam = ("I'm having spam spam spam spam " |
| 1053 |
"spam spam spam beaked beans spam " |
| 1054 |
"spam spam and spam!").replace(' ', '\t\n') |
| 1055 |
|
| 1056 |
def setUp(self): |
| 1057 |
cf = self.newconfig() |
| 1058 |
for i in range(100): |
| 1059 |
s = 'section{}'.format(i) |
| 1060 |
cf.add_section(s) |
| 1061 |
for j in range(10): |
| 1062 |
cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam) |
| 1063 |
with open(os_helper.TESTFN, 'w', encoding="utf-8") as f: |
| 1064 |
cf.write(f) |
| 1065 |
|
| 1066 |
def tearDown(self): |
| 1067 |
os.unlink(os_helper.TESTFN) |
| 1068 |
|
| 1069 |
def test_dominating_multiline_values(self): |
| 1070 |
# We're reading from file because this is where the code changed |
| 1071 |
# during performance updates in Python 3.2 |
| 1072 |
cf_from_file = self.newconfig() |
| 1073 |
with open(os_helper.TESTFN, encoding="utf-8") as f: |
| 1074 |
cf_from_file.read_file(f) |
| 1075 |
self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'), |
| 1076 |
self.wonderful_spam.replace('\t\n', '\n')) |
| 1077 |
|
| 1078 |
|
| 1079 |
class RawConfigParserTestCase(BasicTestCase, unittest.TestCase): |
| 1080 |
config_class = configparser.RawConfigParser |
| 1081 |
|
| 1082 |
def test_interpolation(self): |
| 1083 |
cf = self.get_interpolation_config() |
| 1084 |
eq = self.assertEqual |
| 1085 |
eq(cf.get("Foo", "bar"), |
| 1086 |
"something %(with1)s interpolation (1 step)") |
| 1087 |
eq(cf.get("Foo", "bar9"), |
| 1088 |
"something %(with9)s lots of interpolation (9 steps)") |
| 1089 |
eq(cf.get("Foo", "bar10"), |
| 1090 |
"something %(with10)s lots of interpolation (10 steps)") |
| 1091 |
eq(cf.get("Foo", "bar11"), |
| 1092 |
"something %(with11)s lots of interpolation (11 steps)") |
| 1093 |
|
| 1094 |
def test_items(self): |
| 1095 |
self.check_items_config([('default', '<default>'), |
| 1096 |
('getdefault', '|%(default)s|'), |
| 1097 |
('key', '|%(name)s|'), |
| 1098 |
('name', '%(value)s')]) |
| 1099 |
|
| 1100 |
def test_set_nonstring_types(self): |
| 1101 |
cf = self.newconfig() |
| 1102 |
cf.add_section('non-string') |
| 1103 |
cf.set('non-string', 'int', 1) |
| 1104 |
cf.set('non-string', 'list', [0, 1, 1, 2, 3, 5, 8, 13]) |
| 1105 |
cf.set('non-string', 'dict', {'pi': 3.14159}) |
| 1106 |
self.assertEqual(cf.get('non-string', 'int'), 1) |
| 1107 |
self.assertEqual(cf.get('non-string', 'list'), |
| 1108 |
[0, 1, 1, 2, 3, 5, 8, 13]) |
| 1109 |
self.assertEqual(cf.get('non-string', 'dict'), {'pi': 3.14159}) |
| 1110 |
cf.add_section(123) |
| 1111 |
cf.set(123, 'this is sick', True) |
| 1112 |
self.assertEqual(cf.get(123, 'this is sick'), True) |
| 1113 |
if cf._dict is configparser._default_dict: |
| 1114 |
# would not work for SortedDict; only checking for the most common |
| 1115 |
# default dictionary (dict) |
| 1116 |
cf.optionxform = lambda x: x |
| 1117 |
cf.set('non-string', 1, 1) |
| 1118 |
self.assertEqual(cf.get('non-string', 1), 1) |
| 1119 |
|
| 1120 |
def test_defaults_keyword(self): |
| 1121 |
"""bpo-23835 legacy behavior for RawConfigParser""" |
| 1122 |
with self.assertRaises(AttributeError) as ctx: |
| 1123 |
self.newconfig(defaults={1: 2.4}) |
| 1124 |
err = ctx.exception |
| 1125 |
self.assertEqual(str(err), "'int' object has no attribute 'lower'") |
| 1126 |
cf = self.newconfig(defaults={"A": 5.2}) |
| 1127 |
self.assertAlmostEqual(cf[self.default_section]['a'], 5.2) |
| 1128 |
|
| 1129 |
|
| 1130 |
class RawConfigParserTestCaseNonStandardDelimiters(RawConfigParserTestCase): |
| 1131 |
delimiters = (':=', '$') |
| 1132 |
comment_prefixes = ('//', '"') |
| 1133 |
inline_comment_prefixes = ('//', '"') |
| 1134 |
|
| 1135 |
|
| 1136 |
class RawConfigParserTestSambaConf(CfgParserTestCaseClass, unittest.TestCase): |
| 1137 |
config_class = configparser.RawConfigParser |
| 1138 |
comment_prefixes = ('#', ';', '----') |
| 1139 |
inline_comment_prefixes = ('//',) |
| 1140 |
empty_lines_in_values = False |
| 1141 |
|
| 1142 |
def test_reading(self): |
| 1143 |
smbconf = support.findfile("cfgparser.2", subdir="configdata") |
| 1144 |
# check when we pass a mix of readable and non-readable files: |
| 1145 |
cf = self.newconfig() |
| 1146 |
parsed_files = cf.read([smbconf, "nonexistent-file"], encoding='utf-8') |
| 1147 |
self.assertEqual(parsed_files, [smbconf]) |
| 1148 |
sections = ['global', 'homes', 'printers', |
| 1149 |
'print$', 'pdf-generator', 'tmp', 'Agustin'] |
| 1150 |
self.assertEqual(cf.sections(), sections) |
| 1151 |
self.assertEqual(cf.get("global", "workgroup"), "MDKGROUP") |
| 1152 |
self.assertEqual(cf.getint("global", "max log size"), 50) |
| 1153 |
self.assertEqual(cf.get("global", "hosts allow"), "127.") |
| 1154 |
self.assertEqual(cf.get("tmp", "echo command"), "cat %s; rm %s") |
| 1155 |
|
| 1156 |
class ConfigParserTestCaseExtendedInterpolation(BasicTestCase, unittest.TestCase): |
| 1157 |
config_class = configparser.ConfigParser |
| 1158 |
interpolation = configparser.ExtendedInterpolation() |
| 1159 |
default_section = 'common' |
| 1160 |
strict = True |
| 1161 |
|
| 1162 |
def fromstring(self, string, defaults=None, optionxform=None): |
| 1163 |
cf = self.newconfig(defaults) |
| 1164 |
if optionxform: |
| 1165 |
cf.optionxform = optionxform |
| 1166 |
cf.read_string(string) |
| 1167 |
return cf |
| 1168 |
|
| 1169 |
def test_extended_interpolation(self): |
| 1170 |
cf = self.fromstring(textwrap.dedent(""" |
| 1171 |
[common] |
| 1172 |
favourite Beatle = Paul |
| 1173 |
favourite color = green |
| 1174 |
|
| 1175 |
[tom] |
| 1176 |
favourite band = ${favourite color} day |
| 1177 |
favourite pope = John ${favourite Beatle} II |
| 1178 |
sequel = ${favourite pope}I |
| 1179 |
|
| 1180 |
[ambv] |
| 1181 |
favourite Beatle = George |
| 1182 |
son of Edward VII = ${favourite Beatle} V |
| 1183 |
son of George V = ${son of Edward VII}I |
| 1184 |
|
| 1185 |
[stanley] |
| 1186 |
favourite Beatle = ${ambv:favourite Beatle} |
| 1187 |
favourite pope = ${tom:favourite pope} |
| 1188 |
favourite color = black |
| 1189 |
favourite state of mind = paranoid |
| 1190 |
favourite movie = soylent ${common:favourite color} |
| 1191 |
favourite song = ${favourite color} sabbath - ${favourite state of mind} |
| 1192 |
""").strip()) |
| 1193 |
|
| 1194 |
eq = self.assertEqual |
| 1195 |
eq(cf['common']['favourite Beatle'], 'Paul') |
| 1196 |
eq(cf['common']['favourite color'], 'green') |
| 1197 |
eq(cf['tom']['favourite Beatle'], 'Paul') |
| 1198 |
eq(cf['tom']['favourite color'], 'green') |
| 1199 |
eq(cf['tom']['favourite band'], 'green day') |
| 1200 |
eq(cf['tom']['favourite pope'], 'John Paul II') |
| 1201 |
eq(cf['tom']['sequel'], 'John Paul III') |
| 1202 |
eq(cf['ambv']['favourite Beatle'], 'George') |
| 1203 |
eq(cf['ambv']['favourite color'], 'green') |
| 1204 |
eq(cf['ambv']['son of Edward VII'], 'George V') |
| 1205 |
eq(cf['ambv']['son of George V'], 'George VI') |
| 1206 |
eq(cf['stanley']['favourite Beatle'], 'George') |
| 1207 |
eq(cf['stanley']['favourite color'], 'black') |
| 1208 |
eq(cf['stanley']['favourite state of mind'], 'paranoid') |
| 1209 |
eq(cf['stanley']['favourite movie'], 'soylent green') |
| 1210 |
eq(cf['stanley']['favourite pope'], 'John Paul II') |
| 1211 |
eq(cf['stanley']['favourite song'], |
| 1212 |
'black sabbath - paranoid') |
| 1213 |
|
| 1214 |
def test_endless_loop(self): |
| 1215 |
cf = self.fromstring(textwrap.dedent(""" |
| 1216 |
[one for you] |
| 1217 |
ping = ${one for me:pong} |
| 1218 |
|
| 1219 |
[one for me] |
| 1220 |
pong = ${one for you:ping} |
| 1221 |
|
| 1222 |
[selfish] |
| 1223 |
me = ${me} |
| 1224 |
""").strip()) |
| 1225 |
|
| 1226 |
with self.assertRaises(configparser.InterpolationDepthError): |
| 1227 |
cf['one for you']['ping'] |
| 1228 |
with self.assertRaises(configparser.InterpolationDepthError): |
| 1229 |
cf['selfish']['me'] |
| 1230 |
|
| 1231 |
def test_strange_options(self): |
| 1232 |
cf = self.fromstring(""" |
| 1233 |
[dollars] |
| 1234 |
$var = $$value |
| 1235 |
$var2 = ${$var} |
| 1236 |
${sick} = cannot interpolate me |
| 1237 |
|
| 1238 |
[interpolated] |
| 1239 |
$other = ${dollars:$var} |
| 1240 |
$trying = ${dollars:${sick}} |
| 1241 |
""") |
| 1242 |
|
| 1243 |
self.assertEqual(cf['dollars']['$var'], '$value') |
| 1244 |
self.assertEqual(cf['interpolated']['$other'], '$value') |
| 1245 |
self.assertEqual(cf['dollars']['${sick}'], 'cannot interpolate me') |
| 1246 |
exception_class = configparser.InterpolationMissingOptionError |
| 1247 |
with self.assertRaises(exception_class) as cm: |
| 1248 |
cf['interpolated']['$trying'] |
| 1249 |
self.assertEqual(cm.exception.reference, 'dollars:${sick') |
| 1250 |
self.assertEqual(cm.exception.args[2], '${dollars:${sick}}') #rawval |
| 1251 |
|
| 1252 |
def test_case_sensitivity_basic(self): |
| 1253 |
ini = textwrap.dedent(""" |
| 1254 |
[common] |
| 1255 |
optionlower = value |
| 1256 |
OptionUpper = Value |
| 1257 |
|
| 1258 |
[Common] |
| 1259 |
optionlower = a better ${common:optionlower} |
| 1260 |
OptionUpper = A Better ${common:OptionUpper} |
| 1261 |
|
| 1262 |
[random] |
| 1263 |
foolower = ${common:optionlower} redefined |
| 1264 |
FooUpper = ${Common:OptionUpper} Redefined |
| 1265 |
""").strip() |
| 1266 |
|
| 1267 |
cf = self.fromstring(ini) |
| 1268 |
eq = self.assertEqual |
| 1269 |
eq(cf['common']['optionlower'], 'value') |
| 1270 |
eq(cf['common']['OptionUpper'], 'Value') |
| 1271 |
eq(cf['Common']['optionlower'], 'a better value') |
| 1272 |
eq(cf['Common']['OptionUpper'], 'A Better Value') |
| 1273 |
eq(cf['random']['foolower'], 'value redefined') |
| 1274 |
eq(cf['random']['FooUpper'], 'A Better Value Redefined') |
| 1275 |
|
| 1276 |
def test_case_sensitivity_conflicts(self): |
| 1277 |
ini = textwrap.dedent(""" |
| 1278 |
[common] |
| 1279 |
option = value |
| 1280 |
Option = Value |
| 1281 |
|
| 1282 |
[Common] |
| 1283 |
option = a better ${common:option} |
| 1284 |
Option = A Better ${common:Option} |
| 1285 |
|
| 1286 |
[random] |
| 1287 |
foo = ${common:option} redefined |
| 1288 |
Foo = ${Common:Option} Redefined |
| 1289 |
""").strip() |
| 1290 |
with self.assertRaises(configparser.DuplicateOptionError): |
| 1291 |
cf = self.fromstring(ini) |
| 1292 |
|
| 1293 |
# raw options |
| 1294 |
cf = self.fromstring(ini, optionxform=lambda opt: opt) |
| 1295 |
eq = self.assertEqual |
| 1296 |
eq(cf['common']['option'], 'value') |
| 1297 |
eq(cf['common']['Option'], 'Value') |
| 1298 |
eq(cf['Common']['option'], 'a better value') |
| 1299 |
eq(cf['Common']['Option'], 'A Better Value') |
| 1300 |
eq(cf['random']['foo'], 'value redefined') |
| 1301 |
eq(cf['random']['Foo'], 'A Better Value Redefined') |
| 1302 |
|
| 1303 |
def test_other_errors(self): |
| 1304 |
cf = self.fromstring(""" |
| 1305 |
[interpolation fail] |
| 1306 |
case1 = ${where's the brace |
| 1307 |
case2 = ${does_not_exist} |
| 1308 |
case3 = ${wrong_section:wrong_value} |
| 1309 |
case4 = ${i:like:colon:characters} |
| 1310 |
case5 = $100 for Fail No 5! |
| 1311 |
""") |
| 1312 |
|
| 1313 |
with self.assertRaises(configparser.InterpolationSyntaxError): |
| 1314 |
cf['interpolation fail']['case1'] |
| 1315 |
with self.assertRaises(configparser.InterpolationMissingOptionError): |
| 1316 |
cf['interpolation fail']['case2'] |
| 1317 |
with self.assertRaises(configparser.InterpolationMissingOptionError): |
| 1318 |
cf['interpolation fail']['case3'] |
| 1319 |
with self.assertRaises(configparser.InterpolationSyntaxError): |
| 1320 |
cf['interpolation fail']['case4'] |
| 1321 |
with self.assertRaises(configparser.InterpolationSyntaxError): |
| 1322 |
cf['interpolation fail']['case5'] |
| 1323 |
with self.assertRaises(ValueError): |
| 1324 |
cf['interpolation fail']['case6'] = "BLACK $ABBATH" |
| 1325 |
|
| 1326 |
|
| 1327 |
class ConfigParserTestCaseNoValue(ConfigParserTestCase): |
| 1328 |
allow_no_value = True |
| 1329 |
|
| 1330 |
|
| 1331 |
class NoValueAndExtendedInterpolation(CfgParserTestCaseClass): |
| 1332 |
interpolation = configparser.ExtendedInterpolation() |
| 1333 |
allow_no_value = True |
| 1334 |
|
| 1335 |
def test_interpolation_with_allow_no_value(self): |
| 1336 |
config = textwrap.dedent(""" |
| 1337 |
[dummy] |
| 1338 |
a |
| 1339 |
b = ${a} |
| 1340 |
""") |
| 1341 |
cf = self.fromstring(config) |
| 1342 |
|
| 1343 |
self.assertIs(cf["dummy"]["a"], None) |
| 1344 |
self.assertEqual(cf["dummy"]["b"], "") |
| 1345 |
|
| 1346 |
def test_explicit_none(self): |
| 1347 |
config = textwrap.dedent(""" |
| 1348 |
[dummy] |
| 1349 |
a = None |
| 1350 |
b = ${a} |
| 1351 |
""") |
| 1352 |
cf = self.fromstring(config) |
| 1353 |
|
| 1354 |
self.assertEqual(cf["dummy"]["a"], "None") |
| 1355 |
self.assertEqual(cf["dummy"]["b"], "None") |
| 1356 |
|
| 1357 |
|
| 1358 |
class ConfigParserNoValueAndExtendedInterpolationTest( |
| 1359 |
NoValueAndExtendedInterpolation, |
| 1360 |
unittest.TestCase, |
| 1361 |
): |
| 1362 |
config_class = configparser.ConfigParser |
| 1363 |
|
| 1364 |
|
| 1365 |
class RawConfigParserNoValueAndExtendedInterpolationTest( |
| 1366 |
NoValueAndExtendedInterpolation, |
| 1367 |
unittest.TestCase, |
| 1368 |
): |
| 1369 |
config_class = configparser.RawConfigParser |
| 1370 |
|
| 1371 |
|
| 1372 |
class ConfigParserTestCaseTrickyFile(CfgParserTestCaseClass, unittest.TestCase): |
| 1373 |
config_class = configparser.ConfigParser |
| 1374 |
delimiters = {'='} |
| 1375 |
comment_prefixes = {'#'} |
| 1376 |
allow_no_value = True |
| 1377 |
|
| 1378 |
def test_cfgparser_dot_3(self): |
| 1379 |
tricky = support.findfile("cfgparser.3", subdir="configdata") |
| 1380 |
cf = self.newconfig() |
| 1381 |
self.assertEqual(len(cf.read(tricky, encoding='utf-8')), 1) |
| 1382 |
self.assertEqual(cf.sections(), ['strange', |
| 1383 |
'corruption', |
| 1384 |
'yeah, sections can be ' |
| 1385 |
'indented as well', |
| 1386 |
'another one!', |
| 1387 |
'no values here', |
| 1388 |
'tricky interpolation', |
| 1389 |
'more interpolation']) |
| 1390 |
self.assertEqual(cf.getint(self.default_section, 'go', |
| 1391 |
vars={'interpolate': '-1'}), -1) |
| 1392 |
with self.assertRaises(ValueError): |
| 1393 |
# no interpolation will happen |
| 1394 |
cf.getint(self.default_section, 'go', raw=True, |
| 1395 |
vars={'interpolate': '-1'}) |
| 1396 |
self.assertEqual(len(cf.get('strange', 'other').split('\n')), 4) |
| 1397 |
self.assertEqual(len(cf.get('corruption', 'value').split('\n')), 10) |
| 1398 |
longname = 'yeah, sections can be indented as well' |
| 1399 |
self.assertFalse(cf.getboolean(longname, 'are they subsections')) |
| 1400 |
self.assertEqual(cf.get(longname, 'lets use some Unicode'), '片仮名') |
| 1401 |
self.assertEqual(len(cf.items('another one!')), 5) # 4 in section and |
| 1402 |
# `go` from DEFAULT |
| 1403 |
with self.assertRaises(configparser.InterpolationMissingOptionError): |
| 1404 |
cf.items('no values here') |
| 1405 |
self.assertEqual(cf.get('tricky interpolation', 'lets'), 'do this') |
| 1406 |
self.assertEqual(cf.get('tricky interpolation', 'lets'), |
| 1407 |
cf.get('tricky interpolation', 'go')) |
| 1408 |
self.assertEqual(cf.get('more interpolation', 'lets'), 'go shopping') |
| 1409 |
|
| 1410 |
def test_unicode_failure(self): |
| 1411 |
tricky = support.findfile("cfgparser.3", subdir="configdata") |
| 1412 |
cf = self.newconfig() |
| 1413 |
with self.assertRaises(UnicodeDecodeError): |
| 1414 |
cf.read(tricky, encoding='ascii') |
| 1415 |
|
| 1416 |
|
| 1417 |
class Issue7005TestCase(unittest.TestCase): |
| 1418 |
"""Test output when None is set() as a value and allow_no_value == False. |
| 1419 |
|
| 1420 |
http://bugs.python.org/issue7005 |
| 1421 |
|
| 1422 |
""" |
| 1423 |
|
| 1424 |
expected_output = "[section]\noption = None\n\n" |
| 1425 |
|
| 1426 |
def prepare(self, config_class): |
| 1427 |
# This is the default, but that's the point. |
| 1428 |
cp = config_class(allow_no_value=False) |
| 1429 |
cp.add_section("section") |
| 1430 |
cp.set("section", "option", None) |
| 1431 |
sio = io.StringIO() |
| 1432 |
cp.write(sio) |
| 1433 |
return sio.getvalue() |
| 1434 |
|
| 1435 |
def test_none_as_value_stringified(self): |
| 1436 |
cp = configparser.ConfigParser(allow_no_value=False) |
| 1437 |
cp.add_section("section") |
| 1438 |
with self.assertRaises(TypeError): |
| 1439 |
cp.set("section", "option", None) |
| 1440 |
|
| 1441 |
def test_none_as_value_stringified_raw(self): |
| 1442 |
output = self.prepare(configparser.RawConfigParser) |
| 1443 |
self.assertEqual(output, self.expected_output) |
| 1444 |
|
| 1445 |
|
| 1446 |
class SortedTestCase(RawConfigParserTestCase): |
| 1447 |
dict_type = SortedDict |
| 1448 |
|
| 1449 |
def test_sorted(self): |
| 1450 |
cf = self.fromstring("[b]\n" |
| 1451 |
"o4=1\n" |
| 1452 |
"o3=2\n" |
| 1453 |
"o2=3\n" |
| 1454 |
"o1=4\n" |
| 1455 |
"[a]\n" |
| 1456 |
"k=v\n") |
| 1457 |
output = io.StringIO() |
| 1458 |
cf.write(output) |
| 1459 |
self.assertEqual(output.getvalue(), |
| 1460 |
"[a]\n" |
| 1461 |
"k = v\n\n" |
| 1462 |
"[b]\n" |
| 1463 |
"o1 = 4\n" |
| 1464 |
"o2 = 3\n" |
| 1465 |
"o3 = 2\n" |
| 1466 |
"o4 = 1\n\n") |
| 1467 |
|
| 1468 |
|
| 1469 |
class CompatibleTestCase(CfgParserTestCaseClass, unittest.TestCase): |
| 1470 |
config_class = configparser.RawConfigParser |
| 1471 |
comment_prefixes = '#;' |
| 1472 |
inline_comment_prefixes = ';' |
| 1473 |
|
| 1474 |
def test_comment_handling(self): |
| 1475 |
config_string = textwrap.dedent("""\ |
| 1476 |
[Commented Bar] |
| 1477 |
baz=qwe ; a comment |
| 1478 |
foo: bar # not a comment! |
| 1479 |
# but this is a comment |
| 1480 |
; another comment |
| 1481 |
quirk: this;is not a comment |
| 1482 |
; a space must precede an inline comment |
| 1483 |
""") |
| 1484 |
cf = self.fromstring(config_string) |
| 1485 |
self.assertEqual(cf.get('Commented Bar', 'foo'), |
| 1486 |
'bar # not a comment!') |
| 1487 |
self.assertEqual(cf.get('Commented Bar', 'baz'), 'qwe') |
| 1488 |
self.assertEqual(cf.get('Commented Bar', 'quirk'), |
| 1489 |
'this;is not a comment') |
| 1490 |
|
| 1491 |
class CopyTestCase(BasicTestCase, unittest.TestCase): |
| 1492 |
config_class = configparser.ConfigParser |
| 1493 |
|
| 1494 |
def fromstring(self, string, defaults=None): |
| 1495 |
cf = self.newconfig(defaults) |
| 1496 |
cf.read_string(string) |
| 1497 |
cf_copy = self.newconfig() |
| 1498 |
cf_copy.read_dict(cf) |
| 1499 |
# we have to clean up option duplicates that appeared because of |
| 1500 |
# the magic DEFAULTSECT behaviour. |
| 1501 |
for section in cf_copy.values(): |
| 1502 |
if section.name == self.default_section: |
| 1503 |
continue |
| 1504 |
for default, value in cf[self.default_section].items(): |
| 1505 |
if section[default] == value: |
| 1506 |
del section[default] |
| 1507 |
return cf_copy |
| 1508 |
|
| 1509 |
|
| 1510 |
class FakeFile: |
| 1511 |
def __init__(self): |
| 1512 |
file_path = support.findfile("cfgparser.1", subdir="configdata") |
| 1513 |
with open(file_path, encoding="utf-8") as f: |
| 1514 |
self.lines = f.readlines() |
| 1515 |
self.lines.reverse() |
| 1516 |
|
| 1517 |
def readline(self): |
| 1518 |
if len(self.lines): |
| 1519 |
return self.lines.pop() |
| 1520 |
return '' |
| 1521 |
|
| 1522 |
|
| 1523 |
def readline_generator(f): |
| 1524 |
"""As advised in Doc/library/configparser.rst.""" |
| 1525 |
line = f.readline() |
| 1526 |
while line: |
| 1527 |
yield line |
| 1528 |
line = f.readline() |
| 1529 |
|
| 1530 |
|
| 1531 |
class ReadFileTestCase(unittest.TestCase): |
| 1532 |
def test_file(self): |
| 1533 |
file_paths = [support.findfile("cfgparser.1", subdir="configdata")] |
| 1534 |
try: |
| 1535 |
file_paths.append(file_paths[0].encode('utf8')) |
| 1536 |
except UnicodeEncodeError: |
| 1537 |
pass # unfortunately we can't test bytes on this path |
| 1538 |
for file_path in file_paths: |
| 1539 |
parser = configparser.ConfigParser() |
| 1540 |
with open(file_path, encoding="utf-8") as f: |
| 1541 |
parser.read_file(f) |
| 1542 |
self.assertIn("Foo Bar", parser) |
| 1543 |
self.assertIn("foo", parser["Foo Bar"]) |
| 1544 |
self.assertEqual(parser["Foo Bar"]["foo"], "newbar") |
| 1545 |
|
| 1546 |
def test_iterable(self): |
| 1547 |
lines = textwrap.dedent(""" |
| 1548 |
[Foo Bar] |
| 1549 |
foo=newbar""").strip().split('\n') |
| 1550 |
parser = configparser.ConfigParser() |
| 1551 |
parser.read_file(lines) |
| 1552 |
self.assertIn("Foo Bar", parser) |
| 1553 |
self.assertIn("foo", parser["Foo Bar"]) |
| 1554 |
self.assertEqual(parser["Foo Bar"]["foo"], "newbar") |
| 1555 |
|
| 1556 |
def test_readline_generator(self): |
| 1557 |
"""Issue #11670.""" |
| 1558 |
parser = configparser.ConfigParser() |
| 1559 |
with self.assertRaises(TypeError): |
| 1560 |
parser.read_file(FakeFile()) |
| 1561 |
parser.read_file(readline_generator(FakeFile())) |
| 1562 |
self.assertIn("Foo Bar", parser) |
| 1563 |
self.assertIn("foo", parser["Foo Bar"]) |
| 1564 |
self.assertEqual(parser["Foo Bar"]["foo"], "newbar") |
| 1565 |
|
| 1566 |
def test_source_as_bytes(self): |
| 1567 |
"""Issue #18260.""" |
| 1568 |
lines = textwrap.dedent(""" |
| 1569 |
[badbad] |
| 1570 |
[badbad]""").strip().split('\n') |
| 1571 |
parser = configparser.ConfigParser() |
| 1572 |
with self.assertRaises(configparser.DuplicateSectionError) as dse: |
| 1573 |
parser.read_file(lines, source=b"badbad") |
| 1574 |
self.assertEqual( |
| 1575 |
str(dse.exception), |
| 1576 |
"While reading from b'badbad' [line 2]: section 'badbad' " |
| 1577 |
"already exists" |
| 1578 |
) |
| 1579 |
lines = textwrap.dedent(""" |
| 1580 |
[badbad] |
| 1581 |
bad = bad |
| 1582 |
bad = bad""").strip().split('\n') |
| 1583 |
parser = configparser.ConfigParser() |
| 1584 |
with self.assertRaises(configparser.DuplicateOptionError) as dse: |
| 1585 |
parser.read_file(lines, source=b"badbad") |
| 1586 |
self.assertEqual( |
| 1587 |
str(dse.exception), |
| 1588 |
"While reading from b'badbad' [line 3]: option 'bad' in section " |
| 1589 |
"'badbad' already exists" |
| 1590 |
) |
| 1591 |
lines = textwrap.dedent(""" |
| 1592 |
[badbad] |
| 1593 |
= bad""").strip().split('\n') |
| 1594 |
parser = configparser.ConfigParser() |
| 1595 |
with self.assertRaises(configparser.ParsingError) as dse: |
| 1596 |
parser.read_file(lines, source=b"badbad") |
| 1597 |
self.assertEqual( |
| 1598 |
str(dse.exception), |
| 1599 |
"Source contains parsing errors: b'badbad'\n\t[line 2]: '= bad'" |
| 1600 |
) |
| 1601 |
lines = textwrap.dedent(""" |
| 1602 |
[badbad |
| 1603 |
bad = bad""").strip().split('\n') |
| 1604 |
parser = configparser.ConfigParser() |
| 1605 |
with self.assertRaises(configparser.MissingSectionHeaderError) as dse: |
| 1606 |
parser.read_file(lines, source=b"badbad") |
| 1607 |
self.assertEqual( |
| 1608 |
str(dse.exception), |
| 1609 |
"File contains no section headers.\nfile: b'badbad', line: 1\n" |
| 1610 |
"'[badbad'" |
| 1611 |
) |
| 1612 |
|
| 1613 |
def test_keys_without_value_with_extra_whitespace(self): |
| 1614 |
lines = [ |
| 1615 |
'[SECT]\n', |
| 1616 |
'KEY1\n', |
| 1617 |
' KEY2 = VAL2\n', # note the Space before the key! |
| 1618 |
] |
| 1619 |
parser = configparser.ConfigParser( |
| 1620 |
comment_prefixes="", |
| 1621 |
allow_no_value=True, |
| 1622 |
strict=False, |
| 1623 |
delimiters=('=',), |
| 1624 |
interpolation=None, |
| 1625 |
) |
| 1626 |
with self.assertRaises(configparser.MultilineContinuationError) as dse: |
| 1627 |
parser.read_file(lines) |
| 1628 |
self.assertEqual( |
| 1629 |
str(dse.exception), |
| 1630 |
"Key without value continued with an indented line.\n" |
| 1631 |
"file: '<???>', line: 3\n" |
| 1632 |
"' KEY2 = VAL2\\n'" |
| 1633 |
) |
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
class CoverageOneHundredTestCase(unittest.TestCase): |
| 1639 |
"""Covers edge cases in the codebase.""" |
| 1640 |
|
| 1641 |
def test_duplicate_option_error(self): |
| 1642 |
error = configparser.DuplicateOptionError('section', 'option') |
| 1643 |
self.assertEqual(error.section, 'section') |
| 1644 |
self.assertEqual(error.option, 'option') |
| 1645 |
self.assertEqual(error.source, None) |
| 1646 |
self.assertEqual(error.lineno, None) |
| 1647 |
self.assertEqual(error.args, ('section', 'option', None, None)) |
| 1648 |
self.assertEqual(str(error), "Option 'option' in section 'section' " |
| 1649 |
"already exists") |
| 1650 |
|
| 1651 |
def test_interpolation_depth_error(self): |
| 1652 |
error = configparser.InterpolationDepthError('option', 'section', |
| 1653 |
'rawval') |
| 1654 |
self.assertEqual(error.args, ('option', 'section', 'rawval')) |
| 1655 |
self.assertEqual(error.option, 'option') |
| 1656 |
self.assertEqual(error.section, 'section') |
| 1657 |
|
| 1658 |
def test_parsing_error(self): |
| 1659 |
with self.assertRaises(TypeError) as cm: |
| 1660 |
configparser.ParsingError() |
| 1661 |
error = configparser.ParsingError(source='source') |
| 1662 |
self.assertEqual(error.source, 'source') |
| 1663 |
error = configparser.ParsingError('source') |
| 1664 |
self.assertEqual(error.source, 'source') |
| 1665 |
|
| 1666 |
def test_interpolation_validation(self): |
| 1667 |
parser = configparser.ConfigParser() |
| 1668 |
parser.read_string(""" |
| 1669 |
[section] |
| 1670 |
invalid_percent = % |
| 1671 |
invalid_reference = %(() |
| 1672 |
invalid_variable = %(does_not_exist)s |
| 1673 |
""") |
| 1674 |
with self.assertRaises(configparser.InterpolationSyntaxError) as cm: |
| 1675 |
parser['section']['invalid_percent'] |
| 1676 |
self.assertEqual(str(cm.exception), "'%' must be followed by '%' or " |
| 1677 |
"'(', found: '%'") |
| 1678 |
with self.assertRaises(configparser.InterpolationSyntaxError) as cm: |
| 1679 |
parser['section']['invalid_reference'] |
| 1680 |
self.assertEqual(str(cm.exception), "bad interpolation variable " |
| 1681 |
"reference '%(()'") |
| 1682 |
|
| 1683 |
def test_sectionproxy_repr(self): |
| 1684 |
parser = configparser.ConfigParser() |
| 1685 |
parser.read_string(""" |
| 1686 |
[section] |
| 1687 |
key = value |
| 1688 |
""") |
| 1689 |
self.assertEqual(repr(parser['section']), '<Section: section>') |
| 1690 |
|
| 1691 |
def test_inconsistent_converters_state(self): |
| 1692 |
parser = configparser.ConfigParser() |
| 1693 |
import decimal |
| 1694 |
parser.converters['decimal'] = decimal.Decimal |
| 1695 |
parser.read_string(""" |
| 1696 |
[s1] |
| 1697 |
one = 1 |
| 1698 |
[s2] |
| 1699 |
two = 2 |
| 1700 |
""") |
| 1701 |
self.assertIn('decimal', parser.converters) |
| 1702 |
self.assertEqual(parser.getdecimal('s1', 'one'), 1) |
| 1703 |
self.assertEqual(parser.getdecimal('s2', 'two'), 2) |
| 1704 |
self.assertEqual(parser['s1'].getdecimal('one'), 1) |
| 1705 |
self.assertEqual(parser['s2'].getdecimal('two'), 2) |
| 1706 |
del parser.getdecimal |
| 1707 |
with self.assertRaises(AttributeError): |
| 1708 |
parser.getdecimal('s1', 'one') |
| 1709 |
self.assertIn('decimal', parser.converters) |
| 1710 |
del parser.converters['decimal'] |
| 1711 |
self.assertNotIn('decimal', parser.converters) |
| 1712 |
with self.assertRaises(AttributeError): |
| 1713 |
parser.getdecimal('s1', 'one') |
| 1714 |
with self.assertRaises(AttributeError): |
| 1715 |
parser['s1'].getdecimal('one') |
| 1716 |
with self.assertRaises(AttributeError): |
| 1717 |
parser['s2'].getdecimal('two') |
| 1718 |
|
| 1719 |
|
| 1720 |
class ExceptionPicklingTestCase(unittest.TestCase): |
| 1721 |
"""Tests for issue #13760: ConfigParser exceptions are not picklable.""" |
| 1722 |
|
| 1723 |
def test_error(self): |
| 1724 |
import pickle |
| 1725 |
e1 = configparser.Error('value') |
| 1726 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1727 |
pickled = pickle.dumps(e1, proto) |
| 1728 |
e2 = pickle.loads(pickled) |
| 1729 |
self.assertEqual(e1.message, e2.message) |
| 1730 |
self.assertEqual(repr(e1), repr(e2)) |
| 1731 |
|
| 1732 |
def test_combine_error_linear_complexity(self): |
| 1733 |
# Ensure that ParsingError.combine() has linear complexity. |
| 1734 |
# See https://github.com/python/cpython/issues/148370. |
| 1735 |
n = 50000 |
| 1736 |
s = '[*]\n' + (err_line := '=\n') * n |
| 1737 |
p = configparser.ConfigParser(strict=False) |
| 1738 |
with self.assertRaises(configparser.ParsingError) as cm: |
| 1739 |
p.read_string(s) |
| 1740 |
errlines = cm.exception.message.splitlines() |
| 1741 |
self.assertEqual(len(errlines), n + 1) |
| 1742 |
self.assertStartsWith(errlines[0], "Source contains parsing errors: ") |
| 1743 |
self.assertEqual(errlines[42], f"\t[line {43:2d}]: {err_line!r}") |
| 1744 |
|
| 1745 |
def test_nosectionerror(self): |
| 1746 |
import pickle |
| 1747 |
e1 = configparser.NoSectionError('section') |
| 1748 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1749 |
pickled = pickle.dumps(e1, proto) |
| 1750 |
e2 = pickle.loads(pickled) |
| 1751 |
self.assertEqual(e1.message, e2.message) |
| 1752 |
self.assertEqual(e1.args, e2.args) |
| 1753 |
self.assertEqual(e1.section, e2.section) |
| 1754 |
self.assertEqual(repr(e1), repr(e2)) |
| 1755 |
|
| 1756 |
def test_nooptionerror(self): |
| 1757 |
import pickle |
| 1758 |
e1 = configparser.NoOptionError('option', 'section') |
| 1759 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1760 |
pickled = pickle.dumps(e1, proto) |
| 1761 |
e2 = pickle.loads(pickled) |
| 1762 |
self.assertEqual(e1.message, e2.message) |
| 1763 |
self.assertEqual(e1.args, e2.args) |
| 1764 |
self.assertEqual(e1.section, e2.section) |
| 1765 |
self.assertEqual(e1.option, e2.option) |
| 1766 |
self.assertEqual(repr(e1), repr(e2)) |
| 1767 |
|
| 1768 |
def test_duplicatesectionerror(self): |
| 1769 |
import pickle |
| 1770 |
e1 = configparser.DuplicateSectionError('section', 'source', 123) |
| 1771 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1772 |
pickled = pickle.dumps(e1, proto) |
| 1773 |
e2 = pickle.loads(pickled) |
| 1774 |
self.assertEqual(e1.message, e2.message) |
| 1775 |
self.assertEqual(e1.args, e2.args) |
| 1776 |
self.assertEqual(e1.section, e2.section) |
| 1777 |
self.assertEqual(e1.source, e2.source) |
| 1778 |
self.assertEqual(e1.lineno, e2.lineno) |
| 1779 |
self.assertEqual(repr(e1), repr(e2)) |
| 1780 |
|
| 1781 |
def test_duplicateoptionerror(self): |
| 1782 |
import pickle |
| 1783 |
e1 = configparser.DuplicateOptionError('section', 'option', 'source', |
| 1784 |
123) |
| 1785 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1786 |
pickled = pickle.dumps(e1, proto) |
| 1787 |
e2 = pickle.loads(pickled) |
| 1788 |
self.assertEqual(e1.message, e2.message) |
| 1789 |
self.assertEqual(e1.args, e2.args) |
| 1790 |
self.assertEqual(e1.section, e2.section) |
| 1791 |
self.assertEqual(e1.option, e2.option) |
| 1792 |
self.assertEqual(e1.source, e2.source) |
| 1793 |
self.assertEqual(e1.lineno, e2.lineno) |
| 1794 |
self.assertEqual(repr(e1), repr(e2)) |
| 1795 |
|
| 1796 |
def test_interpolationerror(self): |
| 1797 |
import pickle |
| 1798 |
e1 = configparser.InterpolationError('option', 'section', 'msg') |
| 1799 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1800 |
pickled = pickle.dumps(e1, proto) |
| 1801 |
e2 = pickle.loads(pickled) |
| 1802 |
self.assertEqual(e1.message, e2.message) |
| 1803 |
self.assertEqual(e1.args, e2.args) |
| 1804 |
self.assertEqual(e1.section, e2.section) |
| 1805 |
self.assertEqual(e1.option, e2.option) |
| 1806 |
self.assertEqual(repr(e1), repr(e2)) |
| 1807 |
|
| 1808 |
def test_interpolationmissingoptionerror(self): |
| 1809 |
import pickle |
| 1810 |
e1 = configparser.InterpolationMissingOptionError('option', 'section', |
| 1811 |
'rawval', 'reference') |
| 1812 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1813 |
pickled = pickle.dumps(e1, proto) |
| 1814 |
e2 = pickle.loads(pickled) |
| 1815 |
self.assertEqual(e1.message, e2.message) |
| 1816 |
self.assertEqual(e1.args, e2.args) |
| 1817 |
self.assertEqual(e1.section, e2.section) |
| 1818 |
self.assertEqual(e1.option, e2.option) |
| 1819 |
self.assertEqual(e1.reference, e2.reference) |
| 1820 |
self.assertEqual(repr(e1), repr(e2)) |
| 1821 |
|
| 1822 |
def test_interpolationsyntaxerror(self): |
| 1823 |
import pickle |
| 1824 |
e1 = configparser.InterpolationSyntaxError('option', 'section', 'msg') |
| 1825 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1826 |
pickled = pickle.dumps(e1, proto) |
| 1827 |
e2 = pickle.loads(pickled) |
| 1828 |
self.assertEqual(e1.message, e2.message) |
| 1829 |
self.assertEqual(e1.args, e2.args) |
| 1830 |
self.assertEqual(e1.section, e2.section) |
| 1831 |
self.assertEqual(e1.option, e2.option) |
| 1832 |
self.assertEqual(repr(e1), repr(e2)) |
| 1833 |
|
| 1834 |
def test_interpolationdeptherror(self): |
| 1835 |
import pickle |
| 1836 |
e1 = configparser.InterpolationDepthError('option', 'section', |
| 1837 |
'rawval') |
| 1838 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1839 |
pickled = pickle.dumps(e1, proto) |
| 1840 |
e2 = pickle.loads(pickled) |
| 1841 |
self.assertEqual(e1.message, e2.message) |
| 1842 |
self.assertEqual(e1.args, e2.args) |
| 1843 |
self.assertEqual(e1.section, e2.section) |
| 1844 |
self.assertEqual(e1.option, e2.option) |
| 1845 |
self.assertEqual(repr(e1), repr(e2)) |
| 1846 |
|
| 1847 |
def test_parsingerror(self): |
| 1848 |
import pickle |
| 1849 |
e1 = configparser.ParsingError('source') |
| 1850 |
e1.append(1, 'line1') |
| 1851 |
e1.append(2, 'line2') |
| 1852 |
e1.append(3, 'line3') |
| 1853 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1854 |
pickled = pickle.dumps(e1, proto) |
| 1855 |
e2 = pickle.loads(pickled) |
| 1856 |
self.assertEqual(e1.message, e2.message) |
| 1857 |
self.assertEqual(e1.args, e2.args) |
| 1858 |
self.assertEqual(e1.source, e2.source) |
| 1859 |
self.assertEqual(e1.errors, e2.errors) |
| 1860 |
self.assertEqual(repr(e1), repr(e2)) |
| 1861 |
e1 = configparser.ParsingError('filename') |
| 1862 |
e1.append(1, 'line1') |
| 1863 |
e1.append(2, 'line2') |
| 1864 |
e1.append(3, 'line3') |
| 1865 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1866 |
pickled = pickle.dumps(e1, proto) |
| 1867 |
e2 = pickle.loads(pickled) |
| 1868 |
self.assertEqual(e1.message, e2.message) |
| 1869 |
self.assertEqual(e1.args, e2.args) |
| 1870 |
self.assertEqual(e1.source, e2.source) |
| 1871 |
self.assertEqual(e1.errors, e2.errors) |
| 1872 |
self.assertEqual(repr(e1), repr(e2)) |
| 1873 |
|
| 1874 |
def test_missingsectionheadererror(self): |
| 1875 |
import pickle |
| 1876 |
e1 = configparser.MissingSectionHeaderError('filename', 123, 'line') |
| 1877 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 1878 |
pickled = pickle.dumps(e1, proto) |
| 1879 |
e2 = pickle.loads(pickled) |
| 1880 |
self.assertEqual(e1.message, e2.message) |
| 1881 |
self.assertEqual(e1.args, e2.args) |
| 1882 |
self.assertEqual(e1.line, e2.line) |
| 1883 |
self.assertEqual(e1.source, e2.source) |
| 1884 |
self.assertEqual(e1.lineno, e2.lineno) |
| 1885 |
self.assertEqual(repr(e1), repr(e2)) |
| 1886 |
|
| 1887 |
|
| 1888 |
class InlineCommentStrippingTestCase(unittest.TestCase): |
| 1889 |
"""Tests for issue #14590: ConfigParser doesn't strip inline comment when |
| 1890 |
delimiter occurs earlier without preceding space..""" |
| 1891 |
|
| 1892 |
def test_stripping(self): |
| 1893 |
cfg = configparser.ConfigParser(inline_comment_prefixes=(';', '#', |
| 1894 |
'//')) |
| 1895 |
cfg.read_string(""" |
| 1896 |
[section] |
| 1897 |
k1 = v1;still v1 |
| 1898 |
k2 = v2 ;a comment |
| 1899 |
k3 = v3 ; also a comment |
| 1900 |
k4 = v4;still v4 ;a comment |
| 1901 |
k5 = v5;still v5 ; also a comment |
| 1902 |
k6 = v6;still v6; and still v6 ;a comment |
| 1903 |
k7 = v7;still v7; and still v7 ; also a comment |
| 1904 |
|
| 1905 |
[multiprefix] |
| 1906 |
k1 = v1;still v1 #a comment ; yeah, pretty much |
| 1907 |
k2 = v2 // this already is a comment ; continued |
| 1908 |
k3 = v3;#//still v3# and still v3 ; a comment |
| 1909 |
""") |
| 1910 |
s = cfg['section'] |
| 1911 |
self.assertEqual(s['k1'], 'v1;still v1') |
| 1912 |
self.assertEqual(s['k2'], 'v2') |
| 1913 |
self.assertEqual(s['k3'], 'v3') |
| 1914 |
self.assertEqual(s['k4'], 'v4;still v4') |
| 1915 |
self.assertEqual(s['k5'], 'v5;still v5') |
| 1916 |
self.assertEqual(s['k6'], 'v6;still v6; and still v6') |
| 1917 |
self.assertEqual(s['k7'], 'v7;still v7; and still v7') |
| 1918 |
s = cfg['multiprefix'] |
| 1919 |
self.assertEqual(s['k1'], 'v1;still v1') |
| 1920 |
self.assertEqual(s['k2'], 'v2') |
| 1921 |
self.assertEqual(s['k3'], 'v3;#//still v3# and still v3') |
| 1922 |
|
| 1923 |
|
| 1924 |
class ExceptionContextTestCase(unittest.TestCase): |
| 1925 |
""" Test that implementation details doesn't leak |
| 1926 |
through raising exceptions. """ |
| 1927 |
|
| 1928 |
def test_get_basic_interpolation(self): |
| 1929 |
parser = configparser.ConfigParser() |
| 1930 |
parser.read_string(""" |
| 1931 |
[Paths] |
| 1932 |
home_dir: /Users |
| 1933 |
my_dir: %(home_dir1)s/lumberjack |
| 1934 |
my_pictures: %(my_dir)s/Pictures |
| 1935 |
""") |
| 1936 |
cm = self.assertRaises(configparser.InterpolationMissingOptionError) |
| 1937 |
with cm: |
| 1938 |
parser.get('Paths', 'my_dir') |
| 1939 |
self.assertIs(cm.exception.__suppress_context__, True) |
| 1940 |
|
| 1941 |
def test_get_extended_interpolation(self): |
| 1942 |
parser = configparser.ConfigParser( |
| 1943 |
interpolation=configparser.ExtendedInterpolation()) |
| 1944 |
parser.read_string(""" |
| 1945 |
[Paths] |
| 1946 |
home_dir: /Users |
| 1947 |
my_dir: ${home_dir1}/lumberjack |
| 1948 |
my_pictures: ${my_dir}/Pictures |
| 1949 |
""") |
| 1950 |
cm = self.assertRaises(configparser.InterpolationMissingOptionError) |
| 1951 |
with cm: |
| 1952 |
parser.get('Paths', 'my_dir') |
| 1953 |
self.assertIs(cm.exception.__suppress_context__, True) |
| 1954 |
|
| 1955 |
def test_missing_options(self): |
| 1956 |
parser = configparser.ConfigParser() |
| 1957 |
parser.read_string(""" |
| 1958 |
[Paths] |
| 1959 |
home_dir: /Users |
| 1960 |
""") |
| 1961 |
with self.assertRaises(configparser.NoSectionError) as cm: |
| 1962 |
parser.options('test') |
| 1963 |
self.assertIs(cm.exception.__suppress_context__, True) |
| 1964 |
|
| 1965 |
def test_missing_section(self): |
| 1966 |
config = configparser.ConfigParser() |
| 1967 |
with self.assertRaises(configparser.NoSectionError) as cm: |
| 1968 |
config.set('Section1', 'an_int', '15') |
| 1969 |
self.assertIs(cm.exception.__suppress_context__, True) |
| 1970 |
|
| 1971 |
def test_remove_option(self): |
| 1972 |
config = configparser.ConfigParser() |
| 1973 |
with self.assertRaises(configparser.NoSectionError) as cm: |
| 1974 |
config.remove_option('Section1', 'an_int') |
| 1975 |
self.assertIs(cm.exception.__suppress_context__, True) |
| 1976 |
|
| 1977 |
|
| 1978 |
class ConvertersTestCase(BasicTestCase, unittest.TestCase): |
| 1979 |
"""Introduced in 3.5, issue #18159.""" |
| 1980 |
|
| 1981 |
config_class = configparser.ConfigParser |
| 1982 |
|
| 1983 |
def newconfig(self, defaults=None): |
| 1984 |
instance = super().newconfig(defaults=defaults) |
| 1985 |
instance.converters['list'] = lambda v: [e.strip() for e in v.split() |
| 1986 |
if e.strip()] |
| 1987 |
return instance |
| 1988 |
|
| 1989 |
def test_converters(self): |
| 1990 |
cfg = self.newconfig() |
| 1991 |
self.assertIn('boolean', cfg.converters) |
| 1992 |
self.assertIn('list', cfg.converters) |
| 1993 |
self.assertIsNone(cfg.converters['int']) |
| 1994 |
self.assertIsNone(cfg.converters['float']) |
| 1995 |
self.assertIsNone(cfg.converters['boolean']) |
| 1996 |
self.assertIsNotNone(cfg.converters['list']) |
| 1997 |
self.assertEqual(len(cfg.converters), 4) |
| 1998 |
with self.assertRaises(ValueError): |
| 1999 |
cfg.converters[''] = lambda v: v |
| 2000 |
with self.assertRaises(ValueError): |
| 2001 |
cfg.converters[None] = lambda v: v |
| 2002 |
cfg.read_string(""" |
| 2003 |
[s] |
| 2004 |
str = string |
| 2005 |
int = 1 |
| 2006 |
float = 0.5 |
| 2007 |
list = a b c d e f g |
| 2008 |
bool = yes |
| 2009 |
""") |
| 2010 |
s = cfg['s'] |
| 2011 |
self.assertEqual(s['str'], 'string') |
| 2012 |
self.assertEqual(s['int'], '1') |
| 2013 |
self.assertEqual(s['float'], '0.5') |
| 2014 |
self.assertEqual(s['list'], 'a b c d e f g') |
| 2015 |
self.assertEqual(s['bool'], 'yes') |
| 2016 |
self.assertEqual(cfg.get('s', 'str'), 'string') |
| 2017 |
self.assertEqual(cfg.get('s', 'int'), '1') |
| 2018 |
self.assertEqual(cfg.get('s', 'float'), '0.5') |
| 2019 |
self.assertEqual(cfg.get('s', 'list'), 'a b c d e f g') |
| 2020 |
self.assertEqual(cfg.get('s', 'bool'), 'yes') |
| 2021 |
self.assertEqual(cfg.get('s', 'str'), 'string') |
| 2022 |
self.assertEqual(cfg.getint('s', 'int'), 1) |
| 2023 |
self.assertEqual(cfg.getfloat('s', 'float'), 0.5) |
| 2024 |
self.assertEqual(cfg.getlist('s', 'list'), ['a', 'b', 'c', 'd', |
| 2025 |
'e', 'f', 'g']) |
| 2026 |
self.assertEqual(cfg.getboolean('s', 'bool'), True) |
| 2027 |
self.assertEqual(s.get('str'), 'string') |
| 2028 |
self.assertEqual(s.getint('int'), 1) |
| 2029 |
self.assertEqual(s.getfloat('float'), 0.5) |
| 2030 |
self.assertEqual(s.getlist('list'), ['a', 'b', 'c', 'd', |
| 2031 |
'e', 'f', 'g']) |
| 2032 |
self.assertEqual(s.getboolean('bool'), True) |
| 2033 |
with self.assertRaises(AttributeError): |
| 2034 |
cfg.getdecimal('s', 'float') |
| 2035 |
with self.assertRaises(AttributeError): |
| 2036 |
s.getdecimal('float') |
| 2037 |
import decimal |
| 2038 |
cfg.converters['decimal'] = decimal.Decimal |
| 2039 |
self.assertIn('decimal', cfg.converters) |
| 2040 |
self.assertIsNotNone(cfg.converters['decimal']) |
| 2041 |
self.assertEqual(len(cfg.converters), 5) |
| 2042 |
dec0_5 = decimal.Decimal('0.5') |
| 2043 |
self.assertEqual(cfg.getdecimal('s', 'float'), dec0_5) |
| 2044 |
self.assertEqual(s.getdecimal('float'), dec0_5) |
| 2045 |
del cfg.converters['decimal'] |
| 2046 |
self.assertNotIn('decimal', cfg.converters) |
| 2047 |
self.assertEqual(len(cfg.converters), 4) |
| 2048 |
with self.assertRaises(AttributeError): |
| 2049 |
cfg.getdecimal('s', 'float') |
| 2050 |
with self.assertRaises(AttributeError): |
| 2051 |
s.getdecimal('float') |
| 2052 |
with self.assertRaises(KeyError): |
| 2053 |
del cfg.converters['decimal'] |
| 2054 |
with self.assertRaises(KeyError): |
| 2055 |
del cfg.converters[''] |
| 2056 |
with self.assertRaises(KeyError): |
| 2057 |
del cfg.converters[None] |
| 2058 |
|
| 2059 |
|
| 2060 |
class BlatantOverrideConvertersTestCase(unittest.TestCase): |
| 2061 |
"""What if somebody overrode a getboolean()? We want to make sure that in |
| 2062 |
this case the automatic converters do not kick in.""" |
| 2063 |
|
| 2064 |
config = """ |
| 2065 |
[one] |
| 2066 |
one = false |
| 2067 |
two = false |
| 2068 |
three = long story short |
| 2069 |
|
| 2070 |
[two] |
| 2071 |
one = false |
| 2072 |
two = false |
| 2073 |
three = four |
| 2074 |
""" |
| 2075 |
|
| 2076 |
def test_converters_at_init(self): |
| 2077 |
cfg = configparser.ConfigParser(converters={'len': len}) |
| 2078 |
cfg.read_string(self.config) |
| 2079 |
self._test_len(cfg) |
| 2080 |
self.assertIsNotNone(cfg.converters['len']) |
| 2081 |
|
| 2082 |
def test_inheritance(self): |
| 2083 |
class StrangeConfigParser(configparser.ConfigParser): |
| 2084 |
gettysburg = 'a historic borough in south central Pennsylvania' |
| 2085 |
|
| 2086 |
def getboolean(self, section, option, *, raw=False, vars=None, |
| 2087 |
fallback=configparser._UNSET): |
| 2088 |
if section == option: |
| 2089 |
return True |
| 2090 |
return super().getboolean(section, option, raw=raw, vars=vars, |
| 2091 |
fallback=fallback) |
| 2092 |
def getlen(self, section, option, *, raw=False, vars=None, |
| 2093 |
fallback=configparser._UNSET): |
| 2094 |
return self._get_conv(section, option, len, raw=raw, vars=vars, |
| 2095 |
fallback=fallback) |
| 2096 |
|
| 2097 |
cfg = StrangeConfigParser() |
| 2098 |
cfg.read_string(self.config) |
| 2099 |
self._test_len(cfg) |
| 2100 |
self.assertIsNone(cfg.converters['len']) |
| 2101 |
self.assertTrue(cfg.getboolean('one', 'one')) |
| 2102 |
self.assertTrue(cfg.getboolean('two', 'two')) |
| 2103 |
self.assertFalse(cfg.getboolean('one', 'two')) |
| 2104 |
self.assertFalse(cfg.getboolean('two', 'one')) |
| 2105 |
cfg.converters['boolean'] = cfg._convert_to_boolean |
| 2106 |
self.assertFalse(cfg.getboolean('one', 'one')) |
| 2107 |
self.assertFalse(cfg.getboolean('two', 'two')) |
| 2108 |
self.assertFalse(cfg.getboolean('one', 'two')) |
| 2109 |
self.assertFalse(cfg.getboolean('two', 'one')) |
| 2110 |
|
| 2111 |
def _test_len(self, cfg): |
| 2112 |
self.assertEqual(len(cfg.converters), 4) |
| 2113 |
self.assertIn('boolean', cfg.converters) |
| 2114 |
self.assertIn('len', cfg.converters) |
| 2115 |
self.assertNotIn('tysburg', cfg.converters) |
| 2116 |
self.assertIsNone(cfg.converters['int']) |
| 2117 |
self.assertIsNone(cfg.converters['float']) |
| 2118 |
self.assertIsNone(cfg.converters['boolean']) |
| 2119 |
self.assertEqual(cfg.getlen('one', 'one'), 5) |
| 2120 |
self.assertEqual(cfg.getlen('one', 'two'), 5) |
| 2121 |
self.assertEqual(cfg.getlen('one', 'three'), 16) |
| 2122 |
self.assertEqual(cfg.getlen('two', 'one'), 5) |
| 2123 |
self.assertEqual(cfg.getlen('two', 'two'), 5) |
| 2124 |
self.assertEqual(cfg.getlen('two', 'three'), 4) |
| 2125 |
self.assertEqual(cfg.getlen('two', 'four', fallback=0), 0) |
| 2126 |
with self.assertRaises(configparser.NoOptionError): |
| 2127 |
cfg.getlen('two', 'four') |
| 2128 |
self.assertEqual(cfg['one'].getlen('one'), 5) |
| 2129 |
self.assertEqual(cfg['one'].getlen('two'), 5) |
| 2130 |
self.assertEqual(cfg['one'].getlen('three'), 16) |
| 2131 |
self.assertEqual(cfg['two'].getlen('one'), 5) |
| 2132 |
self.assertEqual(cfg['two'].getlen('two'), 5) |
| 2133 |
self.assertEqual(cfg['two'].getlen('three'), 4) |
| 2134 |
self.assertEqual(cfg['two'].getlen('four', 0), 0) |
| 2135 |
self.assertEqual(cfg['two'].getlen('four'), None) |
| 2136 |
|
| 2137 |
def test_instance_assignment(self): |
| 2138 |
cfg = configparser.ConfigParser() |
| 2139 |
cfg.getboolean = lambda section, option: True |
| 2140 |
cfg.getlen = lambda section, option: len(cfg[section][option]) |
| 2141 |
cfg.read_string(self.config) |
| 2142 |
self.assertEqual(len(cfg.converters), 3) |
| 2143 |
self.assertIn('boolean', cfg.converters) |
| 2144 |
self.assertNotIn('len', cfg.converters) |
| 2145 |
self.assertIsNone(cfg.converters['int']) |
| 2146 |
self.assertIsNone(cfg.converters['float']) |
| 2147 |
self.assertIsNone(cfg.converters['boolean']) |
| 2148 |
self.assertTrue(cfg.getboolean('one', 'one')) |
| 2149 |
self.assertTrue(cfg.getboolean('two', 'two')) |
| 2150 |
self.assertTrue(cfg.getboolean('one', 'two')) |
| 2151 |
self.assertTrue(cfg.getboolean('two', 'one')) |
| 2152 |
cfg.converters['boolean'] = cfg._convert_to_boolean |
| 2153 |
self.assertFalse(cfg.getboolean('one', 'one')) |
| 2154 |
self.assertFalse(cfg.getboolean('two', 'two')) |
| 2155 |
self.assertFalse(cfg.getboolean('one', 'two')) |
| 2156 |
self.assertFalse(cfg.getboolean('two', 'one')) |
| 2157 |
self.assertEqual(cfg.getlen('one', 'one'), 5) |
| 2158 |
self.assertEqual(cfg.getlen('one', 'two'), 5) |
| 2159 |
self.assertEqual(cfg.getlen('one', 'three'), 16) |
| 2160 |
self.assertEqual(cfg.getlen('two', 'one'), 5) |
| 2161 |
self.assertEqual(cfg.getlen('two', 'two'), 5) |
| 2162 |
self.assertEqual(cfg.getlen('two', 'three'), 4) |
| 2163 |
# If a getter impl is assigned straight to the instance, it won't |
| 2164 |
# be available on the section proxies. |
| 2165 |
with self.assertRaises(AttributeError): |
| 2166 |
self.assertEqual(cfg['one'].getlen('one'), 5) |
| 2167 |
with self.assertRaises(AttributeError): |
| 2168 |
self.assertEqual(cfg['two'].getlen('one'), 5) |
| 2169 |
|
| 2170 |
|
| 2171 |
class SectionlessTestCase(unittest.TestCase): |
| 2172 |
|
| 2173 |
def fromstring(self, string): |
| 2174 |
cfg = configparser.ConfigParser(allow_unnamed_section=True) |
| 2175 |
cfg.read_string(string) |
| 2176 |
return cfg |
| 2177 |
|
| 2178 |
def test_no_first_section(self): |
| 2179 |
cfg1 = self.fromstring(""" |
| 2180 |
a = 1 |
| 2181 |
b = 2 |
| 2182 |
[sect1] |
| 2183 |
c = 3 |
| 2184 |
""") |
| 2185 |
|
| 2186 |
self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg1.sections())) |
| 2187 |
self.assertEqual('1', cfg1[configparser.UNNAMED_SECTION]['a']) |
| 2188 |
self.assertEqual('2', cfg1[configparser.UNNAMED_SECTION]['b']) |
| 2189 |
self.assertEqual('3', cfg1['sect1']['c']) |
| 2190 |
|
| 2191 |
output = io.StringIO() |
| 2192 |
cfg1.write(output) |
| 2193 |
cfg2 = self.fromstring(output.getvalue()) |
| 2194 |
|
| 2195 |
#self.assertEqual(set([configparser.UNNAMED_SECTION, 'sect1']), set(cfg2.sections())) |
| 2196 |
self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a']) |
| 2197 |
self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b']) |
| 2198 |
self.assertEqual('3', cfg2['sect1']['c']) |
| 2199 |
|
| 2200 |
def test_no_section(self): |
| 2201 |
cfg1 = self.fromstring(""" |
| 2202 |
a = 1 |
| 2203 |
b = 2 |
| 2204 |
""") |
| 2205 |
|
| 2206 |
self.assertEqual([configparser.UNNAMED_SECTION], cfg1.sections()) |
| 2207 |
self.assertEqual('1', cfg1[configparser.UNNAMED_SECTION]['a']) |
| 2208 |
self.assertEqual('2', cfg1[configparser.UNNAMED_SECTION]['b']) |
| 2209 |
|
| 2210 |
output = io.StringIO() |
| 2211 |
cfg1.write(output) |
| 2212 |
cfg2 = self.fromstring(output.getvalue()) |
| 2213 |
|
| 2214 |
self.assertEqual([configparser.UNNAMED_SECTION], cfg2.sections()) |
| 2215 |
self.assertEqual('1', cfg2[configparser.UNNAMED_SECTION]['a']) |
| 2216 |
self.assertEqual('2', cfg2[configparser.UNNAMED_SECTION]['b']) |
| 2217 |
|
| 2218 |
def test_empty_unnamed_section(self): |
| 2219 |
cfg = configparser.ConfigParser(allow_unnamed_section=True) |
| 2220 |
cfg.add_section(configparser.UNNAMED_SECTION) |
| 2221 |
cfg.add_section('section') |
| 2222 |
output = io.StringIO() |
| 2223 |
cfg.write(output) |
| 2224 |
self.assertEqual(output.getvalue(), '[section]\n\n') |
| 2225 |
|
| 2226 |
def test_add_section(self): |
| 2227 |
cfg = configparser.ConfigParser(allow_unnamed_section=True) |
| 2228 |
cfg.add_section(configparser.UNNAMED_SECTION) |
| 2229 |
cfg.set(configparser.UNNAMED_SECTION, 'a', '1') |
| 2230 |
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a']) |
| 2231 |
output = io.StringIO() |
| 2232 |
cfg.write(output) |
| 2233 |
self.assertEqual(output.getvalue(), 'a = 1\n\n') |
| 2234 |
|
| 2235 |
cfg = configparser.ConfigParser(allow_unnamed_section=True) |
| 2236 |
cfg[configparser.UNNAMED_SECTION] = {'a': '1'} |
| 2237 |
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a']) |
| 2238 |
output = io.StringIO() |
| 2239 |
cfg.write(output) |
| 2240 |
self.assertEqual(output.getvalue(), 'a = 1\n\n') |
| 2241 |
|
| 2242 |
def test_disabled_error(self): |
| 2243 |
with self.assertRaises(configparser.MissingSectionHeaderError): |
| 2244 |
configparser.ConfigParser().read_string("a = 1") |
| 2245 |
|
| 2246 |
with self.assertRaises(configparser.UnnamedSectionDisabledError): |
| 2247 |
configparser.ConfigParser().add_section(configparser.UNNAMED_SECTION) |
| 2248 |
|
| 2249 |
with self.assertRaises(configparser.UnnamedSectionDisabledError): |
| 2250 |
configparser.ConfigParser()[configparser.UNNAMED_SECTION] = {'a': '1'} |
| 2251 |
|
| 2252 |
def test_multiple_configs(self): |
| 2253 |
cfg = configparser.ConfigParser(allow_unnamed_section=True) |
| 2254 |
cfg.read_string('a = 1') |
| 2255 |
cfg.read_string('b = 2') |
| 2256 |
|
| 2257 |
self.assertEqual([configparser.UNNAMED_SECTION], cfg.sections()) |
| 2258 |
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a']) |
| 2259 |
self.assertEqual('2', cfg[configparser.UNNAMED_SECTION]['b']) |
| 2260 |
|
| 2261 |
|
| 2262 |
class InvalidInputTestCase(unittest.TestCase): |
| 2263 |
"""Tests for issue #65697, where configparser will write configs |
| 2264 |
it parses back differently. Ex: keys containing delimiters or |
| 2265 |
matching the section pattern""" |
| 2266 |
|
| 2267 |
def test_delimiter_in_key(self): |
| 2268 |
cfg = configparser.ConfigParser(delimiters=('=')) |
| 2269 |
cfg.add_section('section1') |
| 2270 |
cfg.set('section1', 'a=b', 'c') |
| 2271 |
output = io.StringIO() |
| 2272 |
with self.assertRaises(configparser.InvalidWriteError): |
| 2273 |
cfg.write(output) |
| 2274 |
output.close() |
| 2275 |
|
| 2276 |
def test_section_bracket_in_key(self): |
| 2277 |
cfg = configparser.ConfigParser() |
| 2278 |
cfg.add_section('section1') |
| 2279 |
cfg.set('section1', '[this parses back as a section]', 'foo') |
| 2280 |
output = io.StringIO() |
| 2281 |
with self.assertRaises(configparser.InvalidWriteError): |
| 2282 |
cfg.write(output) |
| 2283 |
output.close() |
| 2284 |
|
| 2285 |
|
| 2286 |
class ReDoSTestCase(unittest.TestCase): |
| 2287 |
"""Regression tests for quadratic regex backtracking (gh-146333).""" |
| 2288 |
|
| 2289 |
def test_option_regex_does_not_backtrack(self): |
| 2290 |
# A line with many spaces between non-delimiter characters |
| 2291 |
# should be parsed in linear time, not quadratic. |
| 2292 |
parser = configparser.RawConfigParser() |
| 2293 |
content = "[section]\n" + "x" + " " * 40000 + "y" + "\n" |
| 2294 |
# This should complete almost instantly. Before the fix, |
| 2295 |
# it would take over a minute due to catastrophic backtracking. |
| 2296 |
with self.assertRaises(configparser.ParsingError): |
| 2297 |
parser.read_string(content) |
| 2298 |
|
| 2299 |
def test_option_regex_no_value_does_not_backtrack(self): |
| 2300 |
parser = configparser.RawConfigParser(allow_no_value=True) |
| 2301 |
content = "[section]\n" + "x" + " " * 40000 + "y" + "\n" |
| 2302 |
parser.read_string(content) |
| 2303 |
self.assertTrue(parser.has_option("section", "x" + " " * 40000 + "y")) |
| 2304 |
|
| 2305 |
|
| 2306 |
class MiscTestCase(unittest.TestCase): |
| 2307 |
def test__all__(self): |
| 2308 |
support.check__all__(self, configparser, not_exported={"Error"}) |
| 2309 |
|
| 2310 |
|
| 2311 |
if __name__ == '__main__': |
| 2312 |
unittest.main() |