diff options
author | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2016-04-01 11:20:30 +0900 |
---|---|---|
committer | Takeshi KOMIYA <i.tkomiya@gmail.com> | 2016-04-01 11:20:30 +0900 |
commit | 8ab06530be89e3f42c43899b43872b0df1dd5a3d (patch) | |
tree | 2c035f673930a0edd531dab6238fccab654dc613 | |
parent | 42395a177adaa92c4955248d9b96f337e8923bd8 (diff) | |
download | sphinx-git-8ab06530be89e3f42c43899b43872b0df1dd5a3d.tar.gz |
Fix #2394: Sphinx crashes when html_last_updated_fmt is invalid
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | sphinx/builders/epub.py | 3 | ||||
-rw-r--r-- | sphinx/builders/html.py | 3 | ||||
-rw-r--r-- | sphinx/transforms.py | 3 | ||||
-rw-r--r-- | sphinx/util/i18n.py | 12 | ||||
-rw-r--r-- | tests/test_util_i18n.py | 8 |
6 files changed, 24 insertions, 6 deletions
@@ -16,6 +16,7 @@ Bugs fixed * C++, added support for ``extern`` and ``thread_local``. * C++, type declarations are now using the prefixes ``typedef``, ``using``, and ``type``, depending on the style of declaration. +* #2394: Fix Sphinx crashes when html_last_updated_fmt is invalid Release 1.4 (released Mar 28, 2016) diff --git a/sphinx/builders/epub.py b/sphinx/builders/epub.py index d1610bda9..265421d2c 100644 --- a/sphinx/builders/epub.py +++ b/sphinx/builders/epub.py @@ -530,7 +530,8 @@ class EpubBuilder(StandaloneHTMLBuilder): metadata['copyright'] = self.esc(self.config.epub_copyright) metadata['scheme'] = self.esc(self.config.epub_scheme) metadata['id'] = self.esc(self.config.epub_identifier) - metadata['date'] = self.esc(format_date('YYYY-MM-dd', language=self.config.language)) + metadata['date'] = self.esc(format_date('YYYY-MM-dd', language=self.config.language, + warn=self.warn)) metadata['files'] = files metadata['spine'] = spine metadata['guide'] = guide diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 367e28b8d..1b3db1402 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -293,7 +293,8 @@ class StandaloneHTMLBuilder(Builder): lufmt = self.config.html_last_updated_fmt if lufmt is not None: self.last_updated = format_date(lufmt or _('MMM dd, YYYY'), - language=self.config.language) + language=self.config.language, + warn=self.warn) else: self.last_updated = None diff --git a/sphinx/transforms.py b/sphinx/transforms.py index 583271116..7272d3b19 100644 --- a/sphinx/transforms.py +++ b/sphinx/transforms.py @@ -44,6 +44,7 @@ class DefaultSubstitutions(Transform): default_priority = 210 def apply(self): + env = self.document.settings.env config = self.document.settings.env.config # only handle those not otherwise defined in the document to_handle = default_substitutions - set(self.document.substitution_defs) @@ -54,7 +55,7 @@ class DefaultSubstitutions(Transform): if refname == 'today' and not text: # special handling: can also specify a strftime format text = format_date(config.today_fmt or _('MMMM dd, YYYY'), - language=config.language) + language=config.language, warn=env.warn) ref.replace_self(nodes.Text(text, text)) diff --git a/sphinx/util/i18n.py b/sphinx/util/i18n.py index 94492ff45..5d98b115c 100644 --- a/sphinx/util/i18n.py +++ b/sphinx/util/i18n.py @@ -149,7 +149,7 @@ date_format_mappings = { } -def babel_format_date(date, format, locale): +def babel_format_date(date, format, locale, warn=None): if locale is None: locale = 'en' @@ -158,9 +158,15 @@ def babel_format_date(date, format, locale): except (ValueError, babel.core.UnknownLocaleError): # fallback to English return babel.dates.format_date(date, format, locale='en') + except AttributeError: + if warn: + warn('Invalid date format. Quote the string by single quote ' + 'if you want to output it directly: %s' % format) + return format -def format_date(format, date=None, language=None): + +def format_date(format, date=None, language=None, warn=None): if format is None: format = 'medium' @@ -175,7 +181,7 @@ def format_date(format, date=None, language=None): if '%' not in format: # consider the format as babel's - return babel_format_date(date, format, locale=language) + return babel_format_date(date, format, locale=language, warn=warn) else: warnings.warn('ustrftime format support will be dropped at Sphinx-1.5', DeprecationWarning) diff --git a/tests/test_util_i18n.py b/tests/test_util_i18n.py index e84892b5c..a534b2771 100644 --- a/tests/test_util_i18n.py +++ b/tests/test_util_i18n.py @@ -185,6 +185,14 @@ def test_format_date(): assert i18n.format_date(format, date=date, language='ja') == u'2月 07, 2016' assert i18n.format_date(format, date=date, language='de') == 'Februar 07, 2016' + # invalid date format + format = 'Mon Mar 28 12:37:08 2016, commit 4367aef' + assert i18n.format_date(format, date=date) == format + + # quoted format + quoted_format = "'Mon Mar 28 12:37:08 2016, commit 4367aef'" + assert i18n.format_date(quoted_format, date=date) == format + def test_get_filename_for_language(): app = TestApp() |