diff --git a/sphinx/config.py b/sphinx/config.py index 441da2fee..25450c156 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -22,6 +22,7 @@ from sphinx.util.pycompat import execfile_, NoneType from sphinx.util.i18n import format_date nonascii_re = re.compile(br'[\x80-\xff]') +copyright_year_re = re.compile(br'^((\d{4}-)?)(\d{4})(?=[ ,])') CONFIG_SYNTAX_ERROR = "There is a syntax error in your configuration file: %s" if PY3: @@ -302,11 +303,10 @@ class Config(object): # correct values of copyright year that are not coherent with # the SOURCE_DATE_EPOCH environment variable: if getenv('SOURCE_DATE_EPOCH') is not None: - for k in ['copyright','epub_copyright']: + for k in ('copyright','epub_copyright'): if k in config: - config[k] = re.sub('^((\d{4}-)?)(\d{4})(?=[ ,])', - '\g<1>%s' % format_date('%Y'), - config[k]) + config[k] = copyright_year_re.sub('\g<1>%s' % format_date('%Y'), + config[k]) def check_types(self, warn): # check all values for deviation from the default value's type, since diff --git a/tests/test_correct_year.py b/tests/test_correct_year.py index e09c3b630..7a156cbf2 100644 --- a/tests/test_correct_year.py +++ b/tests/test_correct_year.py @@ -14,34 +14,36 @@ from util import TestApp def test_correct_year(): - # save current value of SOURCE_DATE_EPOCH - sde = os.environ.pop('SOURCE_DATE_EPOCH',None) + try: + # save current value of SOURCE_DATE_EPOCH + sde = os.environ.pop('SOURCE_DATE_EPOCH',None) - # test with SOURCE_DATE_EPOCH unset: no modification - app = TestApp(buildername='html',testroot='correct-year') - app.builder.build_all() - content = (app.outdir / 'contents.html').text() - app.cleanup() - assert '2006-2009' in content + # test with SOURCE_DATE_EPOCH unset: no modification + app = TestApp(buildername='html',testroot='correct-year') + app.builder.build_all() + content = (app.outdir / 'contents.html').text() + app.cleanup() + assert '2006-2009' in content - # test with SOURCE_DATE_EPOCH set: copyright year should be - # updated - os.environ['SOURCE_DATE_EPOCH'] = "1293840000" - app = TestApp(buildername='html',testroot='correct-year') - app.builder.build_all() - content = (app.outdir / 'contents.html').text() - app.cleanup() - assert '2006-2011' in content + # test with SOURCE_DATE_EPOCH set: copyright year should be + # updated + os.environ['SOURCE_DATE_EPOCH'] = "1293840000" + app = TestApp(buildername='html',testroot='correct-year') + app.builder.build_all() + content = (app.outdir / 'contents.html').text() + app.cleanup() + assert '2006-2011' in content - os.environ['SOURCE_DATE_EPOCH'] = "1293839999" - app = TestApp(buildername='html',testroot='correct-year') - app.builder.build_all() - content = (app.outdir / 'contents.html').text() - app.cleanup() - assert '2006-2010' in content + os.environ['SOURCE_DATE_EPOCH'] = "1293839999" + app = TestApp(buildername='html',testroot='correct-year') + app.builder.build_all() + content = (app.outdir / 'contents.html').text() + app.cleanup() + assert '2006-2010' in content - # Restores SOURCE_DATE_EPOCH - if sde == None: - os.environ.pop('SOURCE_DATE_EPOCH',None) - else: - os.environ['SOURCE_DATE_EPOCH'] = sde + finally: + # Restores SOURCE_DATE_EPOCH + if sde == None: + os.environ.pop('SOURCE_DATE_EPOCH',None) + else: + os.environ['SOURCE_DATE_EPOCH'] = sde