Skip to content

Commit

Permalink
Add implicit Env to Collection if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillies committed Dec 23, 2018
1 parent 07dff29 commit eef553e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ Changes

All issue numbers are relative to https://github.com/Toblerity/Fiona/issues.

1.8.5 (TBD)
-----------

- On entering a collection context (Collection.__enter__) a new anonymous GDAL
environment is created if needed and entered. This makes `with
fiona.open(...) as collection:` roughly equivalent to `with fiona.open(...)
as collection, Env():`. This helps prevent bugs when Collections are created
and then used later or in different scopes.

1.8.4 (2018-12-10)
------------------

Expand Down
9 changes: 6 additions & 3 deletions fiona/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
from fiona.ogrext import buffer_to_virtual_file, remove_virtual_file, GEOMETRY_TYPES
from fiona.errors import (DriverError, SchemaError, CRSError, UnsupportedGeometryTypeError, DriverSupportError)
from fiona.logutils import FieldSkipLogFilter
from fiona._env import driver_count, get_gdal_release_name, get_gdal_version_tuple
from fiona.env import Env
from fiona._env import get_gdal_release_name, get_gdal_version_tuple
from fiona.env import env_ctx_if_needed
from fiona.errors import FionaDeprecationWarning
from fiona.drvsupport import supported_drivers
from fiona.path import Path, UnparsedPath, vsi_path, parse_path
from fiona.path import Path, vsi_path, parse_path
from six import string_types, binary_type


Expand Down Expand Up @@ -459,10 +459,13 @@ def closed(self):

def __enter__(self):
logging.getLogger('fiona.ogrext').addFilter(self.field_skip_log_filter)
self._env = env_ctx_if_needed()
self._env.__enter__()
return self

def __exit__(self, type, value, traceback):
logging.getLogger('fiona.ogrext').removeFilter(self.field_skip_log_filter)
self._env.__exit__()
self.close()

def __del__(self):
Expand Down
20 changes: 17 additions & 3 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Testing collections and workspaces

import datetime
import logging
import sys
import re

import pytest

import fiona
from fiona.collection import Collection, supported_drivers
from fiona.env import getenv
from fiona.errors import FionaValueError, DriverError

from .conftest import WGS84PATTERN
Expand Down Expand Up @@ -872,7 +872,7 @@ def test_collection_zip_http():

def test_encoding_option_warning(tmpdir, caplog):
"""There is no ENCODING creation option log warning for GeoJSON"""
ds = fiona.Collection(str(tmpdir.join("test.geojson")), "w", driver="GeoJSON", crs="epsg:4326",
fiona.Collection(str(tmpdir.join("test.geojson")), "w", driver="GeoJSON", crs="epsg:4326",
schema={"geometry": "Point", "properties": {"foo": "int"}})
assert not caplog.text

Expand All @@ -881,7 +881,21 @@ def test_closed_session_next(path_coutwildrnp_shp):
"""Confirm fix for issue #687"""
src = fiona.open(path_coutwildrnp_shp)
itr = iter(src)
feats = list(itr)
list(itr)
src.close()
with pytest.raises(FionaValueError):
next(itr)


def test_collection_no_env(path_coutwildrnp_shp):
"""We have no GDAL env left over from open"""
collection = fiona.open(path_coutwildrnp_shp)
assert collection
with pytest.raises(Exception):
getenv()


def test_collection_env(path_coutwildrnp_shp):
"""We have a GDAL env within collection context"""
with fiona.open(path_coutwildrnp_shp):
assert 'FIONA_ENV' in getenv()

0 comments on commit eef553e

Please sign in to comment.