Skip to content

Commit 010f6ab

Browse files
authored
Merge pull request #2281 from xiehuanyi/fix-config-integer-precision-20261011
fix(config): preserve exact large integer values
2 parents e296473 + 04fa56f commit 010f6ab

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎git/config.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,8 +1018,8 @@ def _string_to_value(self, valuestr: Union[str, None]) -> Union[int, float, str,
10181018
for numtype in types:
10191019
try:
10201020
val = numtype(valuestr)
1021-
# truncated value ?
1022-
if val != float(valuestr):
1021+
# Reject NaN without coercing exact integer values to float.
1022+
if numtype is float and val != float(valuestr):
10231023
continue
10241024
return val
10251025
except (ValueError, TypeError):

‎test/test_config.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,38 @@ def tearDown(self):
3737
if osp.isfile(lfp):
3838
raise AssertionError("Previous TC left hanging git-lock file: {}".format(lfp))
3939

40+
def test_integer_config_values_keep_precision(self):
41+
for text, expected in (
42+
("9007199254740993", 9007199254740993),
43+
("9223372036854775807", 9223372036854775807),
44+
("-9007199254740993", -9007199254740993),
45+
):
46+
with self.subTest(text=text):
47+
config_file = io.BytesIO(("[limits]\nvalue = %s\n" % text).encode("ascii"))
48+
config_file.name = "integer_precision.config"
49+
with GitConfigParser(config_file) as config:
50+
value = config.get_value("limits", "value")
51+
self.assertEqual(value, expected)
52+
self.assertIs(type(value), int)
53+
self.assertEqual(config.get_values("limits", "value"), [expected])
54+
55+
def test_numeric_config_conversion_preserves_other_values(self):
56+
for text, expected in (
57+
("11.11", 11.11),
58+
("1e3", 1000.0),
59+
("nan", "nan"),
60+
("NaN", "NaN"),
61+
("true", True),
62+
("false", False),
63+
):
64+
with self.subTest(text=text):
65+
config_file = io.BytesIO(("[values]\nvalue = %s\n" % text).encode("ascii"))
66+
config_file.name = "numeric_conversion.config"
67+
with GitConfigParser(config_file) as config:
68+
value = config.get_value("values", "value")
69+
self.assertEqual(value, expected)
70+
self.assertIs(type(value), type(expected))
71+
4072
def _to_memcache(self, file_path):
4173
with open(file_path, "rb") as fp:
4274
sio = io.BytesIO(fp.read())

0 commit comments

Comments
 (0)