diff options
author | Abseil Team <absl-team@google.com> | 2021-02-12 12:31:03 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2021-02-12 12:31:30 -0800 |
commit | 73b4b0f5e3e52caa7c026c3742e4abd6a52dffc8 (patch) | |
tree | a707ff59c9e54c7b75270c717fecd8d280013ee9 /absl | |
parent | 2444faee6d820c024c26380f60aa5e12fdf4b3a4 (diff) | |
download | absl-py-73b4b0f5e3e52caa7c026c3742e4abd6a52dffc8.tar.gz |
Fix CsvListSerializer to honor its list_sep parameter.
PiperOrigin-RevId: 357245847
Change-Id: I7637199f61f23e78646720ad406f539c89962fe3
Diffstat (limited to 'absl')
-rw-r--r-- | absl/CHANGELOG.md | 4 | ||||
-rw-r--r-- | absl/flags/_argument_parser.py | 6 | ||||
-rw-r--r-- | absl/flags/tests/_argument_parser_test.py | 7 |
3 files changed, 15 insertions, 2 deletions
diff --git a/absl/CHANGELOG.md b/absl/CHANGELOG.md index 2710593..9ff1924 100644 --- a/absl/CHANGELOG.md +++ b/absl/CHANGELOG.md @@ -16,6 +16,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com). product of parameters values, specified as a sequences of values for each parameter or as kwargs-like dicts of parameter values. +### Fixed + +* (flags) Made `CsvListSerializer` respect its delimiter argument. + ## 0.11.0 (2020-10-27) ### Changed diff --git a/absl/flags/_argument_parser.py b/absl/flags/_argument_parser.py index a706191..3d316ab 100644 --- a/absl/flags/_argument_parser.py +++ b/absl/flags/_argument_parser.py @@ -499,12 +499,14 @@ class CsvListSerializer(ArgumentSerializer): if six.PY2: # In Python2 csv.writer doesn't accept unicode, so we convert to UTF-8. output = io.BytesIO() - csv.writer(output).writerow([unicode(x).encode('utf-8') for x in value]) + writer = csv.writer(output, delimiter=self.list_sep) + writer.writerow([unicode(x).encode('utf-8') for x in value]) # pylint: disable=undefined-variable serialized_value = output.getvalue().decode('utf-8').strip() else: # In Python3 csv.writer expects a text stream. output = io.StringIO() - csv.writer(output).writerow([str(x) for x in value]) + writer = csv.writer(output, delimiter=self.list_sep) + writer.writerow([str(x) for x in value]) serialized_value = output.getvalue().strip() # We need the returned value to be pure ascii or Unicodes so that diff --git a/absl/flags/tests/_argument_parser_test.py b/absl/flags/tests/_argument_parser_test.py index e62c903..4074669 100644 --- a/absl/flags/tests/_argument_parser_test.py +++ b/absl/flags/tests/_argument_parser_test.py @@ -190,6 +190,13 @@ class EnumClassParserTest(parameterized.TestCase): self.assertEqual(value, parser.parse(expected)) +class SerializerTest(absltest.TestCase): + + def test_csv_serializer(self): + serializer = _argument_parser.CsvListSerializer('+') + self.assertEqual(serializer.serialize(['foo', 'bar']), 'foo+bar') + + class HelperFunctionsTest(absltest.TestCase): def test_is_integer_type(self): |