<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Valentin's Lab &#187; windows</title>
	<atom:link href="https://vaab.blog.kal.fr/tag/windows/feed/" rel="self" type="application/rss+xml" />
	<link>https://vaab.blog.kal.fr</link>
	<description>Ratiocination of an opensource techie</description>
	<lastBuildDate>Thu, 15 Nov 2018 08:04:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F&amp;language=en_US&amp;category=text&amp;title=Valentin%27s+Lab&amp;description=Ratiocination+of+an+opensource+techie&amp;tags=blog" type="text/html" />
	<item>
		<title>Fixing Windows Python 2.7 unicode issue with subprocess&#8217;s Popen.</title>
		<link>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/</link>
		<comments>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/#comments</comments>
		<pubDate>Thu, 16 Mar 2017 10:22:49 +0000</pubDate>
		<dc:creator><![CDATA[vaab]]></dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://vaab.blog.kal.fr/?p=591</guid>
		<description><![CDATA[TL;DR Fixing Windows Python 2.7 unicode issue with subprocess.Popen being unable to properly send command lines to the system. Here is the python code fixing unicode issue Details What Python 2.7 is plagued by bugs that won't be fixed. One &#8230;<p class="read-more"><a href="https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/">Read more &#187;</a></p>]]></description>
				<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<div class="section" id="tl-dr">
<h3>TL;DR</h3>
<p>Fixing <a class="reference external" href="http://bugs.python.org/issue19264">Windows Python 2.7 unicode issue</a> with <tt class="docutils literal">subprocess.Popen</tt> being unable to properly
send command lines to the system.</p>
<p>Here is <a class="reference external" href="https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9">the python code fixing unicode issue</a></p>
</div>
<div class="section" id="details">
<h3>Details</h3>
<div class="section" id="what">
<h4>What</h4>
<p>Python 2.7 is plagued by bugs that won't be fixed.</p>
<p>One of them is that <strong>you can't use unicode chars on
sub process call command line</strong> via <tt class="docutils literal"><span class="pre">subprocess.Popen(..)</span></tt>
in windows platform.</p>
<p>This code fixes <tt class="docutils literal"><span class="pre">Popen(..)</span></tt> in python 2.7 under windows. This allows your
code simply work on python 2.7 with little changes.</p>
</div>
<div class="section" id="how">
<h4>How</h4>
<p>The following code leverage <tt class="docutils literal">ctypes</tt> to call the <tt class="docutils literal"><span class="pre">CreateProcessW(..)</span></tt>
function of the windows C API. This function should have been used
in cPython 2.7 but wasn't. This is the core reason why Python 2.7
on windows does not support unicode command line.</p>
<p>This is how Python 3.0+ works.</p>
</div>
<div class="section" id="the-code">
<h4>The code</h4>
<p><strong>This code was not thoroughly tested</strong> and I'm posting it on a gist so I
can update it and people can comment if they run into any issues.</p>
<p><strong>So here is</strong> <a class="reference external" href="https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9">the python code fixing unicode issue</a>.</p>
</div>
<div class="section" id="a-test">
<h4>A test ?</h4>
<p>As a test, we'll create a small <tt class="docutils literal">test.py</tt> python file
that you can call with the new <tt class="docutils literal"><span class="pre">subprocess.Popen(..)</span></tt> provided
in the Gist.</p>
<p>To be complete, we'll need another fix of python 2.7 unicode support: <a class="reference external" href="http://code.activestate.com/recipes/572200/">recipe to read the arguments in unicode</a>.</p>
<p>Here's the full code for the testing:</p>
<ul class="simple">
<li>with <tt class="docutils literal">subprocess_fix</tt> module being the code in the Gist. Correctly
sending the full unicode command line to the system. This is thus used on the calling side.</li>
<li>And <tt class="docutils literal">commandline_fix</tt> module being the recipe to
decode the current programs unicode command line with a final
added <tt class="docutils literal">sys.argv = win32_unicode_argv()</tt> statement. This is thus used on the called side.</li>
</ul>
<p>The caller side (named <tt class="docutils literal">test.py</tt>):</p>
<pre class="literal-block">
# -*- coding: utf-8 -*-

from subprocess import PIPE
from subprocess_fix import Popen

def indent(text, chars=&quot; &quot;, first=None):
    if first:
        first_line = text.split(&quot;\n&quot;)[0]
        rest = '\n'.join(text.split(&quot;\n&quot;)[1:])
        return '\n'.join([(first + first_line).rstrip(),
                          indent(rest, chars=chars)])
    return '\n'.join([(chars + line).rstrip()
                      for line in text.split('\n')])

p = Popen(u&quot;python reading.py ć&quot;, shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()

if p.returncode != 0:
    print(&quot;errlvl: %s&quot; % p.returncode)

if err:
    print(&quot;stderr:\n%s&quot; % indent(err, &quot; | &quot;))
if out:
    print(&quot;stdout:\n%s&quot; % indent(out, &quot; | &quot;))
</pre>
<p>The called side (named <tt class="docutils literal">reading.py</tt>):</p>
<pre class="literal-block">
# -*- coding: utf-8 -*-

import sys

import commandline_fix

print &quot;command line received (repr): %r&quot; % (sys.argv, )
print &quot;command line received (str): %s&quot; % (u&quot; &quot;.join(sys.argv), )
</pre>
<p><tt class="docutils literal">test.py</tt> uses the fixed <tt class="docutils literal"><span class="pre">Popen(..)</span></tt> to call <tt class="docutils literal">reading.py</tt> in a subprocess
and it specifies a unicode character in the command line arguments. <tt class="docutils literal">reading.py</tt> will
simply read the unicode command line and print it to the console.</p>
<p>To run the test:</p>
<pre class="literal-block">
python test.py
</pre>
<p>Notice that depending on your active charset (you can check with <tt class="docutils literal">chcp</tt>),
the console might fail to display the character correctly. You might need
then to do a <tt class="docutils literal">chcp 65001</tt>.</p>
</div>
</div>
</div>
 <p><a href="https://vaab.blog.kal.fr/?flattrss_redirect&amp;id=591&amp;md5=77d3033e88bd60e262aba556bd682af8" title="Flattr" target="_blank"><img src="https://vaab.blog.kal.fr/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>https://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<atom:link rel="payment" title="Flattr this!" href="https://flattr.com/submit/auto?user_id=vaab&amp;popout=1&amp;url=https%3A%2F%2Fvaab.blog.kal.fr%2F2017%2F03%2F16%2Ffixing-windows-python-2-7-unicode-issue-with-subprocesss-popen%2F&amp;language=en_GB&amp;category=text&amp;title=Fixing+Windows+Python+2.7+unicode+issue+with+subprocess%26%238217%3Bs+Popen.&amp;description=TL%3BDR+Fixing+Windows+Python+2.7+unicode+issue+with+subprocess.Popen+being+unable+to+properly+send+command+lines+to+the+system.+Here+is+the+python+code+fixing+unicode+issue+Details+What+Python...&amp;tags=ctypes%2Cpython%2Cwindows%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>
