Is it possible to suppress Python's escape sequence processing on a given string without using the raw specifier?
<p><strong>Conclusion:</strong> It's impossible to override or disable Python's built-in escape sequence processing, such that, you can skip using the raw prefix specifier.  I dug into Python's internals to figure this out.  So if anyone tries designing objects that work on complex strings (like regex) as part of some kind of framework, make sure to specify in the docstrings that string arguments to the object's <code>__init__()</code> <strong>MUST</strong> include the <code>r</code> prefix!</p>

<p><br/><br/><br/><strong>Original question:</strong> I am finding it a bit difficult to force Python to not "change" anything about a user-inputted string, which may contain among other things, regex or escaped hexadecimal sequences.  I've already tried various combinations of raw strings, <code>.encode('string-escape')</code> (and its decode counterpart), but I can't find the right approach.</p>

<p>Given an escaped, hexadecimal representation of the Documentation IPv6 address <code>2001:0db8:85a3:0000:0000:8a2e:0370:7334</code>, using <code>.encode()</code>, this small script (called <code>x.py</code>):</p>

<pre><code>#!/usr/bin/env python

class foo(object):
    __slots__ = ("_bar",)
    def __init__(self, input):
        if input is not None:
            self._bar = input.encode('string-escape')
        else:
            self._bar = "qux?"

    def _get_bar(self): return self._bar
    bar = property(_get_bar)
#

x = foo("\x20\x01\x0d\xb8\x85\xa3\x00\x00\x00\x00\x8a\x2e\x03\x70\x73\x34")
print x.bar
</code></pre>

<p><br/>Will yield the following output when executed:</p>

<pre><code>$ ./x.py
 \x01\r\xb8\x85\xa3\x00\x00\x00\x00\x8a.\x03ps4
</code></pre>

<p><br/>Note the <code>\x20</code> got converted to an ASCII space character, along with a few others.  This is basically correct due to Python processing the escaped hex sequences and converting them to their printable ASCII values.</p>

<p><br/>This can be solved if the initializer to <code>foo()</code> was treated as a raw string (and the <code>.encode()</code> call removed), like this:</p>

<pre><code>x = foo(r"\x20\x01\x0d\xb8\x85\xa3\x00\x00\x00\x00\x8a\x2e\x03\x70\x73\x34")
</code></pre>

<p><br/>However, my end goal is to create a kind of framework that can be used and I want to hide these kinds of "implementation details" from the end user.  If they called <code>foo()</code> with the above IPv6 address in escaped hexadecimal form (without the raw specifier) and immediately print it back out, they should get back <em>exactly</em> what they put in w/o knowing or using the raw specifier.  So I need to find a way to have <code>foo</code>'s <code>__init__()</code> do whatever processing is necessary to enable that.</p>

<p><br/><br/><strong>Edit:</strong> Per <a href="http://stackoverflow.com/a/647787">this SO question</a>, it seems it's a defect of Python, in that it <strong>always</strong> performs some kind of escape sequence processing.  There does not appear to be any kind of facility to completely turn off escape sequence processing, even temporarily.  Sucks.  I guess I am going to have to research subclassing <code>str</code> to create something like <code>rawstr</code> that intelligently determines what escape sequences Python processed in a string, and convert them back to their original format.  This is not going to be fun...</p>

<p><br/><strong>Edit2:</strong> Another example, given the sample regex below:</p>

<pre><code>"^.{0}\xcb\x00\x71[\x00-\xff]"
</code></pre>

<p><br/>If I assign this to a var or pass it to a function <strong>without</strong> using the raw specifier, the <code>\x71</code> gets converted to the letter <code>q</code>.  Even if I add <code>.encode('string-escape')</code> or <code>.replace('\\', '\\\\')</code>, the escape sequences <strong>are still processed</strong>.  thus resulting in this output:</p>

<pre><code>"^.{0}\xcb\x00q[\x00-\xff]"
</code></pre>

<p><br/>How can I stop this, again, without using the raw specifier?  Is there some way to "turn off" the escape sequence processing or "revert" it after the fact thus that the <code>q</code> turns back into <code>\x71</code>?  Is there a way to process the string and escape the backslashes <strong>before</strong> the escape sequence processing happens?</p>
