Select to view content in your preferred language

Add Context Manager Methods to ArcPy Cursor Classes

3821
11
10-03-2023 08:24 AM
Status: Implemented
GIS_Spellblade
Frequent Contributor

TLDR: ArcPy Cursors do not universally respect Context Managers; Add Appropriate Methods to the Cursor Class to enable graceful exits (discard, disconnect, close) as part of the __exit__() call.

 

Recently an Esri blog was published detailing a relatively common "gotcha" pertaining to cursors applied against a file geodatabase: https://www.esri.com/arcgis-blog/products/arcgis-pro/data-management/locked-by-another-application-u...

The given solution was to embed a del call within a context manager (a with statement). This idea runs contrary to the syntactic sugar abstraction that is a context manager, or put otherwise, we use a context manager to not have to worry about some of the nitty-gritty of programming details like closing files and cursors. The suggested solution is problematic as it requires a user to have in-depth knowledge of what is likely an oversight due to the long-lived nature of the ArcPy library; further compounded by the lack of documentation outside of the blog's reference. https://realpython.com/python-with-statement/

This idea is to add in certain methods within the ArcPy Cursor Class to have it conform to the Python DBA Specification (https://peps.python.org/pep-0249/); while this sounds like a lot, in reality the additions would be fairly minimal and would be similar to the solution discussed in this blog post: https://dev.to/c_v_ya/sql-cursor-via-context-manager-2gc7

Having an implementation would make the ArcPy library more robust and allow the ArcPy Cursor to behave the same way regardless of the underlying data source-- --which would be the expectation for any other library, a function to behave the same way for all the inputs it is programmed to accept. This would benefit the users as they would not need to have an arcane understanding of a library that is possibly older than some of its users; they would be able to trust that ArcPy cursors comply with context managers which is a common usage pattern since Python 2.5.

11 Comments
MarcoBoeringa

Hi @HannesZiegler ,

Thanks for getting back on this.

Considering the Dutch branch already reproduced these issues, you should have no trouble reproducing them as well.

For both cases, I supplied really simple reproducible cases, just a few tiny scripts attached to a toolbox and some data. Should be a breeze to setup and test. Just look up the Dutch case numbers and download the stuff when needed.

Google Translate is your best friend when it comes to the details of my conversation with the Dutch ESRI employees, but for at least one of the cases, I wrote the steps to reproduce it already in English when sending it to the Dutch branch. For the other one it is in Dutch, but an automatic translation should suffice.

Especially the Definition Query issue is egregious, as it breaks a crucial contract that ESRI itself defined for Layer objects on the one hand, and selections on the other, but the fixing of the locking issue and being able to clear a lock with 'Layer.SetSelectionSet([],"NEW")' would be highly welcome as well.