Re: Possible bug with TypeError in Python

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Aaron Konstam wrote:
On Wed, 2008-01-30 at 19:34 +0530, Anoop Chandran wrote:
On Jan 30, 2008 7:28 PM, Mark C. Allman <mcallman@xxxxxxxxxxxx> wrote:
On Wed, 2008-01-30 at 07:40 -0600, Aaron Konstam wrote:
        > The following seems like an bug in python in both f7 and f8
        but I would
        > like input before I post a bugzilla. It seems the exception
        handler
        > cannot trap the TypeError in python.
        > For example:
        > def plus(a,b):
        >       try:
        >               return(a+b)
        >       except TypeError:
        >               return None
        >
        > If we define plus as above and call it with: plus(3,) we
        should get
        > nothing returned. Instead we get:
        > Traceback (most recent call last):
        >   File "./calculator", line 47, in <module>
        >     exec("register=op[tokens[0]](register)")
        >   File "<string>", line 1, in <module>
        > TypeError: plus() takes exactly 2 arguments (1 given)
        >
        > This seems like a bug. An ideas out there?
        > --

        >
If I remember right, the exception is occurring before the
        actual call
        into "plus()."  Try:
           plus(3,"My String");
        and you should see the TypeError.


try:
  a = plus(3)
except TypeError:
  a = 0
print a

Output should be 0.

You are both correct but it still seems like a bug. Until you try to
execute plus the system does not know that plus requires 2 arguments.
But nevertheless a TypeError is generated  so the exception handler
should be triggered.
The exception is generated while calling plus (as said before) so before your try/except. So its thrown right back at you and not caught.
But you have given me further insight into the problem so I thank you
all for that insight.
The question is how do I do what I want to do; that is, check that the number
 of arguments to plus are correct? In the program in which I am using this
construction  the function executed and the arguments are generated dynamically
so doing this checking is necessary.
Using default values might do the trick for you.


>>> def plus(a=None, b=None):
...        try:
...             return a+b
...        except:
...             return None
...
>>> print plus(3, )
None
>>> print plus(3)
None
>>> print plus(3,2)
5

Good luck
Daniel



--
=======================================================================
Whether weary or unweary, O man, do not rest, Do not cease your
single-handed struggle. Go on, do not rest. -- An old Gujarati hymn
=======================================================================
Aaron Konstam telephone: (210) 656-0355 e-mail: akonstam@xxxxxxxxxxxxx


--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
[Index of Archives]     [Older Fedora Users]     [Fedora Announce]     [Fedora Package Announce]     [EPEL Announce]     [Fedora Magazine]     [Fedora News]     [Fedora Summer Coding]     [Fedora Laptop]     [Fedora Cloud]     [Fedora Advisory Board]     [Fedora Education]     [Fedora Security]     [Fedora Scitech]     [Fedora Robotics]     [Fedora Maintainers]     [Fedora Infrastructure]     [Fedora Websites]     [Anaconda Devel]     [Fedora Devel Java]     [Fedora Legacy]     [Fedora Desktop]     [Fedora Fonts]     [ATA RAID]     [Fedora Marketing]     [Fedora Management Tools]     [Fedora Mentors]     [SSH]     [Fedora Package Review]     [Fedora R Devel]     [Fedora PHP Devel]     [Kickstart]     [Fedora Music]     [Fedora Packaging]     [Centos]     [Fedora SELinux]     [Fedora Legal]     [Fedora Kernel]     [Fedora OCaml]     [Coolkey]     [Virtualization Tools]     [ET Management Tools]     [Yum Users]     [Tux]     [Yosemite News]     [Gnome Users]     [KDE Users]     [Fedora Art]     [Fedora Docs]     [Asterisk PBX]     [Fedora Sparc]     [Fedora Universal Network Connector]     [Libvirt Users]     [Fedora ARM]

  Powered by Linux