rexec in jython??

M

Mark Fink

Hi there,
I at the moment port a library from Python to Jython (at lease I try to
do so :))). The library uses the Rexec to form a type adapter to cast
parameters given as text into the according Jython type. I learned
rexec is not available in Jython. Is something that is commonly used in
Jython for such tasks available?
In general: how do I find such information by myself?
Best Regards,
Mark
 
M

Mark Fink

sorry for the double post!
It is the r_eval() functionality of the rexec module I am looking
forward to replace
 
M

Mark Fink

this is really funny...
I tried to use eval(String) as an replacement. It works now but the
calculation results from my tests are not as expected: 2 + 3 = 23 !!! 2
- 3 = 2-3...
I have the feeling that there is a really easy solution for this.
Unfortunately I have no enough experience
 
D

Diez B. Roggisch

Mark said:
this is really funny...
I tried to use eval(String) as an replacement. It works now but the
calculation results from my tests are not as expected: 2 + 3 = 23 !!! 2
- 3 = 2-3...
I have the feeling that there is a really easy solution for this.
Unfortunately I have no enough experience

How about showing code? Otherwise we all have to use our crystal balls, and
these thingies tend to be not so precise...

Diez
 
M

Mark Fink

This is the original code section of the library including the
comments:
class AutoAdapter(TypeAdapter):
"""
This adapter returns a type based on the format of the table cell
contents, following python's rules for literals (plus a few
others).
We could fall back on this when we don't know what type is
expected.
I am lazy and so use rexec to do the work.

#Note: The RExec class can prevent code from performing unsafe
#operations like reading or writing disk files, or using TCP/IP
#sockets. However, it does not protect against code using
#extremely large amounts of memory or processor time.
"""
r_eval = RExec().r_eval
def parse(self,s):
if s.strip().lower() == 'true':
return 1
elif s.strip().lower() == 'false':
return 0
else:
try:
return self.r_eval(s)
except SystemExit:
return None
except:
return s

I completely removed the import of rexec and replaced "return
self.r_eval(s)" with "return self.eval(s)"
 
D

Diez B. Roggisch

Mark said:
This is the original code section of the library including the
comments:
class AutoAdapter(TypeAdapter):
"""
This adapter returns a type based on the format of the table cell
contents, following python's rules for literals (plus a few
others).
We could fall back on this when we don't know what type is
expected.
I am lazy and so use rexec to do the work.

#Note: The RExec class can prevent code from performing unsafe
#operations like reading or writing disk files, or using TCP/IP
#sockets. However, it does not protect against code using
#extremely large amounts of memory or processor time.
"""
r_eval = RExec().r_eval
def parse(self,s):
if s.strip().lower() == 'true':
return 1
elif s.strip().lower() == 'false':
return 0
else:
try:
return self.r_eval(s)
except SystemExit:
return None
except:
return s

I completely removed the import of rexec and replaced "return
self.r_eval(s)" with "return self.eval(s)"

And what does self.eval look like? Crystal ball still not working....

Diez
 
M

Mark Fink

:))) it works!
I replaced it to "return eval(s)" and the results look extremely well
guess I should get one of this crystal balls myself.

one minor issue is still left:
correct me if I am wrong: result of 2/-3 is 0 (at least this is how it
is defined in the testcase)
In Jython 2.1.3:-1
Anyway many thanks for your help. Without this would have taken forever.
 
K

Kent Johnson

Mark said:
one minor issue is still left:
correct me if I am wrong: result of 2/-3 is 0 (at least this is how it
is defined in the testcase)
In Jython 2.1.3:


-1

CPython gives the same result and the language reference says, "Plain or
long integer division yields an integer of the same type; the result is
that of mathematical division with the `floor' function applied to the
result." so this seems to be intentional.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top