abortOperation, do you need to stop editing? arcpy.da.Editor

794
2
Jump to solution
04-02-2018 11:09 AM
MKF62
by
Occasional Contributor III

Quick question - when you call abortOperation() in an edit session -- you still have to stopEditing() afterward right? abortOperation is described as "canceling" an edit, I wasn't sure if stopEditing was necessary. Does what I have below look appropriate?

try:
    editor = arcpy.da.Editor(db_con)
    editor.startEditing(False, False)
    editor.startOperation()
    with arcpy.da.InsertCursor(gpTestWriteFC, ['SHAPE@', 'FirstName', 'LastName']) as iCursor:
        for item in insertList:
            iCursor.insertRow(item)
    editor.stopOperation()
    editor.stopEditing(True)
except:
    if editor.isEditing == True:
        editor.abortOperation()
        editor.stopEditing(False)‍‍‍‍‍‍‍‍‍‍‍‍‍
        fullError = traceback.format_exc()
        arcpy.AddError(fullError)
        sys.exit(1)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
BlakeTerhune
MVP Regular Contributor

From the documentation:

Editor—Help | ArcGIS for Desktop 

stopEditing should not be called while an edit operation is in progress. Instead, abort the edit operation and stop the edit session.

View solution in original post

2 Replies
BlakeTerhune
MVP Regular Contributor

From the documentation:

Editor—Help | ArcGIS for Desktop 

stopEditing should not be called while an edit operation is in progress. Instead, abort the edit operation and stop the edit session.

DanPatterson_Retired
MVP Emeritus

Then there is the whole 'with' thing

Edit sessions and operations can also be used with Python with statements. The with statements act as context managers and handle the appropriate start, stop, and abort calls for you. The example below highlights the basic structure of the Editor class used with with statements.

and the caveat about doing multiple edit operations.  I wouldn't use a try except block since 'with' is supposed to handle failure as well (in that edits will be terminated properly.)

Avoid using a cursor across multiple edit operations. Doing so can result in unexpected behavior and data loss.