title: (unicode error) 'unicodeescape' codec can't decode bytes - string with '\u'
body: <p>Writing my code for Python 2.6, but with Python 3 in mind, I thought it was a good idea to put</p>
  
  <pre><code>from __future__ import unicode_literals
  </code></pre>
  
  <p>at the top of some modules. In other words, I am asking for troubles (to avoid them in the future), but I might be missing some important knowledge here. I want to be able to pass a string representing a filepath and instantiate an object as simple as </p>
  
  <p><code>MyObject('H:\unittests')</code></p>
  
  <p>In <b>Python 2.6</b>, this works just fine, no need to use double backslashes or a raw string, even for a directory starting with <code>'\u..'</code>, which is exactly what I want. In the <code>__init__</code> method I make sure all single <code>\</code> occurences are interpreted as '<code>\\</code>', including those before special characters as in <code>\a</code>, <code>\b</code>, <code>\f</code>,<code>\n</code>, <code>\r</code>, <code>\t</code> and <code>\v</code> (only <code>\x</code> remains a problem). Also decoding the given string into unicode using (local) encoding works as expected.</p>
  
  <p>Preparing for <b>Python 3.x</b>, simulating my actual problem in an editor (starting with a clean console in Python 2.6), the following happens:</p>
  
  <pre><code>&gt;&gt;&gt; '\u'
  '\\u'
  &gt;&gt;&gt; r'\u'
  '\\u'
  </code></pre>
  
  <p>(OK until here: <code>'\u'</code> is encoded by the console using the local encoding)   </p>
  
  <pre><code>&gt;&gt;&gt; from __future__ import unicode_literals
  &gt;&gt;&gt; '\u'
  SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: end of string in escape sequence
  </code></pre>
  
  <p>In other words, the (unicode) string is not interpreted as unicode at all, nor does it get decoded automatically with the local encoding. Even so for a raw string:</p>
  
  <pre><code>&gt;&gt;&gt; r'\u'
  SyntaxError: (unicode error) 'rawunicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX
  </code></pre>
  
  <p>same for  <code>u'\u'</code>:</p>
  
  <pre><code>&gt;&gt;&gt; u'\u'
  SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: end of string in escape sequence
  </code></pre>
  
  <p>Also, I would expect <code>isinstance(str(''), unicode)</code> to return <code>True</code> (which it does not), because importing unicode_literals should make all string-types unicode. <b>(edit:)</b> Because <a href="http://diveintopython3.org/strings.html#divingin" rel="noreferrer">in Python 3, all strings are sequences of Unicode characters</a>, I would expect <code>str(''))</code> to return such a unicode-string, and <code>type(str(''))</code> to be both  <code>&lt;type 'unicode'&gt;</code>, and  <code>&lt;type 'str'&gt;</code> (because all strings are unicode) but also realise that <code>&lt;type 'unicode'&gt; is not &lt;type 'str'&gt;</code>. Confusion all around...</p>
  
  <p><b>Questions</b></p>
  
  <ul>
  <li>how can I best pass strings containing '<code>\u</code>'? (without writing '<code>\\u</code>')</li>
  <li>does <code>from __future__ import unicode_literals</code> really implement all Python 3. related unicode changes so that I get a complete Python 3 string environment?</li>
  </ul>
  
  <p>edit:
  In Python 3, <b><a href="http://farmdev.com/talks/unicode/" rel="noreferrer"><code>&lt;type 'str'&gt;</code> is a Unicode object</a></b> and <code>&lt;type 'unicode'&gt;</code> simply does not exist. In my case I want to write code for Python 2(.6) that will work in Python 3. But when I <code>import unicode_literals</code>, I cannot check if a string is of <code>&lt;type 'unicode'&gt;</code> because:</p>
  
  <ul>
  <li>I assume <code>unicode</code> is not part of the namespace</li>
  <li>if <code>unicode</code> is part of the namespace, a literal of <code>&lt;type 'str'&gt;</code> is still unicode when it is created in the same module</li>
  <li><code>type(mystring)</code> will always return <code>&lt;type 'str'&gt;</code> for unicode literals in Python 3</li>
  </ul>
  
  <p>My modules use to be encoded in 'utf-8' by a <code># coding: UTF-8</code> comment at the top, while my <code>locale.getdefaultlocale()[1]</code> returns 'cp1252'. So if I call <code>MyObject('çça')</code> from my console, it is encoded as 'cp1252' in Python 2, and in 'utf-8' when calling <code>MyObject('çça')</code> from the module. In Python 3, it will not be encoded, but a unicode literal.</p>
  
  <p>edit:</p>
  
  <p>I gave up hope about being allowed to avoid using '\' before a <code>u</code> (or <code>x</code> for that matter). Also I understand the limitations of importing <code>unicode_literals</code>. However, the many possible combinations of passing a string from a module to the console and vica versa with each different encoding, and on top of that importing <code>unicode_literals</code> or not and Python 2 vs Python 3, made me want to create an overview by actual testing. Hence the table below. <img src="https://i.stack.imgur.com/sHQSx.gif" alt="enter image description here"></p>
  
  <p>In other words, <code>type(str(''))</code> does not return <code>&lt;type 'str'&gt;</code> in Python 3, but <code>&lt;class 'str'&gt;</code>, and all of Python 2 problems seem to be avoided. </p>