Skip to content

Commit

Permalink
Merge pull request #10738 from rouault/autotest_remove_release
Browse files Browse the repository at this point in the history
autotest: avoid deprecation warnings related to DataSource.Release()
  • Loading branch information
rouault authored Sep 7, 2024
2 parents 8a94525 + 5bb6103 commit 4ca4d62
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 66 deletions.
103 changes: 41 additions & 62 deletions autotest/ogr/ogr_index_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@ def startup_and_cleanup():
@contextlib.contextmanager
def create_index_p_test_file():
drv = ogr.GetDriverByName("MapInfo File")
p_ds = drv.CreateDataSource("index_p.mif")
p_lyr = p_ds.CreateLayer("index_p")
with drv.CreateDataSource("index_p.mif") as p_ds:
p_lyr = p_ds.CreateLayer("index_p")

ogrtest.quick_create_layer_def(p_lyr, [("PKEY", ogr.OFTInteger)])
ogrtest.quick_create_feature(p_lyr, [5], None)
ogrtest.quick_create_feature(p_lyr, [10], None)
ogrtest.quick_create_feature(p_lyr, [9], None)
ogrtest.quick_create_feature(p_lyr, [4], None)
ogrtest.quick_create_feature(p_lyr, [3], None)
ogrtest.quick_create_feature(p_lyr, [1], None)

p_ds.Release()
ogrtest.quick_create_layer_def(p_lyr, [("PKEY", ogr.OFTInteger)])
ogrtest.quick_create_feature(p_lyr, [5], None)
ogrtest.quick_create_feature(p_lyr, [10], None)
ogrtest.quick_create_feature(p_lyr, [9], None)
ogrtest.quick_create_feature(p_lyr, [4], None)
ogrtest.quick_create_feature(p_lyr, [3], None)
ogrtest.quick_create_feature(p_lyr, [1], None)

yield

Expand All @@ -69,21 +67,19 @@ def create_index_p_test_file():
@contextlib.contextmanager
def create_join_t_test_file(create_index=False):
drv = ogr.GetDriverByName("ESRI Shapefile")
s_ds = drv.CreateDataSource("join_t.dbf")
s_lyr = s_ds.CreateLayer("join_t", geom_type=ogr.wkbNone)

ogrtest.quick_create_layer_def(
s_lyr, [("SKEY", ogr.OFTInteger), ("VALUE", ogr.OFTString, 16)]
)
with drv.CreateDataSource("join_t.dbf") as s_ds:
s_lyr = s_ds.CreateLayer("join_t", geom_type=ogr.wkbNone)

for i in range(20):
ogrtest.quick_create_feature(s_lyr, [i, "Value " + str(i)], None)
ogrtest.quick_create_layer_def(
s_lyr, [("SKEY", ogr.OFTInteger), ("VALUE", ogr.OFTString, 16)]
)

if create_index:
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING skey")
for i in range(20):
ogrtest.quick_create_feature(s_lyr, [i, "Value " + str(i)], None)

s_ds.Release()
if create_index:
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING skey")

yield

Expand Down Expand Up @@ -188,12 +184,9 @@ def test_ogr_index_indexed_join_works():

def test_ogr_index_drop_index_removes_files():
with create_join_t_test_file(create_index=True):
s_ds = ogr.OpenShared("join_t.dbf", update=1)

s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

# After dataset closing, check that the index files do not exist after
# dropping the index
Expand Down Expand Up @@ -228,17 +221,13 @@ def test_ogr_index_attribute_filter_works_after_drop_index():
def test_ogr_index_recreating_index_causes_index_files_to_be_created():

with create_join_t_test_file(create_index=True):
s_ds = ogr.OpenShared("join_t.dbf", update=1)

s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

# Re-create an index
s_ds = ogr.OpenShared("join_t.dbf", update=1)
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")
s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")

for filename in ["join_t.idm", "join_t.ind"]:
try:
Expand All @@ -255,17 +244,13 @@ def test_ogr_index_recreating_index_causes_index_files_to_be_created():
def test_ogr_index_recreating_index_causes_index_to_be_populated():

with create_join_t_test_file(create_index=True):
s_ds = ogr.OpenShared("join_t.dbf", update=1)

s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

# Re-create an index
s_ds = ogr.OpenShared("join_t.dbf", update=1)
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")
s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")

with open("join_t.idm", "rt") as f:
xml = f.read()
Expand All @@ -280,25 +265,19 @@ def test_ogr_index_recreating_index_causes_index_to_be_populated():
def test_ogr_index_creating_index_in_separate_steps_works():

with create_join_t_test_file(create_index=True):
s_ds = ogr.OpenShared("join_t.dbf", update=1)

s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
s_ds.ExecuteSQL("DROP INDEX ON join_t USING value")
s_ds.ExecuteSQL("DROP INDEX ON join_t USING skey")

# Re-create an index
s_ds = ogr.OpenShared("join_t.dbf", update=1)
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")
s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING value")

# Close the dataset and re-open
s_ds = ogr.OpenShared("join_t.dbf", update=1)
# At this point the .ind was opened in read-only. Now it
# will be re-opened in read-write mode
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING skey")

s_ds.Release()
with ogr.OpenShared("join_t.dbf", update=1) as s_ds:
# At this point the .ind was opened in read-only. Now it
# will be re-opened in read-write mode
s_ds.ExecuteSQL("CREATE INDEX ON join_t USING skey")

with open("join_t.idm", "rt") as f:
xml = f.read()
Expand Down
10 changes: 6 additions & 4 deletions autotest/ogr/ogr_refcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ def test_ogr_refcount_2():
# Verify that releasing the datasources has the expected behaviour.


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_ogr_refcount_3():

ds_1 = ogr.OpenShared("data/idlink.dbf")
ds_3 = ogr.OpenShared("data/idlink.dbf")

assert ds_1 is not None
assert ds_3 is not None

ds_3.Release()
ds_2 = ogr.OpenShared("data/idlink.dbf")
assert ds_2 is not None
assert ds_1.GetRefCount() == 2
assert ds_2.GetRefCount() == 2
ds_2.Release()

assert ds_1.GetRefCount() == 1

Expand Down

0 comments on commit 4ca4d62

Please sign in to comment.