rexec in jython??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Fink

    rexec in jython??

    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

  • Mark Fink

    #2
    Re: rexec in jython??

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

    Comment

    • Mark Fink

      #3
      Re: rexec in jython??

      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

      Comment

      • Diez B. Roggisch

        #4
        Re: rexec in jython??

        Mark Fink wrote:
        [color=blue]
        > 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[/color]

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

        Diez

        Comment

        • Mark Fink

          #5
          Re: rexec in jython??

          This is the original code section of the library including the
          comments:
          class AutoAdapter(Typ eAdapter):
          """
          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)"

          Comment

          • Diez B. Roggisch

            #6
            Re: rexec in jython??

            Mark Fink wrote:
            [color=blue]
            > This is the original code section of the library including the
            > comments:
            > class AutoAdapter(Typ eAdapter):
            > """
            > 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)"[/color]

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

            Diez

            Comment

            • Mark Fink

              #7
              Re: rexec in jython??

              :-))) 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:[color=blue][color=green][color=darkred]
              >>> 2/-3[/color][/color][/color]
              -1
              Anyway many thanks for your help. Without this would have taken forever.

              Comment

              • Kent Johnson

                #8
                Re: rexec in jython??

                Mark Fink wrote:[color=blue]
                > 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:
                >[color=green][color=darkred]
                >>>>2/-3[/color][/color]
                >
                > -1[/color]

                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

                Comment

                Working...