Hi all I'm trying to write up a module that *safely* sets sys.stderr and sys.stdout, and am currently having troubles with the function verification. I need to assure that the function can indeed be called as the Python manual specifies that sys.stdout and sys.stderr should be defined (standard file-like objects, only requiring a function named "write").
For an example output wrapper class, it could look something so simple as this: class OutputWrapper: def __init__(self,CallBack,*args,**kwargs): self.cb = CallBack def write(self,str): self.cb(str,*args,**kwargs) My problem is in verifying the class we're trying to redirect output to. This is what I have so far: def _VerifyOutputStream(fh): if 'write' not in dir(fh): raise AttributeError, "The Output Stream should have a write method." if not callable(fh.write): raise TypeError, "The Output Stream's write method is not callable." ((( On a side note, I have derived the above exception names to use via. experimentation in an interactive shell: >>> class SomeClass:pass ... >>> w = SomeClass() >>> w.write Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: w instance has no attribute 'write' >>> w.write = "Hurr, strings are not callable!" >>> w.write() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: 'str' object is not callable >>> ))) In the above _VerifyOutputStream function, how would I verify that the fh.write method requires only one argument, as the built-in file objects do? Thanks in advance -Wes PS: As a point of reference, to make your lives easier, the links to the Python manual pages: http://docs.python.org/lib/module-sys.html http://docs.python.org/lib/bltin-file-objects.html ((( My experimentation in IDLE yielded no results, really, either. >>> class C: def write(self, str, noreq=None): pass >>> c=C() >>> dir(c.write.func_code) ['__class__', '__cmp__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'co_argcount', 'co_cellvars', 'co_code', 'co_consts', 'co_filename', 'co_firstlineno', 'co_flags', 'co_freevars', 'co_lnotab', 'co_name', 'co_names', 'co_nlocals', 'co_stacksize', 'co_varnames'] >>> c.write.func_code.co_argcount 3 >>> c.write.func_code.co_varnames ('self', 'str', 'noreq') ))) -- http://mail.python.org/mailman/listinfo/python-list