Re: [Pyobjc-dev] caution with BOOL parameters
Brought to you by:
ronaldoussoren
From: Ronald O. <ous...@ci...> - 2003-03-01 19:28:18
|
On Saturday, Mar 1, 2003, at 19:35 Europe/Amsterdam, Bob Pasker wrote: > if you try to pass an int whose magnitude is greater than can > be stored in a char (-256/+255), then you will get > > objc.error: depythonifying 'char', got int of wrong magnitude > > for example, with a table with greater than 255 rows, the following > will fail: > > self.theButton.setEnabled_(self.theTableView.numberOfRows()) > > but > > self.theButton.setEnabled_(self.theTableView.numberOfRows() != 0) > > works as expected. i assume (but haven't tested), that (say) delegate > methods > which return a BOOL will also fail if the return value is outside the > range. > > also, one things i've done in my version of obj_support.m is to change > identical > messages to different ones, e.g., changing 'depythonifying' to > 'depythonifyingN' > so i can tell which constraint failed: > > ObjCErr_Set(ObjCExc_error, > "depythonifying3 'char', got %s of " > "wrong magnitude", > argument->ob_type->tp_name); > > since python doesnt have a char datatype, one alternative is to relax > the > constraint (in obj_support.m) that _C_CHR parameters must be within > range, > and instead mask off all but the bottom 8 bits. Boy am I glad I didn't apply a change I was thinking of ;-). I thought BOOL is an unsigned char and was thinking about changing the result of pythonifying a 'char' to a string of lenght 1 (what basicly is the corresponding "type" in Python). I don't think masking of all but the bottom 8 bits would be a right idea. What if you want to return False from a method of a list of items is empty and True otherwise. The obvious mostly-pythonic way would be: def myMethod(self): return len(someList) This would fail if the length of the list is a multiple of 256. The right solution for this user-education, too bad we don't have a FAQ yet. Ronald |