eval() function changes

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

eval() function changes

#1 Post by jack_norton »

OK it's me again :D
I thought it was my bad coding in Roommates to cause the problems, but I found even in SOTW or other program I still have this issue with eval. In practice, what before was working fine, in the latest Ren'Py crashes. A line like:

Code: Select all

rv = eval(self.item["Superclass"])(self.item["Name"], self.item["ID"], self.item["Type"],tmp_values,tmp_keys,self.item["D-Type"],eval(self.item["Price"]),eval(self.item["Dropable"]),eval(self.item["Unique"]),self.item["Icon"],self.item["Description"],self.level)
was working completely fine, and now instead returns the error:
TypeError: eval() arg 1 must be a string or code object

So I'm wondering why? I mean, if worked fine in the past 4 years (or so since I've been using Ren'Py) why suddenly breaks now ? some changes introduced by newer version of Python? How can I fix this (except for not upgrading)? Thanks
follow me on Image Image Image
computer games

Counter Arts
Miko-Class Veteran
Posts: 649
Joined: Fri Dec 16, 2005 5:21 pm
Completed: Fading Hearts, Infinite Game Works
Projects: Don't Save the World
Organization: Sakura River
Location: Canada
Contact:

Re: eval() function changes

#2 Post by Counter Arts »

Have you thought of throwing around function pointers instead?
Fading Hearts is RELEASED
http://www.sakurariver.ca

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: eval() function changes

#3 Post by jack_norton »

Well, I can do any "workaround" of course. The problem is when something that used to work for years suddenly changes, so I have to search in the code and find a solution for all those cases, and then retest the game to make sure it works as intended. I am thinking to code a "replacement function".
But of course it's not an ideal solution, so I was wondering if I'm missing something, if is a Ren'Py bug, or if there's an easier fix before changing my code in 60+ different places :(
follow me on Image Image Image
computer games

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: eval() function changes

#4 Post by jack_norton »

Haha in the end the solution was super simple:

Code: Select all

    def evalFixed(expression):
        expressionFixed = str(expression)
follow me on Image Image Image
computer games

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: eval() function changes

#5 Post by jack_norton »

Haha in the end the solution was super simple:

Code: Select all

    def evalFixed(expression):
        expressionFixed = str(expression)
        return eval(expressionFixed)
follow me on Image Image Image
computer games

User avatar
PyTom
Ren'Py Creator
Posts: 16094
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: eval() function changes

#6 Post by PyTom »

It would probably be better to do something line:

Code: Select all

def evalFixed(expression):
    if not isinstance(expression, basestring):
        expression = repr(expression)

    return eval(expression)
What's weird is that this should never have worked - eval has always been defined to take a string or code object. I haven't changed it recently, and it was only ever documented as part of Python, where it's defined as:

Code: Select all

eval(...)
    eval(source[, globals[, locals]]) -> value
    
    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.
In general, eval is a bad idea. It has to compile, transform, and execute python code, which is a slow process. This might be part of the reason why Roomates has been having performance problems on mobile.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
jack_norton
Lemma-Class Veteran
Posts: 4084
Joined: Mon Jul 21, 2008 5:41 pm
Completed: Too many! See my homepage
Projects: A lot! See www.winterwolves.com
Tumblr: winterwolvesgames
Contact:

Re: eval() function changes

#7 Post by jack_norton »

Ah OK, but in Roommates the eval is not used a lot honestly :)
But yeah I'll try to use it less in future games! thanks
follow me on Image Image Image
computer games

Post Reply

Who is online

Users browsing this forum: No registered users