Skip to content

Commit c76a8c5

Browse files
committed
config.get_value: Added default argument including test
1 parent 3326f34 commit c76a8c5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/git/config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,21 @@ def read_only(self):
347347
"""
348348
return self._read_only
349349

350-
def get_value(self, section, option):
350+
def get_value(self, section, option, default = None):
351351
"""
352+
``default``
353+
If not None, the given default value will be returned in case
354+
the option did not exist
352355
Returns
353356
a properly typed value, either int, float or string
354357
Raises TypeError in case the value could not be understood
358+
Otherwise the exceptions known to the ConfigParser will be raised.
355359
"""
356-
valuestr = self.get(section, option)
360+
try:
361+
valuestr = self.get(section, option)
362+
except Exception:
363+
return default
364+
357365
types = ( long, float )
358366
for numtype in types:
359367
try:

test/git/test_config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,8 @@ def test_base(self):
8787
assert num_sections and num_options
8888
assert r_config._is_initialized == True
8989

90+
# get value which doesnt exist, with default
91+
default = "my default value"
92+
assert r_config.get_value("doesnt", "exist", default) == default
93+
94+

0 commit comments

Comments
 (0)