Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'canvas': 0.07; 'python3': 0.07; '"c"': 0.09; '*args,': 0.09; 'attributes': 0.09; 'classes.': 0.09; 'implements': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '"""base': 0.16; '**kwargs)': 0.16; 'attribute,': 0.16; 'called,': 0.16; 'name)': 0.16; 'porting': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'retcode': 0.16; 'subject:proxy': 0.16; 'module': 0.19; 'skip:g 40': 0.19; 'settings': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'pointer': 0.24; 'proxy': 0.24; 'subject:problem': 0.24; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'reporting': 0.29; 'skip:( 40': 0.30; 'skip:g 30': 0.30; 'testing.': 0.31; 'class': 0.32; 'run': 0.32; 'style': 0.33; 'skip:_ 10': 0.34; 'something': 0.35; 'etc': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'false': 0.36; 'leads': 0.36; 'object,': 0.36; 'method': 0.36; 'needed': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'real': 0.63; 'different': 0.65; 'here': 0.66; 'between': 0.67; 'received:109': 0.72; 'records': 0.73; 'cut': 0.74; 'trick,': 0.84; 'recover': 0.91; 'differences': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robin Becker Subject: reporting proxy porting problem Date: Thu, 28 Nov 2013 11:12:10 +0000 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 109.174.168.73 User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385637145 news.xs4all.nl 15981 [2001:888:2000:d::a6]:47341 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60682 I am in the process of porting reportlab to python3.3, one of the contributions is a module that implements a reporting proxy with a canvas that records all access calls and attribute settings etc etc. This fails under python3 because of differences between old and new style classes. I find that I don't understand exactly how the original works so well, but here is a cut down version ########################################################################## class Canvas: def __init__(self,*args,**kwds): self._fontname = 'Helvetica' class PDFAction : """Base class to fake method calls or attributes on Canvas""" def __init__(self, parent, action) : """Saves a pointer to the parent object, and the method name.""" self._parent = parent self._action = action def __getattr__(self, name) : """Probably a method call on an attribute, returns the real one.""" print('PDFAction.__getattr__(%s)' % name) return getattr(getattr(self._parent._underlying, self._action), name) def __call__(self, *args, **kwargs) : """The fake method is called, print it then call the real one.""" if not self._parent._parent._in : self._precomment() self._postcomment() self._parent._parent._in += 1 meth = getattr(self._parent._underlying, self._action) retcode = meth(*args,**kwargs) self._parent._parent._in -= 1 return retcode def __hash__(self) : return hash(getattr(self._parent._underlying, self._action)) def _precomment(self) : print('%s(__dict__=%s)._precomment()' % (self.__class__.__name__,repr(self.__dict__))) def _postcomment(self) : print('%s(__dict__=%s)._postcomment()' % (self.__class__.__name__,repr(self.__dict__))) class PyCanvas: _name = "c" def __init__(self, *args, **kwargs) : self._in = 0 self._parent = self # nice trick, isn't it ? self._underlying = Canvas(*args,**kwargs) def __bool__(self) : """This is needed by platypus' tables.""" return 1 def __str__(self) : return 'PyCanvas.__str__()' def __getattr__(self, name) : return PDFAction(self, name) if __name__=='__main__': c = PyCanvas('filepath.pdf') print('c._fontname=%s' % c._fontname) print('is it a string? %r type=%s' % (isinstance(c._fontname,str),type(c._fontname)))) ########################################################################## when run under python27 I see this C:\code\hg-repos\reportlab>\python27\python.exe z.py PDFAction.__getattr__(__str__) c._fontname=Helvetica is it a string? False type= and under python33 I see this C:\code\hg-repos\reportlab>\python33\python.exe z.py c._fontname=<__main__.PDFAction object at 0x00BF8830> is it a string? False type= clearly something different is happening and this leads to failure in the real pycanvas module testing. Is there a way to recover the old behaviour(s)? -- Robin Becker