fix '{…!j}' for otherwise non-serializable types (##2624)
like 'datetime'
This commit is contained in:
@@ -14,6 +14,7 @@ import string
|
|||||||
import _string
|
import _string
|
||||||
import datetime
|
import datetime
|
||||||
import operator
|
import operator
|
||||||
|
import functools
|
||||||
from . import text, util
|
from . import text, util
|
||||||
|
|
||||||
_CACHE = {}
|
_CACHE = {}
|
||||||
@@ -257,7 +258,7 @@ def parse_format_spec(format_spec, conversion):
|
|||||||
"u": str.upper,
|
"u": str.upper,
|
||||||
"c": str.capitalize,
|
"c": str.capitalize,
|
||||||
"C": string.capwords,
|
"C": string.capwords,
|
||||||
"j": json.dumps,
|
"j": functools.partial(json.dumps, default=str),
|
||||||
"t": str.strip,
|
"t": str.strip,
|
||||||
"T": util.datetime_to_timestamp_string,
|
"T": util.datetime_to_timestamp_string,
|
||||||
"d": text.parse_timestamp,
|
"d": text.parse_timestamp,
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ class TestFormatter(unittest.TestCase):
|
|||||||
self._run_test("{t!d:%Y-%m-%d}", "2010-01-01")
|
self._run_test("{t!d:%Y-%m-%d}", "2010-01-01")
|
||||||
self._run_test("{dt!T}", "1262304000")
|
self._run_test("{dt!T}", "1262304000")
|
||||||
self._run_test("{l!j}", '["a", "b", "c"]')
|
self._run_test("{l!j}", '["a", "b", "c"]')
|
||||||
|
self._run_test("{dt!j}", '"2010-01-01 00:00:00"')
|
||||||
|
|
||||||
with self.assertRaises(KeyError):
|
with self.assertRaises(KeyError):
|
||||||
self._run_test("{a!q}", "hello world")
|
self._run_test("{a!q}", "hello world")
|
||||||
|
|||||||
Reference in New Issue
Block a user