Script incompatibility for Python 2.x and Python 3.x

cmouse

I cannot understand what happens with python3 that prevents this from working, when I try with python3, it just hungs on line 11.

import io,re,unittest,os,json,sys
from subprocess import PIPE, STDOUT, Popen
sub = Popen(["/usr/bin/python3.4", "test2.py", "pipe"], stdin=PIPE, stdout=PIPE, stderr=sys.stderr, close_fds=True, shell=False)
(writer,reader) = (sub.stdin, sub.stdout)
writer.write("HELO\t1\n".encode("utf-8"))
writer.flush()
sub.poll()
line = reader.readline().decode("utf-8")
assert(re.match("^OK\t", line))
writer.write("Q\ttest.com\tIN\tSOA\t-1\t127.0.0.1\n".encode("utf-8"))
line = reader.readline().decode("utf-8")
assert(re.match("^DATA\ttest.com\tIN\tSOA\t300\t-1\tsns.dns.icann.org. noc.dns.icann.org. 2013073082 7200 3600 1209600 3600", line))
sub.stdout.close()
sub.stdin.close()
sub.kill()
sub.wait()

Here's the test2.py file that the above code calls:

import sys
sys.stderr.write(sys.stdin.readline())
sys.stdout.write("OK\tversion foo bar hello\n")
sys.stdout.flush()
sys.stderr.write(sys.stdin.readline())
sys.stdout.write("DATA\ttest.com\tIN\tSOA\t300\t-1\tsns.dns.icann.org. noc.dns.icann.org. 2013073082 7200 3600 1209600 3600")
sys.stdout.flush()
Jonathan Eunice

On my system, I reproduced your problem and fixed it by adding a writer.flush() after your second writer.write() call. E.g.:

import io,re,unittest,os,json,sys
from subprocess import PIPE, STDOUT, Popen
sub = Popen(["/usr/bin/python3.4", "test2.py", "pipe"], stdin=PIPE, stdout=PIPE, stderr=sys.stderr, close_fds=True, shell=False)
(writer,reader) = (sub.stdin, sub.stdout)
writer.write("HELO\t1\n".encode("utf-8"))
writer.flush()
sub.poll()
line = reader.readline().decode("utf-8")
assert(re.match("^OK\t", line))
writer.write("Q\ttest.com\tIN\tSOA\t-1\t127.0.0.1\n".encode("utf-8"))
writer.flush() # <<< INSERTED FLUSH <<<
line = reader.readline().decode("utf-8")
assert(re.match("^DATA\ttest.com\tIN\tSOA\t300\t-1\tsns.dns.icann.org. noc.dns.icann.org. 2013073082 7200 3600 1209600 3600", line))
sub.stdout.close()
sub.stdin.close()
sub.kill()
sub.wait()
sub.wait()

It now executes properly if the main script is run in Python 2 or Python 3 (tested: 2.7 and 3.4)

I say "on my system" because this sort of interleaved reader-writer coordination across text pipes is intrinsically fragile. When it works great, it's great! But very small variations in how the system, system libraries, language implementations, etc. handle I/O can cause you all manner of grief.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: Eval with undefined variables (2*x+x = 3*x)

From Java

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Geocoding Strategies - Python 2.x to Python 3.x

From Dev

Deal with buffer in Python 2.x and 3.x

From Dev

Regex unicode in python 2.x vs 3.x

From Dev

Deal with buffer in Python 2.x and 3.x

From Dev

Accessing Python2.x and 3.x in windows

From Dev

python 3.x execute script with linked scripts ubuntu

From Java

Using both Python 2.x and Python 3.x in IPython Notebook

From Dev

How to parse Python 2.x with Python 3.x ast module?

From Dev

Can't import turtle module in Python 2.x and Python 3.x

From Dev

string is not same in Python3x as for Python2.x but Still it is working

From Dev

Generator evaluation difference between Python 2.x and Python 3.x

From Dev

random.randint shows different output in Python 2.x and Python 3.x with same seed

From Dev

Change "Edit with IDLE" default to python 3.X instead of python 2.X on windows

From Dev

How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

From Dev

PyYAML with Python 3.x

From Dev

Pyinstaller with Python3.x

From Dev

Upgrading to python 3.x

From Dev

PyYAML with Python 3.x

From Dev

Summing a 2d array in Python 3.x

From Dev

Is it possible to create GTK+2.x applications using Python 3?

From Dev

Run Python Script at OS X Startup

From Dev

Run Python Script every X seconds

From Dev

Function with args and default kwargs for Python 2.x and 3.x

From Dev

How can I get 2.x-like sorting behaviour in Python 3.x?

From Dev

Valid syntax in both Python 2.x and 3.x for raising exception?

From Dev

Difference in multithreading overhead between python 2.x and 3.x

Related Related

  1. 1

    Python: Eval with undefined variables (2*x+x = 3*x)

  2. 2

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  3. 3

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  4. 4

    Geocoding Strategies - Python 2.x to Python 3.x

  5. 5

    Deal with buffer in Python 2.x and 3.x

  6. 6

    Regex unicode in python 2.x vs 3.x

  7. 7

    Deal with buffer in Python 2.x and 3.x

  8. 8

    Accessing Python2.x and 3.x in windows

  9. 9

    python 3.x execute script with linked scripts ubuntu

  10. 10

    Using both Python 2.x and Python 3.x in IPython Notebook

  11. 11

    How to parse Python 2.x with Python 3.x ast module?

  12. 12

    Can't import turtle module in Python 2.x and Python 3.x

  13. 13

    string is not same in Python3x as for Python2.x but Still it is working

  14. 14

    Generator evaluation difference between Python 2.x and Python 3.x

  15. 15

    random.randint shows different output in Python 2.x and Python 3.x with same seed

  16. 16

    Change "Edit with IDLE" default to python 3.X instead of python 2.X on windows

  17. 17

    How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

  18. 18

    PyYAML with Python 3.x

  19. 19

    Pyinstaller with Python3.x

  20. 20

    Upgrading to python 3.x

  21. 21

    PyYAML with Python 3.x

  22. 22

    Summing a 2d array in Python 3.x

  23. 23

    Is it possible to create GTK+2.x applications using Python 3?

  24. 24

    Run Python Script at OS X Startup

  25. 25

    Run Python Script every X seconds

  26. 26

    Function with args and default kwargs for Python 2.x and 3.x

  27. 27

    How can I get 2.x-like sorting behaviour in Python 3.x?

  28. 28

    Valid syntax in both Python 2.x and 3.x for raising exception?

  29. 29

    Difference in multithreading overhead between python 2.x and 3.x

HotTag

Archive