Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
No more AttributeErrors when importing cint.py with CBash disabled
Browse files Browse the repository at this point in the history
FormIDs/ActorValues/MGEFCodes are now hashable, to support being used as dictionary keys
  • Loading branch information
waruddar committed Sep 9, 2011
1 parent b492519 commit 7b13670
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 55 deletions.
110 changes: 81 additions & 29 deletions release/cint-template.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ def PositiveIsErrorCheck(result, function, cArguments, *args):
raise

if(CBash):
def LoggingCB(logString):
print logString,
return 0

def RaiseCB():
#Raising is mostly worthless in a callback
#CTypes prints the error, but the traceback is useless
#and it doesn't propagate properly

#Apparently...
#"The standard way of doing something meaningful with the exception is
#to catch it in the callback, set a global flag, somehow encourage the
#C code to unwind back to the original Python call and there check the
#flag and re-raise the exception."

#But this would lead to a large performance hit if it were checked after
#every CBash call. An alternative might be to start a separate thread
#that then raises the error in the main thread after control returns from
#CBash. Dunno.

#This particular callback may disappear, or be morphed into something else
raise CBashError("Check the log.")
return
try:
_CGetVersionMajor = CBash.GetVersionMajor
_CGetVersionMinor = CBash.GetVersionMinor
Expand Down Expand Up @@ -213,35 +236,10 @@ def PositiveIsErrorCheck(result, function, cArguments, *args):
_CGetRecordUpdatedReferences.restype = c_long
_CSetIDFields.restype = c_long
_CGetFieldAttribute.restype = c_ulong

def LoggingCB(logString):
print logString,
return 0

def RaiseCB():
#Raising is mostly worthless in a callback
#CTypes prints the error, but the traceback is useless
#and it doesn't propagate properly

#Apparently...
#"The standard way of doing something meaningful with the exception is
#to catch it in the callback, set a global flag, somehow encourage the
#C code to unwind back to the original Python call and there check the
#flag and re-raise the exception."

#But this would lead to a large performance hit if it were checked after
#every CBash call. An alternative might be to start a separate thread
#that then raises the error in the main thread after control returns from
#CBash. Dunno.

#This particular callback may disappear, or be morphed into something else
raise CBashError("Check the log.")
return

LoggingCallback = CFUNCTYPE(c_long, c_char_p)(LoggingCB)
RaiseCallback = CFUNCTYPE(None)(RaiseCB)
CBash.RedirectMessages(LoggingCallback)
CBash.AllowRaising(RaiseCallback)
LoggingCallback = CFUNCTYPE(c_long, c_char_p)(LoggingCB)
RaiseCallback = CFUNCTYPE(None)(RaiseCB)
CBash.RedirectMessages(LoggingCallback)
CBash.AllowRaising(RaiseCallback)

#Helper functions
class API_FIELDS(object):
Expand Down Expand Up @@ -393,6 +391,9 @@ class UnvalidatedFormID(object):
def __init__(self, master, objectID):
self.master, self.objectID = GPath(master), objectID

def __hash__(self):
return hash((str(self.master), self.objectID))

def __getitem__(self, x):
if x == 0: return self.master
return int(self.objectID & 0x00FFFFFFL)
Expand Down Expand Up @@ -432,6 +433,9 @@ class InvalidFormID(object):
def __init__(self, objectID):
self.objectID = objectID

def __hash__(self):
return hash((None, self.objectID))

def __getitem__(self, x):
if x == 0: return None
return int(self.objectID & 0x00FFFFFFL)
Expand Down Expand Up @@ -459,6 +463,9 @@ class ValidFormID(object):
def __init__(self, master, objectID, shortID, collectionID):
self.master, self.objectID, self.shortID, self._CollectionID = master, objectID, shortID, collectionID

def __hash__(self):
return hash((str(self.master), self.objectID))

def __getitem__(self, x):
if x == 0: return self.master
return int(self.objectID & 0x00FFFFFFL)
Expand Down Expand Up @@ -494,6 +501,9 @@ class EmptyFormID(ValidFormID):
def __init__(self):
pass

def __hash__(self):
return hash(0)

def __getitem__(self, x):
return None

Expand All @@ -515,6 +525,9 @@ class RawFormID(ValidFormID):
def __init__(self, shortID):
self.shortID = shortID

def __hash__(self):
return hash((self.shortID, None))

def __getitem__(self, x):
if x == 0: return self.shortID >> 24
return int(self.shortID & 0x00FFFFFFL)
Expand Down Expand Up @@ -557,6 +570,9 @@ def __init__(self, master, objectID=None):
#CBash FormID, pre-invalidated for all collections
self.formID = FormID.InvalidFormID(objectID)

def __hash__(self):
return hash(self.formID)

def __eq__(self, x):
return x[1] == self.formID[1] and x[0] == self.formID[0]

Expand Down Expand Up @@ -646,6 +662,9 @@ class UnvalidatedActorValue(object):
def __init__(self, master, objectID):
self.master, self.objectID = GPath(master), objectID

def __hash__(self):
return hash((str(self.master), self.objectID))

def __getitem__(self, x):
if x == 0: return self.master
return int(self.objectID & 0x00FFFFFFL)
Expand Down Expand Up @@ -686,6 +705,9 @@ class InvalidActorValue(object):
def __init__(self, objectID):
self.objectID = objectID

def __hash__(self):
return hash((None, self.objectID))

def __getitem__(self, x):
if x == 0: return None
return int(self.objectID & 0x00FFFFFFL)
Expand All @@ -712,6 +734,9 @@ class ValidActorValue(object):
def __init__(self, master, objectID, shortID, collectionID):
self.master, self.objectID, self.shortID, self._CollectionID = master, objectID, shortID, collectionID

def __hash__(self):
return hash((str(self.master), self.objectID))

def __getitem__(self, x):
if x == 0: return self.master
return int(self.objectID & 0x00FFFFFFL)
Expand Down Expand Up @@ -746,6 +771,9 @@ class EmptyActorValue(ValidActorValue):
def __init__(self):
pass

def __hash__(self):
return hash(0)

def __getitem__(self, x):
return None

Expand All @@ -769,6 +797,9 @@ class RawActorValue(ValidActorValue):
def __init__(self, shortID):
self.shortID = shortID

def __hash__(self):
return hash((self.shortID, None))

def __getitem__(self, x):
if x == 0: return self.shortID >> 24
return int(self.shortID & 0x00FFFFFFL)
Expand Down Expand Up @@ -815,6 +846,9 @@ def __init__(self, master, objectID=None):
#CBash ActorValue, pre-invalidated for all collections
self.actorValue = ActorValue.InvalidActorValue(objectID)

def __hash__(self):
return hash(self.actorValue)

def __eq__(self, x):
return x[1] == self.actorValue[1] and x[0] == self.actorValue[0]

Expand Down Expand Up @@ -908,6 +942,9 @@ class UnvalidatedMGEFCode(object):
def __init__(self, master, objectID):
self.master, self.objectID = GPath(master), objectID

def __hash__(self):
return hash((str(self.master), self.objectID))

def __getitem__(self, x):
if x == 0: return self.master
return int(self.objectID & 0xFFFFFF00L)
Expand Down Expand Up @@ -948,6 +985,9 @@ class InvalidMGEFCode(object):
def __init__(self, objectID):
self.objectID = objectID

def __hash__(self):
return hash((None, self.objectID))

def __getitem__(self, x):
if x == 0: return None
return int(self.objectID & 0xFFFFFF00L)
Expand All @@ -974,6 +1014,9 @@ class ValidMGEFCode(object):
def __init__(self, master, objectID, shortID, collectionID):
self.master, self.objectID, self.shortID, self._CollectionID = master, objectID, shortID, collectionID

def __hash__(self):
return hash((str(self.master), self.objectID))

def __getitem__(self, x):
if x == 0: return self.master
return int(self.objectID & 0xFFFFFF00L)
Expand Down Expand Up @@ -1008,6 +1051,9 @@ class EmptyMGEFCode(ValidMGEFCode):
def __init__(self):
pass

def __hash__(self):
return hash(0)

def __getitem__(self, x):
return None

Expand Down Expand Up @@ -1035,6 +1081,9 @@ class RawMGEFCode(ValidMGEFCode):
def __init__(self, shortID):
self.shortID = shortID

def __hash__(self):
return hash((self.shortID, None))

def __getitem__(self, x):
if isinstance(self.shortID, basestring):
return self.shortID
Expand Down Expand Up @@ -1089,6 +1138,9 @@ def __init__(self, master, objectID=None):
#CBash MGEFCode, pre-invalidated for all collections
self.mgefCode = MGEFCode.InvalidMGEFCode(objectID)

def __hash__(self):
return hash(self.mgefCode)

def __eq__(self, x):
return x[1] == self.mgefCode[1] and x[0] == self.mgefCode[0]

Expand Down
Loading

0 comments on commit 7b13670

Please sign in to comment.