0

When I run Parent.py which calls the subprocess child.exe the following error occurs

  File "child.exe", line 1
SyntaxError: Non-UTF-8 code starting with '\x90' in file child.exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

If I run the Parent.py with child.py, the execution successfully returns 'Hello from parent.' from Eclipse.

If I run Parent.py from the shipped IDLE nothing is returned in the case of both child.exe and child.py

I have read the http://python.org/dev/peps/pep-0263/ documentation and understood it, possibly missunderstood it to mean I should add the suggested comments which I have tried adding to child.exe ... they did not work.

Parent.py

import os
import subprocess
import sys

if sys.platform == "win32":
    import msvcrt
    import _subprocess
else:
    import fcntl

# Create pipe for communication
pipeout, pipein = os.pipe()

# Prepare to pass to child process
if sys.platform == "win32":
    curproc = _subprocess.GetCurrentProcess()
    pipeouth = msvcrt.get_osfhandle(pipeout)
    pipeoutih = _subprocess.DuplicateHandle(curproc, pipeouth, curproc, 0, 1,
            _subprocess.DUPLICATE_SAME_ACCESS)

    pipearg = str(int(pipeoutih))  
else:
    pipearg = str(pipeout)

    # Must close pipe input if child will block waiting for end
    # Can also be closed in a preexec_fn passed to subprocess.Popen
    fcntl.fcntl(pipein, fcntl.F_SETFD, fcntl.FD_CLOEXEC)

# Start child with argument indicating which FD/FH to read from
subproc = subprocess.Popen(['python', 'child.exe', pipearg], close_fds=False)

# Close read end of pipe in parent
os.close(pipeout)
if sys.platform == "win32":
    pipeoutih.Close()

# Write to child (could be done with os.write, without os.fdopen)
pipefh = os.fdopen(pipein, 'w')
pipefh.write("Hello from parent.")
pipefh.close()

# Wait for the child to finish
subproc.wait()



Child.exe (frozen with cx_freeze)

import os, sys


if sys.platform == "win32":
    import msvcrt

# Get file descriptor from argument
pipearg = int(sys.argv[1])
if sys.platform == "win32":
    pipeoutfd = msvcrt.open_osfhandle(pipearg, 0)
else:
    pipeoutfd = pipearg

# Read from pipe
# Note:  Could be done with os.read/os.close directly, instead of os.fdopen
pipeout = os.fdopen(pipeoutfd, 'r')
print(pipeout.read())
pipeout.close()
2
  • 1
    Whats the encoding from your eclipse editor? it doesn't seem to be either ascii, nor utf-8. It also doenst' seem to be ut-16 because of the missing Byte Order Mark(BOM). Could you look up your standrad editor encoding ? Commented Dec 27, 2011 at 10:22
  • I'm not 100% I'm looking the the right place, but if i go Edit -> Set Encoding - > It is set to Default (inherited from container: Cp1252) It also has an option to select other types such as UTF-8 Commented Dec 27, 2011 at 10:27

1 Answer 1

2
subprocess.Popen(['python', 'child.exe', pipearg], ...

The problem is that you are trying to make python read a binary file, if child.exe is a normal windows executable. The error raises because the bytes of the binary files are out of the standard ascii - utf8 standard so the interpreter can't read it.

Maybe what you want is to just execute the child.exe just remove the python line from it:

subprocess.Popen(['child.exe', pipearg], ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.