[#1928] Fix deleting non-existent items

This commit is contained in:
Aaron McMillin 2019-09-18 23:17:19 -04:00
parent 772cfda693
commit cb04a71a23
2 changed files with 10 additions and 3 deletions

View file

@ -1697,14 +1697,16 @@ class CmdSetAttribute(ObjManipCommand):
val = obj.attributes.get(key) val = obj.attributes.get(key)
deep = self.do_nested_lookup(val, *nested_keys[:-1]) deep = self.do_nested_lookup(val, *nested_keys[:-1])
if deep is not self.not_found: if deep is not self.not_found:
try:
del deep[del_key] del deep[del_key]
except (IndexError, KeyError, TypeError):
continue
obj.attributes.add(key, val) obj.attributes.add(key, val)
return "\nDeleted attribute '%s' (= nested) from %s." % (attr, obj.name) return "\nDeleted attribute '%s' (= nested) from %s." % (attr, obj.name)
else: else:
exists = obj.attributes.has(key) exists = obj.attributes.has(key)
obj.attributes.remove(attr) obj.attributes.remove(attr)
return "\nDeleted attribute '%s' (= %s) from %s." % (attr, exists, obj.name) return "\nDeleted attribute '%s' (= %s) from %s." % (attr, exists, obj.name)
else:
return "\n%s has no attribute '%s'." % (obj.name, attr) return "\n%s has no attribute '%s'." % (obj.name, attr)
def set_attr(self, obj, attr, value): def set_attr(self, obj, attr, value):

View file

@ -548,6 +548,9 @@ class TestBuilding(CommandTest):
"Obj/test1[0] =", "Deleted attribute 'test1[0]' (= nested) from Obj.") "Obj/test1[0] =", "Deleted attribute 'test1[0]' (= nested) from Obj.")
self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 2") self.call(building.CmdSetAttribute(), "Obj/test1[0]", "Attribute Obj/test1[0] = 2")
self.call(building.CmdSetAttribute(), "Obj/test1[1]", "Obj has no attribute 'test1[1]'.") self.call(building.CmdSetAttribute(), "Obj/test1[1]", "Obj has no attribute 'test1[1]'.")
# Delete non-existent
self.call(building.CmdSetAttribute(),
"Obj/test1[5] =", "Obj has no attribute 'test1[5]'.")
# removing white space proves real parsing # removing white space proves real parsing
self.call(building.CmdSetAttribute(), self.call(building.CmdSetAttribute(),
@ -567,6 +570,8 @@ class TestBuilding(CommandTest):
self.call(building.CmdSetAttribute(), "Obj/test2['two']", "Obj has no attribute 'test2['two']'.") self.call(building.CmdSetAttribute(), "Obj/test2['two']", "Obj has no attribute 'test2['two']'.")
self.call(building.CmdSetAttribute(), "Obj/test2", "Attribute Obj/test2 = {'one': 99, 'three': 3}") self.call(building.CmdSetAttribute(), "Obj/test2", "Attribute Obj/test2 = {'one': 99, 'three': 3}")
self.call(building.CmdSetAttribute(), "Obj/test2[0]", "Obj has no attribute 'test2[0]'.") self.call(building.CmdSetAttribute(), "Obj/test2[0]", "Obj has no attribute 'test2[0]'.")
self.call(building.CmdSetAttribute(),
"Obj/test2['five'] =", "Obj has no attribute 'test2['five']'.")
# Deaper nesting # Deaper nesting
self.call(building.CmdSetAttribute(), self.call(building.CmdSetAttribute(),