diff --git a/src/nbsphinx/__init__.py b/src/nbsphinx/__init__.py index 0000c536..3bd0ed18 100644 --- a/src/nbsphinx/__init__.py +++ b/src/nbsphinx/__init__.py @@ -1689,6 +1689,22 @@ def html_page_context(app, pagename, templatename, context, doctree): app.add_css_file('nbsphinx-gallery.css') +def backwards_compat_overwrite(copyfile=sphinx.util.copyfile): + """Return kwargs dictionary to pass to copyfile() for consistent behavior + + Before version 8 of Sphinx, the default behavior of the copyfile function + was to overwrite the file at the target path if it already existed. + Version 8 requires passing force=True. + + Ref: https://github.com/sphinx-doc/sphinx/pull/12647 + """ + from inspect import signature + if "force" in signature(copyfile).parameters: + return {"force": True} + else: + return {} + + def html_collect_pages(app): """This event handler is abused to copy local files around.""" files = set() @@ -1699,7 +1715,10 @@ def html_collect_pages(app): target = os.path.join(app.builder.outdir, file) sphinx.util.ensuredir(os.path.dirname(target)) try: - sphinx.util.copyfile(os.path.join(app.env.srcdir, file), target) + sphinx.util.copyfile( + os.path.join(app.env.srcdir, file), + target, + **backwards_compat_overwrite()) except OSError as err: logger.warning( 'Cannot copy local file %r: %s', file, err, @@ -1710,7 +1729,8 @@ def html_collect_pages(app): 'brown', len(notebooks)): sphinx.util.copyfile( os.path.join(app.env.nbsphinx_auxdir, notebook), - os.path.join(app.builder.outdir, notebook)) + os.path.join(app.builder.outdir, notebook), + **backwards_compat_overwrite()) return [] # No new HTML pages are created def env_updated(app, env):