0

We have two python scripts, which need to communicate over stdin/out (on Windows). Sadly they both have to be in different Python versions. The snippets are:

Source (Python 3):

sys.stderr.write("LEN1: %s\n" % len(source_file.read()))
subprocess.check_call(["C:\\python2.exe", "-u", "-c",
"""
import foo
foo.to_json()
"""], stdin=source_file, stdout=json_file, stderr=sys.stderr, env=os.environ)

Target (Python 2):

def to_json():
  import msvcrt
  msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
  input_message = sys.stdin.read()
  sys.stderr.write("LEN2: %s\n" % len(input_message))

When I execute the scripts, I get:

LEN2: 0
LEN1: 37165

It seems like I am doing something fundamentally wrong, but cannot really figure out, what exactly. Could anyone try to help me debug, where I am going wrong.

2
  • Have you tried using a Popen with pipes and talking to it with communicate? docs.python.org/2/library/subprocess.html#subprocess.Popen Commented May 26, 2015 at 16:47
  • The setmode call in to_json is redundant since the -u argument sets stdin to binary mode. Passing env=os.environ is probably redundant, unless os.environ has somehow gotten out of sync with the process environment. Commented May 26, 2015 at 17:10

1 Answer 1

3

After the first source_file.read() to determine the file's length, the file pointer is at the end of the file. By the time you've passed the stream to check_call(), you've exhausted it, and further reads will produce the empty string. This can be worked around in two ways:

A. Read the file into memory before calculating its length.

B. Before check_call(), rewind the file object to the beginning of the file with source_file.seek(0).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Then seems like all data is received fine.

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.