Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
> > Can you provide an example of raising a unicode error like this: > > > >u = makeUnicodeString() # your choice of function > >int(u) > > In Python 2.3, I get this: > > >>> int(u"\u1234") > Traceback (most recent call last): > File "", line 1, in ? > UnicodeEncodeError: 'decimal' codec can't encode character '\u1234' in position 0: >invalid decimal Unicode string > >>> > > In Python 2.2, this raises ValueError. I spoke too soon. UnicodeEncodeError is a subclass of ValueError. So catching ValueError from int(str_or_unicode) is the way to go. Who's writing that Zope 3 style guide again? --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
> > Anything can raise MemoryError. > > Ok. But I don't think regular application code should catch these. Correct. > > On converting an 8bit string to an int: > > > > ValueError *only* > > Ok. > > > On converting a Unicode string to an int: > > > > ValueError > > UnicodeError (or UnicodeEncodeError, which is a subclass of it) > > Can you provide an example of raising a unicode error like this: > >u = makeUnicodeString() # your choice of function >int(u) In Python 2.3, I get this: >>> int(u"\u1234") Traceback (most recent call last): File "", line 1, in ? UnicodeEncodeError: 'decimal' codec can't encode character '\u1234' in position 0: invalid decimal Unicode string >>> In Python 2.2, this raises ValueError. I think I may have to report this as a bug in Python 2.3 though. > My point is that once you have a valid unicode object, I don't see how > calling int(valid_unicode_object) will raise a UnicodeError. > > If this is so, then the style should be: > >value = expression_to_compute_value >try: >i = int(value) >except ValueError: ># take corrective action > > rather than: > >try: >i = int(expression_to_compute_value) >except: # Note: calling 'int()' can raise just about anything ># take corrective action Even if we decide that we have to use a bare except after all, the expression_to_compute_value should still be moved outside the try/except. --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
> I think this would be a useful note for the Zope3 style guide. > > > What exceptions can int() raise? > > On converting a preexisting value to an int: > >ValueError, OverflowError, TypeError, AttributeError > >(Any others?) > > On converting an instance that implements __int__: > >Anything at all. >It can even return a non-int value. > > On evaluating the expression inside the int() brackets: > >Anything at all. Anything can raise MemoryError. On converting an 8bit string to an int: ValueError *only* On converting a Unicode string to an int: ValueError UnicodeError (or UnicodeEncodeError, which is a subclass of it) --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
> I saw "try: int() except:" pattern in many places through all the Zope > code. I think catching ValueError only does the job, but then we should > replace it in all the places. :-) Yes, catching ValueError is sufficient for string conversions to int. I thought you could get an OverflowError, but that can only happen when the input is a Python long or float -- int("999") raises ValueError, as does int("xxx"). --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
On Wednesday 11 December 2002 03:43 am, Dmitry Vasiliev wrote: > Casey Duncan wrote: > > On Tuesday 10 December 2002 03:14 pm, Guido van Rossum wrote: > > > >>>Can we get the same patch without the generic "except:", please? > >>> > >>>the last thing I want is a database corruption caused by resizing the > >>>Edit box... > >> > >>Why would this particular except clause cause database corruption? > > > > I think Leonard is refering to catching ConflictErrors. However I don't think > > int() touches the database ;^). > > I saw "try: int() except:" pattern in many places through all the Zope > code. I think catching ValueError only does the job, but then we should > replace it in all the places. :-) Nope, as Guido pointed out (and he should know ;^), Int can raise miriad exceptions. not just ValueError. IMO as long as the int is the *only* thing happening in the try block and you comment the bare except, then we are ok. Changing the except clause will only introduce bugs. -Casey ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
On Tuesday 10 December 2002 03:14 pm, Guido van Rossum wrote: > > Can we get the same patch without the generic "except:", please? > > > > the last thing I want is a database corruption caused by resizing the > > Edit box... > > Why would this particular except clause cause database corruption? I think Leonard is refering to catching ConflictErrors. However I don't think int() touches the database ;^). > int() happens to raise a bunch of different exceptions, and I think > an unqualified except: clause is okay here (though it needs a > comment). Yup and please reformat the other try: except: if you reformat the first one. > Also note that the unpatched code has an unqualified except: already, > so you can't really blame Dmitry. I say blame Canada! -Casey ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
> Can we get the same patch without the generic "except:", please? > > the last thing I want is a database corruption caused by resizing the > Edit box... Why would this particular except clause cause database corruption? int() happens to raise a bunch of different exceptions, and I think an unqualified except: clause is okay here (though it needs a comment). Also note that the unpatched code has an unqualified except: already, so you can't really blame Dmitry. --Guido van Rossum (home page: http://www.python.org/~guido/) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] BUG: ValueError while changing height of the template edit window
Please submit a collector issue for this http://collector.zope.org/Zope so the patch doesn't get lost. Thanks. -Casey On Tuesday 10 December 2002 10:07 am, Dmitry Vasiliev wrote: > Hi All! > > Zope 2.6: pressing of "Taller" or "Shorter" buttons of the page template > edit window raises ValueError. Patch attached. > > -- > Dmitry Vasiliev (dima at hlabs.spb.ru) > ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
[Zope-dev] BUG: ValueError while changing height of the template edit window
Hi All! Zope 2.6: pressing of "Taller" or "Shorter" buttons of the page template edit window raises ValueError. Patch attached. -- Dmitry Vasiliev (dima at hlabs.spb.ru) Index: ZopePageTemplate.py === RCS file: /cvs-repository/Zope/lib/python/Products/PageTemplates/ZopePageTemplate.py,v retrieving revision 1.44 diff -u -r1.44 ZopePageTemplate.py --- ZopePageTemplate.py 18 Sep 2002 15:12:46 - 1.44 +++ ZopePageTemplate.py 10 Dec 2002 14:33:43 - @@ -155,8 +155,11 @@ cols = min(cols, 100) # Max width 100% cols = "%d%%" % cols # Add percent sign back on else: # Absolute width -try: cols = int(width) -except: cols = max(40, int(dtpref_cols) + szchw.get(width, 0)) +try: +cols = int(width) +except: +cols = max(40, int(dtpref_cols[:-1]) + szchw.get(width, 0)) +cols = "%d%%" % cols # Add percent sign back on try: rows = int(height) except: rows = max(1, int(dtpref_rows) + szchh.get(height, 0))