473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Does eval has the same features as Perl's?

Hello members,

I want to know does the "eval" in python have the same features as in
Perl (capture errors)?

For example,in perl I can wrote:

$re = eval { 1 / 0 };

Though 1/0 is a fatal error but since it's in "eval" block so the perl
interpreter doesn't get exit.

Thanks again.
Jan 20 '07 #1
4 2749
On Sat, 20 Jan 2007 17:30:24 +0800, Jm lists wrote:
Hello members,

I want to know does the "eval" in python have the same features as in
Perl (capture errors)?

For example,in perl I can wrote:

$re = eval { 1 / 0 };

Though 1/0 is a fatal error but since it's in "eval" block so the perl
interpreter doesn't get exit.
How hard would it be to actually try it?
>>eval("1/0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

It took about three seconds to actually test it.

eval has many security risks -- as a rule, you should never pass strings
you've got from the user to eval, unless you want the user to p0wn your
computer. It is harder than you might think to make eval safe -- you
should seriously consider it an advanced tool, not a basic tool. As a
newbie, you should work under the following rule:

"If I think I need to use eval, I'm probably wrong."

I've been using Python for seven or eight years, and I don't think I've
ever used eval in serious code.

Now, suppose you find yourself wanting to use eval. You've considered the
above rule carefully, and decided that it doesn't apply in this case.
You've been careful to use it only on safe strings, not arbitrary strings
from users. How do you use eval so it captures errors?

try:
eval("1/0")
except ZeroDivisionError: # capture only one error
pass
or something like this:

try:
eval("something or other goes here")
except Exception: # capture any error
pass

--
Steven.

Jan 20 '07 #2
Thank you.I'm just learning Python and want to make something clear to me.:)

2007/1/20, Steven D'Aprano <st***@remove.this.cybersource.com.au>:
On Sat, 20 Jan 2007 17:30:24 +0800, Jm lists wrote:
Hello members,

I want to know does the "eval" in python have the same features as in
Perl (capture errors)?

For example,in perl I can wrote:

$re = eval { 1 / 0 };

Though 1/0 is a fatal error but since it's in "eval" block so the perl
interpreter doesn't get exit.

How hard would it be to actually try it?
>eval("1/0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

It took about three seconds to actually test it.

eval has many security risks -- as a rule, you should never pass strings
you've got from the user to eval, unless you want the user to p0wn your
computer. It is harder than you might think to make eval safe -- you
should seriously consider it an advanced tool, not a basic tool. As a
newbie, you should work under the following rule:

"If I think I need to use eval, I'm probably wrong."

I've been using Python for seven or eight years, and I don't think I've
ever used eval in serious code.

Now, suppose you find yourself wanting to use eval. You've considered the
above rule carefully, and decided that it doesn't apply in this case.
You've been careful to use it only on safe strings, not arbitrary strings
from users. How do you use eval so it captures errors?

try:
eval("1/0")
except ZeroDivisionError: # capture only one error
pass
or something like this:

try:
eval("something or other goes here")
except Exception: # capture any error
pass

--
Steven.

--
http://mail.python.org/mailman/listinfo/python-list
Jan 20 '07 #3

Jm lists wrote:
Hello members,

I want to know does the "eval" in python have the same features as in
Perl (capture errors)?

For example,in perl I can wrote:

$re = eval { 1 / 0 };

Though 1/0 is a fatal error but since it's in "eval" block so the perl
interpreter doesn't get exit.

Thanks again.
Hi Jim,
If your new to Python and coming from Perl then your question above
needs knowledge of Python Exceptions, and the try statement.

if you had variables x and y and wanted to compute x/y but if y was
zero print some message and continue, then you would most likely do
something like:

x = 1
y = 0
try:
z = x / y
except ZeroDivisionError:
print "This leads to a divide by zero error!"
There is more info in the tutorial chapter 8.

- Paddy.

Jan 20 '07 #4
The python editor won't "get exit." It will raise an exception. With
or without an eval, you can catch the exception.

try:
x = 1/0
except ZeroDivisionError:
x = "infinity"
Jm lists wrote:
Hello members,

I want to know does the "eval" in python have the same features as in
Perl (capture errors)?

For example,in perl I can wrote:

$re = eval { 1 / 0 };

Though 1/0 is a fatal error but since it's in "eval" block so the perl
interpreter doesn't get exit.

Thanks again.
Jan 22 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
5
by: mbbx6spp | last post by:
Hi All, I already searched this newsgroup and google groups to see if I could find a Python equivalent to Perl's Template::Extract, but didn't find anything leading to a Python module that had...
1
by: Sean | last post by:
I know "eval" and "if ($@)" is the equivalent of a try/catch in the C programming world. My question is how to basically throw an error in one perl script that can be caught by another? For...
2
by: news frontiernet.net | last post by:
I have key entered and tried to run example 4-6 from Dany Goodmans DYNAMIC HTML book, version one that is on pages 94-96. This is part of my effort to learn JavaScript. I checked each byte and...
0
by: vetolimits | last post by:
Hi everyone, Machine info: Fedora Core 3, MySQL-server-4.1.9, and perl-DBD-MySQL-2.9003-5 When I try to use mysqlhotcopy command to backup database, following error occurs :(& ...
8
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined...
7
by: Todd Cary | last post by:
I inherited an application where the Flyout works in Firefox but not in IE. My JavaScript background is sparse, so I am not sure how to approach the problem. Any suggestions are welcomed! The...
8
by: sonet | last post by:
Many Language have eval function.The C have eval function? How to execute c code in c ? Can i execute c code from a variable(like perl's code ref)? Or i must do this job by using yacc & lex ? All...
0
by: mag | last post by:
I have a make file that executes a perl script that takes PERL as an eval. The script executes the eval() function via an arguement to the script like this: target: script $(ARGUMENT) ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.