<?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>Sufficiently Small &#187; Jython</title>
	<atom:link href="http://www.smallshire.org.uk/sufficientlysmall/tag/jython/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.smallshire.org.uk/sufficientlysmall</link>
	<description>sin(x) = x</description>
	<lastBuildDate>Sun, 11 Apr 2010 19:36:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>String compatibility between Python implementations</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/06/18/string-compatibility-between-python-implementations/</link>
		<comments>http://www.smallshire.org.uk/sufficientlysmall/2009/06/18/string-compatibility-between-python-implementations/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 14:28:51 +0000</pubDate>
		<dc:creator>Robert Smallshire</dc:creator>
				<category><![CDATA[IronPython]]></category>
		<category><![CDATA[Jython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=340</guid>
		<description><![CDATA[Jython and IronPython run on platforms where strings are unicode capable by default. Both implementations have chosen to make str essentially an alias for unicode in Python source code. The bytes type, introduced in PEP358 as part of transition to fully unicode Python 3.0, is unambiguously a sequence of single byte values. We can see [...]]]></description>
			<content:encoded><![CDATA[<p>Jython and IronPython run on platforms where strings are unicode capable by default. Both implementations have chosen to make <code>str</code> essentially an alias for <code>unicode</code> in Python source code. The <code>bytes</code> type, introduced in <a href="http://www.python.org/dev/peps/pep-0358/">PEP358</a> as part of transition to fully unicode Python 3.0, is unambiguously a sequence of single byte values. We can see in the table below that Jython and IronPython are caught between what is on the one hand most practical for interopability with existing code and their host platforms, and on the other hand the Right Thing as delivered by Python 3.0.</p>
<table>
<tr>
<th></th>
<th>Jython 2.5</th>
<th>IronPython 2.6</th>
<th>CPython 2.6</th>
<th>CPython 3.0</th>
</tr>
<tr>
<th>str</th>
<td>multibyte</td>
<td>multibyte</td>
<td>byte</td>
<td>multibyte</td>
</tr>
<tr>
<th>unicode</th>
<td>multibyte</td>
<td>multibyte</td>
<td>multibyte</td>
<td>multibyte</td>
</tr>
<tr>
<th>bytes</th>
<td>byte</td>
<td>byte</td>
<td>byte</td>
<td>byte</td>
</tr>
</table>
<p>It seems clear that if you need to write code that is portable between the different Python implementations you should steer clear <code>str</code> and use <code>bytes</code> and <code>unicode</code> to unambigiously express your intent.</p>
<p>Of course, this is impossible since the Python Standard Library is littered with uses of <code>str</code>. For example, in IronPython <code>pickle.dumps()</code> returns <code>str</code> just like Python 2.6 but the <code>str</code> is actually has multibyte storage.  IronPython hides this well, but the abstraction can leak, resulting in much confusion.  Again Python 3.0 does what is right, and <code>pickle.dumps()</code> returns a <code>bytes</code> instance.</p>
<p>These difficulties are most likely to occur when interfacing with native Java or .NET APIs that expect byte arrays, for example when pickling to database blobs. </p>
<p>In Jython an <code>str</code> instance can be converted to a Java byte array as follows.</p>
<pre class="brush: python;">
&gt;&gt;&gt; import jarray
&gt;&gt;&gt; a = jarray.array(&quot;This is  string&quot;, 'b')
&gt;&gt;&gt; a
array('b', [84, 104, 105, 115, 32, 105, 115, 32, 32, 115, 116, 114, 105, 110, 103])
</pre>
<p>The equivalent in IronPython, as provided by <a href="http://www.voidspace.org.uk/python/weblog/">Michael Foord</a>,  being,</p>
<pre class="brush: python;">
&gt;&gt;&gt; from System import Array, Byte
&gt;&gt;&gt; a = Array[Byte](tuple(Byte(ord(c)) for c in &quot;This is a string&quot;))
&gt;&gt;&gt; a
Array[Byte]((&lt;System.Byte object at 0x000000000000002B [84]&gt;, &lt;System.Byte object at 0x000000000000002C [104]&gt;, &lt;System.Byte object at 0x000000000000002D [105]&gt;, &lt;System.Byte object at 0x000000000000002E [115]&gt;, &lt;System.Byte object at 0x000000000000002F [32]&gt;, &lt;System.Byte object at 0x0000000000000030 [105]&gt;, &lt;System.Byte object at 0x0000000000000031 [115]&gt;, &lt;System.Byte object at 0x0000000000000032 [32]&gt;, &lt;System.Byte object at 0x0000000000000033 [97]&gt;, &lt;System.Byte object at 0x0000000000000034 [32]&gt;, &lt;System.Byte object at 0x0000000000000035 [115]&gt;, &lt;System.Byte object at 0x0000000000000036 [116]&gt;, &lt;System.Byte object at 0x0000000000000037 [114]&gt;, &lt;System.Byte object at 0x0000000000000038 [105]&gt;, &lt;System.Byte object at 0x0000000000000039 [110]&gt;, &lt;System.Byte object at 0x000000000000003A [103]&gt;))
</pre>
<p>Going back we can use identical code in IronPython and Jython.</p>
<pre class="brush: python;">
&gt;&gt;&gt; s = ''.join(chr(c) for c in a)
&gt;&gt;&gt; s
'This is a string'
</pre>
<hr/>Copyright &copy; 2010 <strong><a href="http://www.smallshire.org.uk/sufficientlysmall">Sufficiently Small</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal@smallshire.org.uk so we can take legal action immediately.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://www.smallshire.org.uk/sufficientlysmall/2009/06/18/string-compatibility-between-python-implementations/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
