Repository: matplotlib/matplotlib
File: lib/matplotlib/sphinxext/plot_directive.py (Python)

Code before:
    app.add_config_value('plot_formats', ['png', 'hires.png', 'pdf'], True)
    app.add_config_value('plot_basedir', None, True)
    app.add_config_value('plot_html_show_formats', True, True)
    app.add_config_value('plot_rcparams', {}, True)
    app.add_config_value('plot_apply_rcparams', False, True)
    app.add_config_value('plot_working_directory', None, True)
    app.add_config_value('plot_template', None, True)
    app.add_config_value('plot_srcset', [], True)
    app.connect('doctree-read', mark_plot_labels)
    app.add_css_file('plot_directive.css')
    app.connect('build-finished', _copy_css_file)
    metadata = {'parallel_read_safe': True, 'parallel_write_safe': True,
                'version': matplotlib.__version__}
    app.connect('builder-inited', init_filename_registry)
    app.add_env_collector(_FilenameCollector)
    return metadata


# -----------------------------------------------------------------------------
# Handle Duplicate Filenames
# -----------------------------------------------------------------------------

def init_filename_registry(app):
    env = app.builder.env
    if not hasattr(env, 'mpl_custom_base_names'):
        env.mpl_custom_base_names = defaultdict(set)


class _FilenameCollector(EnvironmentCollector):
    def process_doc(self, app, doctree):
        pass

    def clear_doc(self, app, env, docname):
        if docname in env.mpl_custom_base_names:
            del env.mpl_custom_base_names[docname]

    def merge_other(self, app, env, docnames, other):
        for docname in docnames:
            if docname in other.mpl_custom_base_names:
                if docname not in env.mpl_custom_base_names:
                    env.mpl_custom_base_names[docname] = set()
                env.mpl_custom_base_names[docname].update(
                    other.mpl_custom_base_names[docname])

# -----------------------------------------------------------------------------
# Doctest handling
# -----------------------------------------------------------------------------


def contains_doctest(text):
    try:

Review comment (suggestion):
The name is a bit generic. Maybe 
```suggestion
        env.mpl_plot_image_basenames = defaultdict(set)
```
Or similar. This naming is *library_directive_option*.

Code after:
    app.add_config_value('plot_html_show_source_link', True, True)
    app.add_config_value('plot_formats', ['png', 'hires.png', 'pdf'], True)
    app.add_config_value('plot_basedir', None, True)
    app.add_config_value('plot_html_show_formats', True, True)
    app.add_config_value('plot_rcparams', {}, True)
    app.add_config_value('plot_apply_rcparams', False, True)
    app.add_config_value('plot_working_directory', None, True)
    app.add_config_value('plot_template', None, True)
    app.add_config_value('plot_srcset', [], True)
    app.connect('doctree-read', mark_plot_labels)
    app.add_css_file('plot_directive.css')
    app.connect('build-finished', _copy_css_file)
    metadata = {'parallel_read_safe': True, 'parallel_write_safe': True,
                'version': matplotlib.__version__}
    app.connect('builder-inited', init_filename_registry)
    app.add_env_collector(_FilenameCollector)
    return metadata


# -----------------------------------------------------------------------------
# Handle Duplicate Filenames
# -----------------------------------------------------------------------------

def init_filename_registry(app):
    env = app.builder.env
    if not hasattr(env, 'mpl_plot_image_basenames'):
        env.mpl_plot_image_basenames = defaultdict(set)


class _FilenameCollector(EnvironmentCollector):
    def process_doc(self, app, doctree):
        pass

    def clear_doc(self, app, env, docname):
        if docname in env.mpl_plot_image_basenames:
            del env.mpl_plot_image_basenames[docname]

    def merge_other(self, app, env, docnames, other):
        for docname in other.mpl_plot_image_basenames:
            env.mpl_plot_image_basenames[docname].update(
                other.mpl_plot_image_basenames[docname])


# -----------------------------------------------------------------------------
# Doctest handling
# -----------------------------------------------------------------------------


def contains_doctest(text):
    try:
        # check if it's valid Python as-is