We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Segfault when closing WarpedVRT
## Expected behavior and actual behavior.

I use contextily which itself call rasterio. Everything works fine with version 1.1.5, but I have a segfault with version 1.1.6 : 
```
Fatal Python error: Segmentation fault
Current thread 0x00007fe33542d740 (most recent call first):
  File "/home/user/.pyenv/versions/project/lib/python3.8/site-packages/rasterio/vrt.py", line 126 in close
  File "/home/user/.pyenv/versions/project/lib/python3.8/site-packages/rasterio/vrt.py", line 123 in __del__
  File "/home/user/.pyenv/versions/project/lib/python3.8/site-packages/contextily/plotting.py", line 148 in add_basemap
```

## Steps to reproduce the problem.

Run the following program : 
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import contextily as ctx
import matplotlib.pyplot as plt

if __name__ == '__main__':
    fig, ax = plt.subplots()
    ctx.add_basemap(ax=ax, crs="EPSG:3857")
```

## Operating system

Debian 10

## Rasterio version and provenance
1.1.6 installed from PyPI using poetry.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `rasterio/io.py`
Content:
```
1 """Classes capable of reading and writing datasets
2 
3 Instances of these classes are called dataset objects.
4 """
5 
6 import logging
7 
8 from rasterio._base import (
9     get_dataset_driver, driver_can_create, driver_can_create_copy)
10 from rasterio._io import (
11     DatasetReaderBase, DatasetWriterBase, BufferedDatasetWriterBase,
12     MemoryFileBase)
13 from rasterio.windows import WindowMethodsMixin
14 from rasterio.env import ensure_env, env_ctx_if_needed
15 from rasterio.transform import TransformMethodsMixin
16 from rasterio.path import UnparsedPath
17 
18 
19 log = logging.getLogger(__name__)
20 
21 
22 class DatasetReader(DatasetReaderBase, WindowMethodsMixin,
23                     TransformMethodsMixin):
24     """An unbuffered data and metadata reader"""
25 
26     def __repr__(self):
27         return "<{} DatasetReader name='{}' mode='{}'>".format(
28             self.closed and 'closed' or 'open', self.name, self.mode)
29 
30 
31 class DatasetWriter(DatasetWriterBase, WindowMethodsMixin,
32                     TransformMethodsMixin):
33     """An unbuffered data and metadata writer. Its methods write data
34     directly to disk.
35     """
36 
37     def __repr__(self):
38         return "<{} DatasetWriter name='{}' mode='{}'>".format(
39             self.closed and 'closed' or 'open', self.name, self.mode)
40 
41 
42 class BufferedDatasetWriter(BufferedDatasetWriterBase, WindowMethodsMixin,
43                             TransformMethodsMixin):
44     """Maintains data and metadata in a buffer, writing to disk or
45     network only when `close()` is called.
46 
47     This allows incremental updates to datasets using formats that don't
48     otherwise support updates, such as JPEG.
49     """
50 
51     def __repr__(self):
52         return "<{} BufferedDatasetWriter name='{}' mode='{}'>".format(
53             self.closed and 'closed' or 'open', self.name, self.mode)
54 
55 
56 class MemoryFile(MemoryFileBase):
57     """A BytesIO-like object, backed by an in-memory file.
58 
59     This allows formatted files to be read and written without I/O.
60 
61     A MemoryFile created with initial bytes becomes immutable. A
62     MemoryFile created without initial bytes may be written to using
63     either file-like or dataset interfaces.
64 
65     Examples
66     --------
67 
68     A GeoTIFF can be loaded in memory and accessed using the GeoTIFF
69     format driver
70 
71     >>> with open('tests/data/RGB.byte.tif', 'rb') as f, MemoryFile(f) as memfile:
72     ...     with memfile.open() as src:
73     ...         pprint.pprint(src.profile)
74     ...
75     {'count': 3,
76      'crs': CRS({'init': 'epsg:32618'}),
77      'driver': 'GTiff',
78      'dtype': 'uint8',
79      'height': 718,
80      'interleave': 'pixel',
81      'nodata': 0.0,
82      'tiled': False,
83      'transform': Affine(300.0379266750948, 0.0, 101985.0,
84            0.0, -300.041782729805, 2826915.0),
85      'width': 791}
86 
87     """
88     def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext=''):
89         """Create a new file in memory
90 
91         Parameters
92         ----------
93         file_or_bytes : file-like object or bytes, optional
94             File or bytes holding initial data.
95         filename : str, optional
96             An optional filename. A unique one will otherwise be generated.
97         ext : str, optional
98             An optional extension.
99 
100         Returns
101         -------
102         MemoryFile
103         """
104         super(MemoryFile, self).__init__(
105             file_or_bytes=file_or_bytes, dirname=dirname, filename=filename, ext=ext)
106 
107     @ensure_env
108     def open(self, driver=None, width=None, height=None, count=None, crs=None,
109              transform=None, dtype=None, nodata=None, sharing=False, **kwargs):
110         """Open the file and return a Rasterio dataset object.
111 
112         If data has already been written, the file is opened in 'r'
113         mode. Otherwise, the file is opened in 'w' mode.
114 
115         Parameters
116         ----------
117         Note well that there is no `path` parameter: a `MemoryFile`
118         contains a single dataset and there is no need to specify a
119         path.
120 
121         Other parameters are optional and have the same semantics as the
122         parameters of `rasterio.open()`.
123         """
124         mempath = UnparsedPath(self.name)
125 
126         if self.closed:
127             raise IOError("I/O operation on closed file.")
128         if len(self) > 0:
129             log.debug("VSI path: {}".format(mempath.path))
130             return DatasetReader(mempath, driver=driver, sharing=sharing, **kwargs)
131         else:
132             writer = get_writer_for_driver(driver)
133             return writer(mempath, 'w+', driver=driver, width=width,
134                           height=height, count=count, crs=crs,
135                           transform=transform, dtype=dtype,
136                           nodata=nodata, sharing=sharing, **kwargs)
137 
138     def __enter__(self):
139         self._env = env_ctx_if_needed()
140         self._env.__enter__()
141         return self
142 
143     def __exit__(self, *args, **kwargs):
144         self._env.__exit__()
145         self.close()
146 
147 
148 class ZipMemoryFile(MemoryFile):
149     """A read-only BytesIO-like object backed by an in-memory zip file.
150 
151     This allows a zip file containing formatted files to be read
152     without I/O.
153     """
154 
155     def __init__(self, file_or_bytes=None):
156         super(ZipMemoryFile, self).__init__(file_or_bytes, ext='zip')
157 
158     @ensure_env
159     def open(self, path, driver=None, sharing=False, **kwargs):
160         """Open a dataset within the zipped stream.
161 
162         Parameters
163         ----------
164         path : str
165             Path to a dataset in the zip file, relative to the root of the
166             archive.
167 
168         Other parameters are optional and have the same semantics as the
169         parameters of `rasterio.open()`.
170 
171         Returns
172         -------
173         A Rasterio dataset object
174         """
175         zippath = UnparsedPath('/vsizip{0}/{1}'.format(self.name, path.lstrip('/')))
176 
177         if self.closed:
178             raise IOError("I/O operation on closed file.")
179         return DatasetReader(zippath, driver=driver, sharing=sharing, **kwargs)
180 
181 
182 def get_writer_for_driver(driver):
183     """Return the writer class appropriate for the specified driver."""
184     if not driver:
185         raise ValueError("'driver' is required to write dataset.")
186     cls = None
187     if driver_can_create(driver):
188         cls = DatasetWriter
189     elif driver_can_create_copy(driver):  # pragma: no branch
190         cls = BufferedDatasetWriter
191     return cls
192 
193 
194 def get_writer_for_path(path, driver=None):
195     """Return the writer class appropriate for the existing dataset."""
196     if not driver:
197         driver = get_dataset_driver(path)
198     return get_writer_for_driver(driver)
199
```

Path: `rasterio/errors.py`
Content:
```
1 """Errors and Warnings."""
2 
3 from click import FileError
4 
5 
6 class RasterioError(Exception):
7     """Root exception class"""
8 
9 
10 class WindowError(RasterioError):
11     """Raised when errors occur during window operations"""
12 
13 
14 class CRSError(ValueError):
15     """Raised when a CRS string or mapping is invalid or cannot serve
16     to define a coordinate transformation."""
17 
18 
19 class EnvError(RasterioError):
20     """Raised when the state of GDAL/AWS environment cannot be created
21     or modified."""
22 
23 
24 class DriverCapabilityError(RasterioError, ValueError):
25     """Raised when a format driver can't a feature such as writing."""
26 
27 
28 class DriverRegistrationError(ValueError):
29     """Raised when a format driver is requested but is not registered."""
30 
31 
32 class FileOverwriteError(FileError):
33     """Raised when Rasterio's CLI refuses to clobber output files."""
34 
35     def __init__(self, message):
36         """Raise FileOverwriteError with message as hint."""
37         super(FileOverwriteError, self).__init__('', hint=message)
38 
39 
40 class RasterioIOError(IOError):
41     """Raised when a dataset cannot be opened using one of the
42     registered format drivers."""
43 
44 
45 class NodataShadowWarning(UserWarning):
46     """Warn that a dataset's nodata attribute is shadowing its alpha band."""
47 
48     def __str__(self):
49         return ("The dataset's nodata attribute is shadowing "
50                 "the alpha band. All masks will be determined "
51                 "by the nodata attribute")
52 
53 
54 class NotGeoreferencedWarning(UserWarning):
55     """Warn that a dataset isn't georeferenced."""
56 
57 
58 class ShapeSkipWarning(UserWarning):
59     """Warn that an invalid or empty shape in a collection has been skipped"""
60 
61 
62 class GDALBehaviorChangeException(RuntimeError):
63     """Raised when GDAL's behavior differs from the given arguments.  For
64     example, antimeridian cutting is always on as of GDAL 2.2.0.  Users
65     expecting it to be off will be presented with a MultiPolygon when the
66     rest of their code expects a Polygon.
67 
68         # Raises an exception on GDAL >= 2.2.0
69         rasterio.warp.transform_geometry(
70             src_crs, dst_crs, antimeridian_cutting=False)
71     """
72 
73 
74 class GDALOptionNotImplementedError(RasterioError):
75     """A dataset opening or dataset creation option can't be supported
76 
77     This will be raised from Rasterio's shim modules. For example, when
78     a user passes arguments to open_dataset() that can't be evaluated
79     by GDAL 1.x.
80     """
81 
82 class GDALVersionError(RasterioError):
83     """Raised if the runtime version of GDAL does not meet the required
84     version of GDAL."""
85 
86 
87 class WindowEvaluationError(ValueError):
88     """Raised when window evaluation fails"""
89 
90 
91 class RasterioDeprecationWarning(UserWarning):
92     """Rasterio module deprecations"""
93 
94 
95 class RasterBlockError(RasterioError):
96     """Raised when raster block access fails"""
97 
98 
99 class BandOverviewError(UserWarning):
100     """Raised when a band overview access fails."""
101 
102 
103 class WarpOptionsError(RasterioError):
104     """Raised when options for a warp operation are invalid"""
105 
106 
107 class UnsupportedOperation(RasterioError):
108     """Raised when reading from a file opened in 'w' mode"""
109 
110 
111 class OverviewCreationError(RasterioError):
112     """Raised when creation of an overview fails"""
113 
114 
115 class DatasetAttributeError(RasterioError, NotImplementedError):
116     """Raised when dataset attributes are misused"""
117 
118 
119 class PathError(RasterioError):
120     """Raised when a dataset path is malformed or invalid"""
121 
122 
123 class ResamplingAlgorithmError(RasterioError):
124     """Raised when a resampling algorithm is invalid or inapplicable"""
125 
126 
127 class TransformError(RasterioError):
128     """Raised when transform arguments are invalid"""
129
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/rasterio/errors.py b/rasterio/errors.py
--- a/rasterio/errors.py
+++ b/rasterio/errors.py
@@ -126,3 +126,7 @@
 
 class TransformError(RasterioError):
     """Raised when transform arguments are invalid"""
+
+
+class WarpedVRTError(RasterioError):
+    """Raised when WarpedVRT can't be initialized"""
diff --git a/rasterio/io.py b/rasterio/io.py
--- a/rasterio/io.py
+++ b/rasterio/io.py
@@ -85,7 +85,7 @@
      'width': 791}
 
     """
-    def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext=''):
+    def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext='.tif'):
         """Create a new file in memory
 
         Parameters
@@ -141,8 +141,8 @@
         return self
 
     def __exit__(self, *args, **kwargs):
-        self._env.__exit__()
         self.close()
+        self._env.__exit__()
 
 
 class ZipMemoryFile(MemoryFile):

{"golden_diff": "diff --git a/rasterio/errors.py b/rasterio/errors.py\n--- a/rasterio/errors.py\n+++ b/rasterio/errors.py\n@@ -126,3 +126,7 @@\n \n class TransformError(RasterioError):\n     \"\"\"Raised when transform arguments are invalid\"\"\"\n+\n+\n+class WarpedVRTError(RasterioError):\n+    \"\"\"Raised when WarpedVRT can't be initialized\"\"\"\ndiff --git a/rasterio/io.py b/rasterio/io.py\n--- a/rasterio/io.py\n+++ b/rasterio/io.py\n@@ -85,7 +85,7 @@\n      'width': 791}\n \n     \"\"\"\n-    def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext=''):\n+    def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext='.tif'):\n         \"\"\"Create a new file in memory\n \n         Parameters\n@@ -141,8 +141,8 @@\n         return self\n \n     def __exit__(self, *args, **kwargs):\n-        self._env.__exit__()\n         self.close()\n+        self._env.__exit__()\n \n \n class ZipMemoryFile(MemoryFile):\n", "issue": "Segfault when closing WarpedVRT\n## Expected behavior and actual behavior.\r\n\r\nI use contextily which itself call rasterio. Everything works fine with version 1.1.5, but I have a segfault with version 1.1.6 : \r\n```\r\nFatal Python error: Segmentation fault\r\nCurrent thread 0x00007fe33542d740 (most recent call first):\r\n  File \"/home/user/.pyenv/versions/project/lib/python3.8/site-packages/rasterio/vrt.py\", line 126 in close\r\n  File \"/home/user/.pyenv/versions/project/lib/python3.8/site-packages/rasterio/vrt.py\", line 123 in __del__\r\n  File \"/home/user/.pyenv/versions/project/lib/python3.8/site-packages/contextily/plotting.py\", line 148 in add_basemap\r\n```\r\n\r\n## Steps to reproduce the problem.\r\n\r\nRun the following program : \r\n```python\r\n#!/usr/bin/env python\r\n# -*- coding: utf-8 -*-\r\n\r\nimport contextily as ctx\r\nimport matplotlib.pyplot as plt\r\n\r\nif __name__ == '__main__':\r\n    fig, ax = plt.subplots()\r\n    ctx.add_basemap(ax=ax, crs=\"EPSG:3857\")\r\n```\r\n\r\n## Operating system\r\n\r\nDebian 10\r\n\r\n## Rasterio version and provenance\r\n1.1.6 installed from PyPI using poetry.\r\n\n", "before_files": [{"content": "\"\"\"Classes capable of reading and writing datasets\n\nInstances of these classes are called dataset objects.\n\"\"\"\n\nimport logging\n\nfrom rasterio._base import (\n    get_dataset_driver, driver_can_create, driver_can_create_copy)\nfrom rasterio._io import (\n    DatasetReaderBase, DatasetWriterBase, BufferedDatasetWriterBase,\n    MemoryFileBase)\nfrom rasterio.windows import WindowMethodsMixin\nfrom rasterio.env import ensure_env, env_ctx_if_needed\nfrom rasterio.transform import TransformMethodsMixin\nfrom rasterio.path import UnparsedPath\n\n\nlog = logging.getLogger(__name__)\n\n\nclass DatasetReader(DatasetReaderBase, WindowMethodsMixin,\n                    TransformMethodsMixin):\n    \"\"\"An unbuffered data and metadata reader\"\"\"\n\n    def __repr__(self):\n        return \"<{} DatasetReader name='{}' mode='{}'>\".format(\n            self.closed and 'closed' or 'open', self.name, self.mode)\n\n\nclass DatasetWriter(DatasetWriterBase, WindowMethodsMixin,\n                    TransformMethodsMixin):\n    \"\"\"An unbuffered data and metadata writer. Its methods write data\n    directly to disk.\n    \"\"\"\n\n    def __repr__(self):\n        return \"<{} DatasetWriter name='{}' mode='{}'>\".format(\n            self.closed and 'closed' or 'open', self.name, self.mode)\n\n\nclass BufferedDatasetWriter(BufferedDatasetWriterBase, WindowMethodsMixin,\n                            TransformMethodsMixin):\n    \"\"\"Maintains data and metadata in a buffer, writing to disk or\n    network only when `close()` is called.\n\n    This allows incremental updates to datasets using formats that don't\n    otherwise support updates, such as JPEG.\n    \"\"\"\n\n    def __repr__(self):\n        return \"<{} BufferedDatasetWriter name='{}' mode='{}'>\".format(\n            self.closed and 'closed' or 'open', self.name, self.mode)\n\n\nclass MemoryFile(MemoryFileBase):\n    \"\"\"A BytesIO-like object, backed by an in-memory file.\n\n    This allows formatted files to be read and written without I/O.\n\n    A MemoryFile created with initial bytes becomes immutable. A\n    MemoryFile created without initial bytes may be written to using\n    either file-like or dataset interfaces.\n\n    Examples\n    --------\n\n    A GeoTIFF can be loaded in memory and accessed using the GeoTIFF\n    format driver\n\n    >>> with open('tests/data/RGB.byte.tif', 'rb') as f, MemoryFile(f) as memfile:\n    ...     with memfile.open() as src:\n    ...         pprint.pprint(src.profile)\n    ...\n    {'count': 3,\n     'crs': CRS({'init': 'epsg:32618'}),\n     'driver': 'GTiff',\n     'dtype': 'uint8',\n     'height': 718,\n     'interleave': 'pixel',\n     'nodata': 0.0,\n     'tiled': False,\n     'transform': Affine(300.0379266750948, 0.0, 101985.0,\n           0.0, -300.041782729805, 2826915.0),\n     'width': 791}\n\n    \"\"\"\n    def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext=''):\n        \"\"\"Create a new file in memory\n\n        Parameters\n        ----------\n        file_or_bytes : file-like object or bytes, optional\n            File or bytes holding initial data.\n        filename : str, optional\n            An optional filename. A unique one will otherwise be generated.\n        ext : str, optional\n            An optional extension.\n\n        Returns\n        -------\n        MemoryFile\n        \"\"\"\n        super(MemoryFile, self).__init__(\n            file_or_bytes=file_or_bytes, dirname=dirname, filename=filename, ext=ext)\n\n    @ensure_env\n    def open(self, driver=None, width=None, height=None, count=None, crs=None,\n             transform=None, dtype=None, nodata=None, sharing=False, **kwargs):\n        \"\"\"Open the file and return a Rasterio dataset object.\n\n        If data has already been written, the file is opened in 'r'\n        mode. Otherwise, the file is opened in 'w' mode.\n\n        Parameters\n        ----------\n        Note well that there is no `path` parameter: a `MemoryFile`\n        contains a single dataset and there is no need to specify a\n        path.\n\n        Other parameters are optional and have the same semantics as the\n        parameters of `rasterio.open()`.\n        \"\"\"\n        mempath = UnparsedPath(self.name)\n\n        if self.closed:\n            raise IOError(\"I/O operation on closed file.\")\n        if len(self) > 0:\n            log.debug(\"VSI path: {}\".format(mempath.path))\n            return DatasetReader(mempath, driver=driver, sharing=sharing, **kwargs)\n        else:\n            writer = get_writer_for_driver(driver)\n            return writer(mempath, 'w+', driver=driver, width=width,\n                          height=height, count=count, crs=crs,\n                          transform=transform, dtype=dtype,\n                          nodata=nodata, sharing=sharing, **kwargs)\n\n    def __enter__(self):\n        self._env = env_ctx_if_needed()\n        self._env.__enter__()\n        return self\n\n    def __exit__(self, *args, **kwargs):\n        self._env.__exit__()\n        self.close()\n\n\nclass ZipMemoryFile(MemoryFile):\n    \"\"\"A read-only BytesIO-like object backed by an in-memory zip file.\n\n    This allows a zip file containing formatted files to be read\n    without I/O.\n    \"\"\"\n\n    def __init__(self, file_or_bytes=None):\n        super(ZipMemoryFile, self).__init__(file_or_bytes, ext='zip')\n\n    @ensure_env\n    def open(self, path, driver=None, sharing=False, **kwargs):\n        \"\"\"Open a dataset within the zipped stream.\n\n        Parameters\n        ----------\n        path : str\n            Path to a dataset in the zip file, relative to the root of the\n            archive.\n\n        Other parameters are optional and have the same semantics as the\n        parameters of `rasterio.open()`.\n\n        Returns\n        -------\n        A Rasterio dataset object\n        \"\"\"\n        zippath = UnparsedPath('/vsizip{0}/{1}'.format(self.name, path.lstrip('/')))\n\n        if self.closed:\n            raise IOError(\"I/O operation on closed file.\")\n        return DatasetReader(zippath, driver=driver, sharing=sharing, **kwargs)\n\n\ndef get_writer_for_driver(driver):\n    \"\"\"Return the writer class appropriate for the specified driver.\"\"\"\n    if not driver:\n        raise ValueError(\"'driver' is required to write dataset.\")\n    cls = None\n    if driver_can_create(driver):\n        cls = DatasetWriter\n    elif driver_can_create_copy(driver):  # pragma: no branch\n        cls = BufferedDatasetWriter\n    return cls\n\n\ndef get_writer_for_path(path, driver=None):\n    \"\"\"Return the writer class appropriate for the existing dataset.\"\"\"\n    if not driver:\n        driver = get_dataset_driver(path)\n    return get_writer_for_driver(driver)\n", "path": "rasterio/io.py"}, {"content": "\"\"\"Errors and Warnings.\"\"\"\n\nfrom click import FileError\n\n\nclass RasterioError(Exception):\n    \"\"\"Root exception class\"\"\"\n\n\nclass WindowError(RasterioError):\n    \"\"\"Raised when errors occur during window operations\"\"\"\n\n\nclass CRSError(ValueError):\n    \"\"\"Raised when a CRS string or mapping is invalid or cannot serve\n    to define a coordinate transformation.\"\"\"\n\n\nclass EnvError(RasterioError):\n    \"\"\"Raised when the state of GDAL/AWS environment cannot be created\n    or modified.\"\"\"\n\n\nclass DriverCapabilityError(RasterioError, ValueError):\n    \"\"\"Raised when a format driver can't a feature such as writing.\"\"\"\n\n\nclass DriverRegistrationError(ValueError):\n    \"\"\"Raised when a format driver is requested but is not registered.\"\"\"\n\n\nclass FileOverwriteError(FileError):\n    \"\"\"Raised when Rasterio's CLI refuses to clobber output files.\"\"\"\n\n    def __init__(self, message):\n        \"\"\"Raise FileOverwriteError with message as hint.\"\"\"\n        super(FileOverwriteError, self).__init__('', hint=message)\n\n\nclass RasterioIOError(IOError):\n    \"\"\"Raised when a dataset cannot be opened using one of the\n    registered format drivers.\"\"\"\n\n\nclass NodataShadowWarning(UserWarning):\n    \"\"\"Warn that a dataset's nodata attribute is shadowing its alpha band.\"\"\"\n\n    def __str__(self):\n        return (\"The dataset's nodata attribute is shadowing \"\n                \"the alpha band. All masks will be determined \"\n                \"by the nodata attribute\")\n\n\nclass NotGeoreferencedWarning(UserWarning):\n    \"\"\"Warn that a dataset isn't georeferenced.\"\"\"\n\n\nclass ShapeSkipWarning(UserWarning):\n    \"\"\"Warn that an invalid or empty shape in a collection has been skipped\"\"\"\n\n\nclass GDALBehaviorChangeException(RuntimeError):\n    \"\"\"Raised when GDAL's behavior differs from the given arguments.  For\n    example, antimeridian cutting is always on as of GDAL 2.2.0.  Users\n    expecting it to be off will be presented with a MultiPolygon when the\n    rest of their code expects a Polygon.\n\n        # Raises an exception on GDAL >= 2.2.0\n        rasterio.warp.transform_geometry(\n            src_crs, dst_crs, antimeridian_cutting=False)\n    \"\"\"\n\n\nclass GDALOptionNotImplementedError(RasterioError):\n    \"\"\"A dataset opening or dataset creation option can't be supported\n\n    This will be raised from Rasterio's shim modules. For example, when\n    a user passes arguments to open_dataset() that can't be evaluated\n    by GDAL 1.x.\n    \"\"\"\n\nclass GDALVersionError(RasterioError):\n    \"\"\"Raised if the runtime version of GDAL does not meet the required\n    version of GDAL.\"\"\"\n\n\nclass WindowEvaluationError(ValueError):\n    \"\"\"Raised when window evaluation fails\"\"\"\n\n\nclass RasterioDeprecationWarning(UserWarning):\n    \"\"\"Rasterio module deprecations\"\"\"\n\n\nclass RasterBlockError(RasterioError):\n    \"\"\"Raised when raster block access fails\"\"\"\n\n\nclass BandOverviewError(UserWarning):\n    \"\"\"Raised when a band overview access fails.\"\"\"\n\n\nclass WarpOptionsError(RasterioError):\n    \"\"\"Raised when options for a warp operation are invalid\"\"\"\n\n\nclass UnsupportedOperation(RasterioError):\n    \"\"\"Raised when reading from a file opened in 'w' mode\"\"\"\n\n\nclass OverviewCreationError(RasterioError):\n    \"\"\"Raised when creation of an overview fails\"\"\"\n\n\nclass DatasetAttributeError(RasterioError, NotImplementedError):\n    \"\"\"Raised when dataset attributes are misused\"\"\"\n\n\nclass PathError(RasterioError):\n    \"\"\"Raised when a dataset path is malformed or invalid\"\"\"\n\n\nclass ResamplingAlgorithmError(RasterioError):\n    \"\"\"Raised when a resampling algorithm is invalid or inapplicable\"\"\"\n\n\nclass TransformError(RasterioError):\n    \"\"\"Raised when transform arguments are invalid\"\"\"\n", "path": "rasterio/errors.py"}], "after_files": [{"content": "\"\"\"Classes capable of reading and writing datasets\n\nInstances of these classes are called dataset objects.\n\"\"\"\n\nimport logging\n\nfrom rasterio._base import (\n    get_dataset_driver, driver_can_create, driver_can_create_copy)\nfrom rasterio._io import (\n    DatasetReaderBase, DatasetWriterBase, BufferedDatasetWriterBase,\n    MemoryFileBase)\nfrom rasterio.windows import WindowMethodsMixin\nfrom rasterio.env import ensure_env, env_ctx_if_needed\nfrom rasterio.transform import TransformMethodsMixin\nfrom rasterio.path import UnparsedPath\n\n\nlog = logging.getLogger(__name__)\n\n\nclass DatasetReader(DatasetReaderBase, WindowMethodsMixin,\n                    TransformMethodsMixin):\n    \"\"\"An unbuffered data and metadata reader\"\"\"\n\n    def __repr__(self):\n        return \"<{} DatasetReader name='{}' mode='{}'>\".format(\n            self.closed and 'closed' or 'open', self.name, self.mode)\n\n\nclass DatasetWriter(DatasetWriterBase, WindowMethodsMixin,\n                    TransformMethodsMixin):\n    \"\"\"An unbuffered data and metadata writer. Its methods write data\n    directly to disk.\n    \"\"\"\n\n    def __repr__(self):\n        return \"<{} DatasetWriter name='{}' mode='{}'>\".format(\n            self.closed and 'closed' or 'open', self.name, self.mode)\n\n\nclass BufferedDatasetWriter(BufferedDatasetWriterBase, WindowMethodsMixin,\n                            TransformMethodsMixin):\n    \"\"\"Maintains data and metadata in a buffer, writing to disk or\n    network only when `close()` is called.\n\n    This allows incremental updates to datasets using formats that don't\n    otherwise support updates, such as JPEG.\n    \"\"\"\n\n    def __repr__(self):\n        return \"<{} BufferedDatasetWriter name='{}' mode='{}'>\".format(\n            self.closed and 'closed' or 'open', self.name, self.mode)\n\n\nclass MemoryFile(MemoryFileBase):\n    \"\"\"A BytesIO-like object, backed by an in-memory file.\n\n    This allows formatted files to be read and written without I/O.\n\n    A MemoryFile created with initial bytes becomes immutable. A\n    MemoryFile created without initial bytes may be written to using\n    either file-like or dataset interfaces.\n\n    Examples\n    --------\n\n    A GeoTIFF can be loaded in memory and accessed using the GeoTIFF\n    format driver\n\n    >>> with open('tests/data/RGB.byte.tif', 'rb') as f, MemoryFile(f) as memfile:\n    ...     with memfile.open() as src:\n    ...         pprint.pprint(src.profile)\n    ...\n    {'count': 3,\n     'crs': CRS({'init': 'epsg:32618'}),\n     'driver': 'GTiff',\n     'dtype': 'uint8',\n     'height': 718,\n     'interleave': 'pixel',\n     'nodata': 0.0,\n     'tiled': False,\n     'transform': Affine(300.0379266750948, 0.0, 101985.0,\n           0.0, -300.041782729805, 2826915.0),\n     'width': 791}\n\n    \"\"\"\n    def __init__(self, file_or_bytes=None, dirname=None, filename=None, ext='.tif'):\n        \"\"\"Create a new file in memory\n\n        Parameters\n        ----------\n        file_or_bytes : file-like object or bytes, optional\n            File or bytes holding initial data.\n        filename : str, optional\n            An optional filename. A unique one will otherwise be generated.\n        ext : str, optional\n            An optional extension.\n\n        Returns\n        -------\n        MemoryFile\n        \"\"\"\n        super(MemoryFile, self).__init__(\n            file_or_bytes=file_or_bytes, dirname=dirname, filename=filename, ext=ext)\n\n    @ensure_env\n    def open(self, driver=None, width=None, height=None, count=None, crs=None,\n             transform=None, dtype=None, nodata=None, sharing=False, **kwargs):\n        \"\"\"Open the file and return a Rasterio dataset object.\n\n        If data has already been written, the file is opened in 'r'\n        mode. Otherwise, the file is opened in 'w' mode.\n\n        Parameters\n        ----------\n        Note well that there is no `path` parameter: a `MemoryFile`\n        contains a single dataset and there is no need to specify a\n        path.\n\n        Other parameters are optional and have the same semantics as the\n        parameters of `rasterio.open()`.\n        \"\"\"\n        mempath = UnparsedPath(self.name)\n\n        if self.closed:\n            raise IOError(\"I/O operation on closed file.\")\n        if len(self) > 0:\n            log.debug(\"VSI path: {}\".format(mempath.path))\n            return DatasetReader(mempath, driver=driver, sharing=sharing, **kwargs)\n        else:\n            writer = get_writer_for_driver(driver)\n            return writer(mempath, 'w+', driver=driver, width=width,\n                          height=height, count=count, crs=crs,\n                          transform=transform, dtype=dtype,\n                          nodata=nodata, sharing=sharing, **kwargs)\n\n    def __enter__(self):\n        self._env = env_ctx_if_needed()\n        self._env.__enter__()\n        return self\n\n    def __exit__(self, *args, **kwargs):\n        self.close()\n        self._env.__exit__()\n\n\nclass ZipMemoryFile(MemoryFile):\n    \"\"\"A read-only BytesIO-like object backed by an in-memory zip file.\n\n    This allows a zip file containing formatted files to be read\n    without I/O.\n    \"\"\"\n\n    def __init__(self, file_or_bytes=None):\n        super(ZipMemoryFile, self).__init__(file_or_bytes, ext='zip')\n\n    @ensure_env\n    def open(self, path, driver=None, sharing=False, **kwargs):\n        \"\"\"Open a dataset within the zipped stream.\n\n        Parameters\n        ----------\n        path : str\n            Path to a dataset in the zip file, relative to the root of the\n            archive.\n\n        Other parameters are optional and have the same semantics as the\n        parameters of `rasterio.open()`.\n\n        Returns\n        -------\n        A Rasterio dataset object\n        \"\"\"\n        zippath = UnparsedPath('/vsizip{0}/{1}'.format(self.name, path.lstrip('/')))\n\n        if self.closed:\n            raise IOError(\"I/O operation on closed file.\")\n        return DatasetReader(zippath, driver=driver, sharing=sharing, **kwargs)\n\n\ndef get_writer_for_driver(driver):\n    \"\"\"Return the writer class appropriate for the specified driver.\"\"\"\n    if not driver:\n        raise ValueError(\"'driver' is required to write dataset.\")\n    cls = None\n    if driver_can_create(driver):\n        cls = DatasetWriter\n    elif driver_can_create_copy(driver):  # pragma: no branch\n        cls = BufferedDatasetWriter\n    return cls\n\n\ndef get_writer_for_path(path, driver=None):\n    \"\"\"Return the writer class appropriate for the existing dataset.\"\"\"\n    if not driver:\n        driver = get_dataset_driver(path)\n    return get_writer_for_driver(driver)\n", "path": "rasterio/io.py"}, {"content": "\"\"\"Errors and Warnings.\"\"\"\n\nfrom click import FileError\n\n\nclass RasterioError(Exception):\n    \"\"\"Root exception class\"\"\"\n\n\nclass WindowError(RasterioError):\n    \"\"\"Raised when errors occur during window operations\"\"\"\n\n\nclass CRSError(ValueError):\n    \"\"\"Raised when a CRS string or mapping is invalid or cannot serve\n    to define a coordinate transformation.\"\"\"\n\n\nclass EnvError(RasterioError):\n    \"\"\"Raised when the state of GDAL/AWS environment cannot be created\n    or modified.\"\"\"\n\n\nclass DriverCapabilityError(RasterioError, ValueError):\n    \"\"\"Raised when a format driver can't a feature such as writing.\"\"\"\n\n\nclass DriverRegistrationError(ValueError):\n    \"\"\"Raised when a format driver is requested but is not registered.\"\"\"\n\n\nclass FileOverwriteError(FileError):\n    \"\"\"Raised when Rasterio's CLI refuses to clobber output files.\"\"\"\n\n    def __init__(self, message):\n        \"\"\"Raise FileOverwriteError with message as hint.\"\"\"\n        super(FileOverwriteError, self).__init__('', hint=message)\n\n\nclass RasterioIOError(IOError):\n    \"\"\"Raised when a dataset cannot be opened using one of the\n    registered format drivers.\"\"\"\n\n\nclass NodataShadowWarning(UserWarning):\n    \"\"\"Warn that a dataset's nodata attribute is shadowing its alpha band.\"\"\"\n\n    def __str__(self):\n        return (\"The dataset's nodata attribute is shadowing \"\n                \"the alpha band. All masks will be determined \"\n                \"by the nodata attribute\")\n\n\nclass NotGeoreferencedWarning(UserWarning):\n    \"\"\"Warn that a dataset isn't georeferenced.\"\"\"\n\n\nclass ShapeSkipWarning(UserWarning):\n    \"\"\"Warn that an invalid or empty shape in a collection has been skipped\"\"\"\n\n\nclass GDALBehaviorChangeException(RuntimeError):\n    \"\"\"Raised when GDAL's behavior differs from the given arguments.  For\n    example, antimeridian cutting is always on as of GDAL 2.2.0.  Users\n    expecting it to be off will be presented with a MultiPolygon when the\n    rest of their code expects a Polygon.\n\n        # Raises an exception on GDAL >= 2.2.0\n        rasterio.warp.transform_geometry(\n            src_crs, dst_crs, antimeridian_cutting=False)\n    \"\"\"\n\n\nclass GDALOptionNotImplementedError(RasterioError):\n    \"\"\"A dataset opening or dataset creation option can't be supported\n\n    This will be raised from Rasterio's shim modules. For example, when\n    a user passes arguments to open_dataset() that can't be evaluated\n    by GDAL 1.x.\n    \"\"\"\n\nclass GDALVersionError(RasterioError):\n    \"\"\"Raised if the runtime version of GDAL does not meet the required\n    version of GDAL.\"\"\"\n\n\nclass WindowEvaluationError(ValueError):\n    \"\"\"Raised when window evaluation fails\"\"\"\n\n\nclass RasterioDeprecationWarning(UserWarning):\n    \"\"\"Rasterio module deprecations\"\"\"\n\n\nclass RasterBlockError(RasterioError):\n    \"\"\"Raised when raster block access fails\"\"\"\n\n\nclass BandOverviewError(UserWarning):\n    \"\"\"Raised when a band overview access fails.\"\"\"\n\n\nclass WarpOptionsError(RasterioError):\n    \"\"\"Raised when options for a warp operation are invalid\"\"\"\n\n\nclass UnsupportedOperation(RasterioError):\n    \"\"\"Raised when reading from a file opened in 'w' mode\"\"\"\n\n\nclass OverviewCreationError(RasterioError):\n    \"\"\"Raised when creation of an overview fails\"\"\"\n\n\nclass DatasetAttributeError(RasterioError, NotImplementedError):\n    \"\"\"Raised when dataset attributes are misused\"\"\"\n\n\nclass PathError(RasterioError):\n    \"\"\"Raised when a dataset path is malformed or invalid\"\"\"\n\n\nclass ResamplingAlgorithmError(RasterioError):\n    \"\"\"Raised when a resampling algorithm is invalid or inapplicable\"\"\"\n\n\nclass TransformError(RasterioError):\n    \"\"\"Raised when transform arguments are invalid\"\"\"\n\n\nclass WarpedVRTError(RasterioError):\n    \"\"\"Raised when WarpedVRT can't be initialized\"\"\"\n", "path": "rasterio/errors.py"}]}