Code no longer works after going from Python 2.7 to 3.3

0

I started using Python 3.3 instead of 2.7 and there is a problem with some lines of code that I use to restart or close the program.

def restart_program(): 
    python = sys.executable
    os.execl(python, python, * sys.argv)

if __name__ == "__main__":
    answer = input("Restart or close program    ")
    if answer.strip() in "Restart restart".split():
        restart_program()

When I use this I get the following error:

Fatal Python error: PyEval_RestoreThread: NULL tstate
Current thread 0x00002030:
File "C:\Python33\lib\os.py", line 531 in execl
File "C:\Users\user\desktop\filename", line 66 in restart_program

Line 66 is

os.execl(python, python, * sys.argv)

I have no idea how to solve this and I really hope that someone has a solution.

If I write something really easy like the following code, then it works with no problems.

import math
import os
import sys

a = input ("enter a")
a = float (a)
b = 12

x = a+b
print (x)

def restart_program(): 
    python = sys.executable
    os.execl(python, python, * sys.argv)

if __name__ == "__main__":
    answer = input("Restart or close the program    ")
    if answer.strip() in ["Restart", "restart"]:
        restart_program()

I guess I could also add my entire code here.

http://pastebin.com/fM8s0iiw

python
asked on Stack Overflow Dec 13, 2012 by user1901162 • edited Dec 13, 2012 by user1901162

1 Answer

1

Try commenting out the Popen call and see if things clean up. That's really not a very good way to open a file though, as it will only work on Windows, and only if you have a file association set for png files.

I bet that Python 3 is less forgiving than 2 about leaving around orphaned stream handlers or something, so blowing away the parent process is causing the child(ren) to choke. If commenting out the Popen gets rid of your problem, try tracking all of the children you create and explicitly killing them before the execl call.

answered on Stack Overflow Dec 13, 2012 by Silas Ray • edited Dec 13, 2012 by Silas Ray

User contributions licensed under CC BY-SA 3.0