diff --git a/tconfpy.py b/tconfpy.py index 60e578f..7c7ea58 100755 --- a/tconfpy.py +++ b/tconfpy.py @@ -6,7 +6,7 @@ # Program Information PROGNAME = "tconfpy" -RCSID = "$Id: tconfpy.py,v 1.139 2004/03/24 23:34:59 tundra Exp $" +RCSID = "$Id: tconfpy.py,v 1.140 2004/03/25 00:18:44 tundra Exp $" VERSION = RCSID.split()[2] # Copyright Information @@ -100,10 +100,11 @@ # String Representations Of Booleans -Booleans = (("1", True), ("0", False), - ("True", True), ("False", False), - ("Yes", True), ("No", False), - ("On", True), ("Off", False)) +Booleans = {"1" : True, "0" : False, + "True" : True, "False" : False, + "Yes" : True, "No" : False, + "On" : True, "Off" : False + } sFALSE = "False" sTRUE = "True" @@ -237,6 +238,7 @@ Messages["eIFEXTRATXT"] = FILENUM + "Extra Text On Line. '%s' Only Accepts Variable References As Arguments" Messages["eVARREADONLY"] = FILENUM + "Variable '%s' Is Read-Only. Cannot Change Its Value" Messages["eRESERVED"] = FILENUM + "Cannot Modify Value Of Reserved Symbol '%s'" +Messages["eTYPEBAD"] = FILENUM + "Type Mismatch. '%s' Must Be Assigned Values Of Type %s Only" Messages["eVARUNDEF"] = FILENUM + "Attempt To Reference Undefined Variable '%s'" @@ -853,9 +855,32 @@ # Otherwise, update an existing entry else: + + # Type Enforcement + # We try to coerce the new value into + # the specified type. If it works, we + # update, otherwise we throw an error + # and do nothing. + + try: + typ = SymTable[l].Type + + # Booleans are a special case - we accept only + # a limited number of strings on the RHS + + if typ == TYPE_BOOL: + r = Booleans[r.capitalize()] + + # For everything else, try an explicit coercion + else: + r = typ(r) + + except: + ErrorMsg("eTYPEBAD", (cfgfile, linenum, l, str(typ).split()[1][:-1])) + + # Finally, we can save the new variable SymTable[l].Value = r - ##### # Line Format Is Not In One Of The Recognized Forms - Syntax Error #####