1
0
mirror of https://github.com/JoelBender/bacpypes synced 2025-09-28 22:15:23 +08:00

more type checking for writing property values when using WriteProperty(), and fewer when setting them directly using 'obj.prop = value' in applications

This commit is contained in:
Joel Bender 2015-11-03 22:48:05 -05:00
parent 06fb7a87a2
commit 1bd0fb4a23

View File

@ -6,7 +6,8 @@ Object
import sys
from .errors import ConfigurationError, ExecutionError
from .errors import ConfigurationError, ExecutionError, \
InvalidParameterDatatype
from .debugging import function_debugging, ModuleLogger, Logging
from .primitivedata import Atomic, BitString, Boolean, CharacterString, Date, \
@ -192,12 +193,21 @@ class Property(Logging):
if not self.mutable:
raise ExecutionError(errorClass='property', errorCode='writeAccessDenied')
# if it's atomic assume correct datatype
if issubclass(self.datatype, Atomic):
if _debug: Property._debug(" - property is atomic, assumed correct type")
elif isinstance(value, self.datatype):
if _debug: Property._debug(" - correct type")
elif arrayIndex is not None:
# if it's atomic assume correct datatype
if issubclass(self.datatype, Atomic):
if _debug: Property._debug(" - property is atomic, checking value")
if not self.datatype.is_valid(value):
raise InvalidParameterDatatype("%s must be of type %s" % (
self.identifier, self.datatype.__name__,
))
elif not isinstance(value, self.datatype):
if _debug: Property._debug(" - property is not atomic and wrong type")
raise InvalidParameterDatatype("%s must be of type %s" % (
self.identifier, self.datatype.__name__,
))
if arrayIndex is not None:
if not issubclass(self.datatype, Array):
raise ExecutionError(errorClass='property', errorCode='propertyIsNotAnArray')
@ -211,10 +221,6 @@ class Property(Logging):
arry[arrayIndex] = value
return
elif value is not None:
# coerce the value
value = self.datatype(value)
if _debug: Property._debug(" - coerced the value: %r", value)
# seems to be OK
obj._values[self.identifier] = value