Input() in Python3

Benjamin Kaplan benjamin.kaplan at case.edu
Fri Apr 22 12:01:07 EDT 2011


On Apr 22, 2011 10:12 AM, "Mel" <mwilson at the-wire.com> wrote:
>
> Westley Martínez wrote:
> > On Fri, Apr 22, 2011 at 04:49:19PM +1000, Chris Angelico wrote:
>
> >> Uhhhh.... NO. NO NO NO. What if someone enters "os.exit()" as their
> >> number? You shouldn't eval() unchecked user input!
> >>
> >> Chris Angelico
> >
> > Right, there's no way to check you're getting a number, however using:
> >
> > a = int(input('enter a number > ')) # use float() for floats
> >
> > will raise an exception if it can't convert the string.
>
> But sys.exit() doesn't return a string.  My fave is
>
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import sys
> >>> a = int (input ('enter a number >'))
> enter a number >sys.setrecursionlimit(1)
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Error in sys.excepthook:
> RuntimeError: maximum recursion depth exceeded
>
> Original exception was:
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> RuntimeError: maximum recursion depth exceeded while calling a Python
object
> >>> int (0)
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Exception RuntimeError: 'maximum recursion depth exceeded while calling a
> Python object' in <type 'exceptions.RuntimeError'> ignored
> Error in sys.excepthook:
> RuntimeError: maximum recursion depth exceeded
>
> Original exception was:
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> RuntimeError: maximum recursion depth exceeded while calling a Python
object
> >>>
>
>
>        Mel.
>

We're talking about python 3, not python 2. If you're using python 2, the
equivalent code would be int(raw_input()) and that isn't vulnerable to this
sort of thing.
> --
> http://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110422/af1c5f36/attachment-0001.html>


More information about the Python-list mailing list