Just a hunch, I think the problem is you've lost the original 'edit' reference with the implicit 'edit' set with your statement:with arcpy.da.Editor(workspace) as editIn other words, I think your statements previous to this block were 'forgotten' (because they're outside the 'scope' of your 'with' block):edit = arcpy.da.Editor(workspace)edit.startEditing(True, False)edit.startOperation()So, edit.startOperation apparently did not throw an error but the later edit.UndoOperation did.Notice the sample code structure working with a file gdb (as in your code) given by the webhelp under the subtopic "Edit sessions and with statements" does not use the coupled start/stop statements:
# The below code structure for edit session with statements from
# Editor (arcpy.da)
import arcpy
# Open an edit session and start an edit operation
with arcpy.da.Editor(workspace) as edit:
# <your edits>
# If an exception is raised, the operation will be aborted, and
# edit session is closed without saving
# If no exceptions are raised, stop the operation and save
# and close the edit session
...looking further down to the samples on that same webpage, a try/except block is used in an example to take advantage of any potentially returned ExecuteError exception so that arcpy 'handles' the edit session by aborting and closing, without any further code:
import arcpy
fc = 'C:/Portland/Portland.gdb/Land/Parks'
workspace = 'C:/Portland/Portland.gdb'
layer_name = 'Parks'
try:
arcpy.MakeFeatureLayer_management(fc, layer_name)
arcpy.SelectLayerByAttribute_management(
layer_name, 'NEW_SELECTION',
"""CUSTODIAN = 'City of Portland'""")
with arcpy.da.Editor(workspace) as edit:
arcpy.CalculateField_management(
layer_name, 'Usage', '"PUBLIC"', 'PYTHON')
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
I think (you'll need to test), you can as needed raise your own error, and likewise raise a specific exception of your own to trigger the same abort and close... that's my hunch. Working within SDE and versions, I believe I'd implement my own start/stop/abort/etc controls, but for a file gdb (as in your case), it should suffice to use an exception.Hope that helps,Wayne