我对 python configobj 有疑问。为什么在 config.validate(validator, preserve_errors=True) 期间将来自 cs 的“参数”(命名参数)添加到配置中?
def test_MissingSectionError(self):
cfg = '''
[general]
name = TestInteractionExample 1
[this_is_not_para_meter]
[[frequency]]
default_value = 900
description = Description of parameter1
'''
cs = '''
[general]
name = string
[parameter]
[[__many__]]
default_value =
description =
'''
cfg_io = StringIO(cfg)
cs_io = StringIO(cs)
config = self.parser._get_configobj(cfg_io, cs_io, self.script)
print 'Before validation: {}'.format(config)
validator = Validator()
results = config.validate(validator, preserve_errors=True)
print 'After validation: {}'.format(config)
print 'result: {}'.format(results)
# self.assertRaises(MissingSectionError, self.parser._validate_config, config, self.script)
打印给出:
验证前:{u'general': {u'name': u'TestInteractionExample 1'}, u'this_is_not_para_meter': {u'frequency': {u'default_value': u'900', u'description': u 'Description of parameter1'}}} 验证后:{u'general': {u'name': u'TestInteractionExample 1'}, u'this_is_not_para_meter': {u'frequency': {u'default_value': u'900 ', u'description': u'description of parameter1'}}, 'parameter': {}} 结果:True
奇怪的是,这验证为真,因为配置中缺少“参数”。但同样,如果它在 config.validate(validator, preserve_errors=True) 中添加到配置中,那么它也会变为 true。也许有一些选项可以将缺失值从 cs 复制到配置。