'psyco' problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulo da Silva

    #1

    'psyco' problem

    When running the following program:

    #! /bin/env python
    # -*- coding: iso-8859-15 -*-

    import psyco
    psyco.full()

    def main():
    n=eval("123+456 ")
    print n

    if __name__ == "__main__":
    main()


    I got:
    ../tp.py:7: warning: eval()/execfile() cannot see the locals in functions
    bound by Psyco; consider using eval() in its two- or three-arguments form
    def main():
    579

    What does this mean? Is there anything wrong?

    Thank you.
  • Andrew Dalke

    #2
    Re: 'psyco' problem

    Paulo da Silva wrote:[color=blue]
    > I got:
    > ../tp.py:7: warning: eval()/execfile() cannot see the locals in functions
    > bound by Psyco; consider using eval() in its two- or three-arguments form[/color]

    In the following

    def main():
    n=eval("123+456 ")
    print n

    it appears that Psycho can't peer into the eval string
    to figure out what's used. It doesn't know, for example,
    if you're trying to do something like this

    import math
    def main():
    n = 0
    n=eval("123+n*m ath.pi")
    print n

    To handle it correctly it would need to understand
    Python's full scopes, which is hard.

    Instead, Psyco is asking you for some help. You
    need to tell it what variables might be used inside
    the eval string by passing the list of possible
    values via the optional 2nd and 3rd arguments to
    eval. See the docs for details. Here's how to make
    what you want work

    def main():
    n=eval("123+456 ", {})
    print n

    WARNING: this is based solely on reading the error
    message. My primary machine is a Mac and Psyco
    doesn't run on it so I cannot test my answer.

    Andrew
    dalke@dalkescie ntific.com

    Comment

    • Hannu Kankaanp??

      #3
      Re: 'psyco' problem

      Paulo da Silva <psXdaXsilva@es otericaX.ptX> wrote in message news:<109737192 0.617950@mystiq ue.esoterica.pt >...[color=blue]
      > When running the following program:
      >
      > #! /bin/env python
      > # -*- coding: iso-8859-15 -*-
      >
      > import psyco
      > psyco.full()
      >
      > def main():
      > n=eval("123+456 ")
      > print n
      >
      > if __name__ == "__main__":
      > main()
      >
      >
      > I got:
      > ./tp.py:7: warning: eval()/execfile() cannot see the locals in functions
      > bound by Psyco; consider using eval() in its two- or three-arguments form
      > def main():
      > 579
      >
      > What does this mean? Is there anything wrong?
      >
      > Thank you.[/color]

      It means you can't do this if main() is bound by Psyco:

      def main():
      x=5
      print eval("123+x")

      because as the warning message says, Psycoed eval() cannot see the
      locals (x here). And it suggest that you should consider using
      eval() in its two- or three-arguments form, which is described
      in the Python Library Reference. So with Psyco, you might use

      y = 3
      def main():
      x=5
      print eval("y+x", globals(), {'x':x})

      But in your case, it of course doesn't matter because in
      the expression 123+456 you aren't using any locals.

      Comment

      Working...