Skip to content

paths.set_temp_cache and paths.set_temp_config don't reset value if exception occurs in creation #9704

Closed

Description

@aarchiba

Description

The context managers paths.set_temp_cache and paths.set_temp_config are supposed to let users temporarily reset the locations of the astropy cache and config file. These are used in the test suite but are also user-visible. Unfortunately, as context managers, if an exception occurs in their __enter__ methods, their __exit__ methods are never called; these exit methods are what is supposed to reset the values to their original state, so if an exception is produced, say because something is badly wrong with the requested cache setup, the bogus cache setup is left behind.

This appears as bug #9634 and PR #9703 revealed and should fix the problem; there should be an example in the code for it.

Activity

  1. aarchiba commented on Nov 29, 2019

    @aarchiba
    ContributorAuthor
  2. aarchiba commented on Nov 29, 2019

    @aarchiba
    ContributorAuthor

    https://github.com/astropy/astropy/pull/9703/files#diff-6e8757768603143a75fb8363ec462764R77 is a better example/regression test:

    def test_set_temp_cache_resets_on_exception(tmpdir):
        """Test for regression of  bug #9704"""
        t = paths.get_cache_dir()
        a = tmpdir / 'a'
        with open(a, 'wt') as f:
            f.write("not a good cache\n")
        with pytest.raises(OSError):
            with paths.set_temp_cache(a):
                pass
        assert t == paths.get_cache_dir()
  3. aarchiba commented on Nov 30, 2019

    @aarchiba
    ContributorAuthor

    Closed by #9703

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions