# Buggy code
def open_dataset(
    filename_or_obj,
    group=None,
    decode_cf=True,
    mask_and_scale=True,
    decode_times=True,
    concat_characters=True,
    decode_coords=True,
):
    """Load and decode a dataset from a file or file-like object.

    Parameters
    ----------
    filename_or_obj : str or file
        Strings are interpreted as a path to a netCDF file or an OpenDAP URL
        and opened with python-netCDF4, unless the filename ends with .gz, in
        which case the file is gunzipped and opened with scipy.io.netcdf (only
        netCDF3 supported). File-like objects are opened with scipy.io.netcdf
        (only netCDF3 supported).
    group : str, optional
        Path to the netCDF4 group in the given file to open (only works for
        netCDF4 files).
    decode_cf : bool, optional
        Whether to decode these variables, assuming they were saved according
        to CF conventions.
    mask_and_scale : bool, optional
        If True, replace array values equal to `_FillValue` with NA and scale
        values according to the formula `original_values * scale_factor +
        add_offset`, where `_FillValue`, `scale_factor` and `add_offset` are
        taken from variable attributes (if they exist).
    decode_times : bool, optional
        If True, decode times encoded in the standard NetCDF datetime format
        into datetime objects. Otherwise, leave them encoded as numbers.
    concat_characters : bool, optional
        If True, concatenate along the last dimension of character arrays to
        form string arrays. Dimensions will only be concatenated over (and
        removed) if they have no corresponding variable and if they are only
        used as the last dimension of character arrays.
    decode_coords : bool, optional
        If True, decode the 'coordinates' attribute to identify coordinates in
        the resulting dataset.

    Returns
    -------
    dataset : Dataset
        The newly created dataset.
    """
    if isinstance(filename_or_obj, basestring):
        if filename_or_obj.endswith(".gz"):
            # if the string ends with .gz, then gunzip and open as netcdf file
            if sys.version_info[:2] < (2, 7):
                raise ValueError("reading a gzipped netCDF not supported on Python 2.6")
            try:
                store = backends.ScipyDataStore(gzip.open(filename_or_obj))
            except TypeError as e:
                # TODO: gzipped loading only works with NetCDF3 files.
                if "is not a valid NetCDF 3 file" in e.message:
                    raise ValueError(
                        "gzipped file loading only supports NetCDF 3 files."
                    )
                else:
                    raise
        else:
            try:
                store = backends.NetCDF4DataStore(filename_or_obj, group=group)
            except ImportError:
                store = backends.ScipyDataStore(filename_or_obj)
    else:
        # assume filename_or_obj is a file-like object
        store = backends.ScipyDataStore(filename_or_obj)

    if decode_cf:
        return conventions.decode_cf(
            store,
            mask_and_scale=mask_and_scale,
            decode_times=decode_times,
            concat_characters=concat_characters,
            decode_coords=decode_coords,
        )
    else:
        return Dataset.load_store(store)


# Traceback (WindowsError)
$ nosetests xray

SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS..C:\Python27\Anaconda\lib\site-packages\scipy\io\netcdf.py:287: RuntimeWarning: Cannot close a netcdf_file opened with mmap=True, when netcdf_variables or arrays referring to its data still exist. All data arrays obtained from such files refer directly to data on disk, and must be copied before the file can be cleanly closed. (See netcdf_file docstring for more information on mmap.)
), category=RuntimeWarning)
.........C:\Python27\Anaconda\lib\site-packages\xray\core\utils.py:53: FutureWarning: numpy equal will not check object identity in the future. The comparison did not return the same result as suggested by the identity (`is`)) and will change.
return ((arr1 == arr2) | (pd.isnull(arr1) &amp; pd.isnull(arr2))).all()
.....EE.EE.E.EEE.E.EE.SSSSSSSSSSSSSSSS...S.SS.SSSSS.....SSS.............F...................S...................F....S......................F........................................S.............F............................F....................................................F.................................
======================================================================
ERROR: test_load_data (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 92, in test_load_data
pass
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 87, in assert_loads
self.assertDatasetAllClose(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 35, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpnniync.nc'

======================================================================
ERROR: test_orthogonal_indexing (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 211, in test_orthogonal_indexing
self.assertDatasetAllClose(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpanlrts.nc'

======================================================================
ERROR: test_roundtrip_None_variable (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 108, in test_roundtrip_None_variable
self.assertDatasetAllClose(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpinhb9v.nc'

======================================================================
ERROR: test_roundtrip_coordinates (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 182, in test_roundtrip_coordinates
self.assertDatasetIdentical(original, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpezxsf_.nc'

======================================================================
ERROR: test_roundtrip_example_1_netcdf (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 165, in test_roundtrip_example_1_netcdf
self.assertDatasetEqual(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpo5zwxy.nc'

======================================================================
ERROR: test_roundtrip_mask_and_scale (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 253, in test_roundtrip_mask_and_scale
self.assertDatasetAllClose(encoded, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmp5jczpa.nc'

======================================================================
ERROR: test_roundtrip_object_dtype (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 137, in test_roundtrip_object_dtype
self.assertDatasetIdentical(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpji2z1n.nc'

======================================================================
ERROR: test_roundtrip_string_data (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 144, in test_roundtrip_string_data
self.assertDatasetIdentical(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmp7sj2iu.nc'

======================================================================
ERROR: test_roundtrip_test_data (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 73, in test_roundtrip_test_data
self.assertDatasetAllClose(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpibvyaw.nc'

======================================================================
ERROR: test_write_store (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 68, in test_write_store
self.assertDatasetAllClose(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 588, in create_store
yield backends.ScipyDataStore(tmp_file, mode='w')
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpyy61n0.nc'

======================================================================
ERROR: test_zero_dimensional_variable (xray.test.test_backends.ScipyOnDiskDataTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 59, in test_zero_dimensional_variable
self.assertDatasetAllClose(expected, actual)
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 600, in roundtrip
yield ds
File "C:\Python27\Anaconda\lib\contextlib.py", line 24, in __exit__
self.gen.next()
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_backends.py", line 273, in create_tmp_file
os.remove(path)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\breisfel\\appdata\\local\\temp\\tmpfqemcm.nc'

======================================================================
FAIL: test_coords (xray.test.test_dataarray.TestDataArray)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_dataarray.py", line 408, in test_coords
self.assertEquals(expected, actual)
AssertionError: 'Coordinates:\n  * x        (x) int64 -1 -2\n  * y        (y) int64 0 1 2' != 'Coordinates:\n  * x        (x) int32 -1 -2\n  * y        (y) int32 0 1 2'

======================================================================
FAIL: test_repr (xray.test.test_dataarray.TestDataArray)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_dataarray.py", line 34, in test_repr
self.assertEqual(expected, repr(data_array))
AssertionError: "<xray.DataArray 'my_variable' (time: 2, x: 3)>\narray([[1, 2, 3],\n       [4, 5, 6]])\nCoordinates:\n    other    int64 0\n  * time     (time) int64 0 1\n  * x        (x) int64 0 1 2\nAttributes:\n    foo: bar" != "<xray.DataArray 'my_variable' (time: 2, x: 3)>\narray([[1, 2, 3],\n       [4, 5, 6]])\nCoordinates:\n    other    int32 0\n  * time     (time) int64 0 1\n  * x        (x) int64 0 1 2\nAttributes:\n    foo: bar"

======================================================================
FAIL: test_coords_properties (xray.test.test_dataset.TestDataset)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_dataset.py", line 335, in test_coords_properties
self.assertEqual(expected, actual)
AssertionError: 'Coordinates:\n  * x        (x) int64 -1 -2\n  * y        (y) int64 0 1 2\n    a        (x) int64 4 5\n    b        int64 -10' != 'Coordinates:\n  * x        (x) int32 -1 -2\n  * y        (y) int32 0 1 2\n    a        (x) int32 4 5\n    b        int32 -10'

======================================================================
FAIL: test_to_and_from_dataframe (xray.test.test_dataset.TestDataset)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_dataset.py", line 1369, in test_to_and_from_dataframe
assert expected.equals(actual), (expected, actual)
AssertionError: (   a    x
y
0  1  bar,    a    x
y
0  1  bar)

======================================================================
FAIL: test_converted_types (xray.test.test_variable.TestAsCompatibleData)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_variable.py", line 813, in test_converted_types
self.assertEqual(np.dtype(int), actual.dtype)
AssertionError: dtype('int32') != dtype('int64')

======================================================================
FAIL: test_count (xray.test.test_variable.TestVariable)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python27\Anaconda\lib\site-packages\xray\test\test_variable.py", line 758, in test_count
self.assertEqual(actual.dtype, 'int64')
AssertionError: dtype('int32') != 'int64'

----------------------------------------------------------------------
Ran 368 tests in 5.705s

FAILED (SKIP=76, errors=11, failures=6)

# Fixed code
def open_dataset(
    filename_or_obj,
    group=None,
    decode_cf=True,
    mask_and_scale=True,
    decode_times=True,
    concat_characters=True,
    decode_coords=True,
):
    """Load and decode a dataset from a file or file-like object.

    Parameters
    ----------
    filename_or_obj : str or file
        Strings are interpreted as a path to a netCDF file or an OpenDAP URL
        and opened with python-netCDF4, unless the filename ends with .gz, in
        which case the file is gunzipped and opened with scipy.io.netcdf (only
        netCDF3 supported). File-like objects are opened with scipy.io.netcdf
        (only netCDF3 supported).
    group : str, optional
        Path to the netCDF4 group in the given file to open (only works for
        netCDF4 files).
    decode_cf : bool, optional
        Whether to decode these variables, assuming they were saved according
        to CF conventions.
    mask_and_scale : bool, optional
        If True, replace array values equal to `_FillValue` with NA and scale
        values according to the formula `original_values * scale_factor +
        add_offset`, where `_FillValue`, `scale_factor` and `add_offset` are
        taken from variable attributes (if they exist).
    decode_times : bool, optional
        If True, decode times encoded in the standard NetCDF datetime format
        into datetime objects. Otherwise, leave them encoded as numbers.
    concat_characters : bool, optional
        If True, concatenate along the last dimension of character arrays to
        form string arrays. Dimensions will only be concatenated over (and
        removed) if they have no corresponding variable and if they are only
        used as the last dimension of character arrays.
    decode_coords : bool, optional
        If True, decode the 'coordinates' attribute to identify coordinates in
        the resulting dataset.

    Returns
    -------
    dataset : Dataset
        The newly created dataset.
    """

    def _decode_store(store):
        if decode_cf:
            return conventions.decode_cf(
                store,
                mask_and_scale=mask_and_scale,
                decode_times=decode_times,
                concat_characters=concat_characters,
                decode_coords=decode_coords,
            )
        else:
            return Dataset.load_store(store)

    if isinstance(filename_or_obj, basestring):
        if filename_or_obj.endswith(".gz"):
            # if the string ends with .gz, then gunzip and open as netcdf file
            if sys.version_info[:2] < (2, 7):
                raise ValueError("reading a gzipped netCDF not supported on Python 2.6")
            try:
                store = backends.ScipyDataStore(gzip.open(filename_or_obj))
            except TypeError as e:
                # TODO: gzipped loading only works with NetCDF3 files.
                if "is not a valid NetCDF 3 file" in e.message:
                    raise ValueError(
                        "gzipped file loading only supports NetCDF 3 files."
                    )
                else:
                    raise
        else:
            try:
                store = backends.NetCDF4DataStore(filename_or_obj, group=group)
            except ImportError:
                store = backends.ScipyDataStore(filename_or_obj)

        with close_on_error(store):
            return _decode_store(store)
    else:
        # assume filename_or_obj is a file-like object
        store = backends.ScipyDataStore(filename_or_obj)
        return _decode_store(store)