# Buggy code
def __init__(self, filename_or_obj, mode="r", mmap=None, version=1):
    import scipy

    if mode != "r" and scipy.__version__ < "0.13":
        warnings.warn(
            "scipy %s detected; "
            "the minimal recommended version is 0.13. "
            "Older version of this library do not reliably "
            "read and write files." % scipy.__version__,
            ImportWarning,
        )

    import scipy.io

    # if filename is a NetCDF3 bytestring we store it in a StringIO
    if isinstance(filename_or_obj, basestring) and filename_or_obj.startswith("CDF"):
        # TODO: this check has the unfortunate side-effect that
        # paths to files cannot start with 'CDF'.
        filename_or_obj = BytesIO(filename_or_obj)
    self.ds = scipy.io.netcdf.netcdf_file(
        filename_or_obj, mode=mode, mmap=mmap, version=version
    )


# 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 __init__(self, filename_or_obj, mode="r", mmap=None, version=2):
    import scipy

    if mode != "r" and scipy.__version__ < "0.13":
        warnings.warn(
            "scipy %s detected; "
            "the minimal recommended version is 0.13. "
            "Older version of this library do not reliably "
            "read and write files." % scipy.__version__,
            ImportWarning,
        )

    import scipy.io

    # if filename is a NetCDF3 bytestring we store it in a StringIO
    if isinstance(filename_or_obj, basestring) and filename_or_obj.startswith("CDF"):
        # TODO: this check has the unfortunate side-effect that
        # paths to files cannot start with 'CDF'.
        filename_or_obj = BytesIO(filename_or_obj)
    self.ds = scipy.io.netcdf.netcdf_file(
        filename_or_obj, mode=mode, mmap=mmap, version=version
    )