<ulclass="tags-list"><li><ahref="https://blog.robur.coop/tags.html#tag-OCaml">OCaml</a></li><li><ahref="https://blog.robur.coop/tags.html#tag-Python">Python</a></li><li><ahref="https://blog.robur.coop/tags.html#tag-unicode">unicode</a></li></ul><p>Sometimes software is written using whatever built-ins you find in your programming language of choice.
However, it can happen that you depend on the precise semantics of those built-ins.
This can be a problem if those semantics become important to your software and you need to port it to another programming language.
This story is about Python and its <code>str.__repr()__</code> function.</p>
<p>The piece of software I was helping port to <ahref="https://ocaml.org/">OCaml</a> was constructing a hash from the string representation of a tuple.
The gist of it was basically this:</p>
<pre><codeclass="language-python">def get_id(x):
id = (x.get_unique_string(), x.path, x.name)
return myhash(str(id))
</code></pre>
<p>In other words it's a Python tuple consisting of mostly strings but also a <code>PosixPath</code> object.
The way <code>str()</code> works is it calls the <code>__str__()</code> method on the argument objects (or otherwise <code>repr(x)</code>).
For Python tuples the <code>__str__()</code> method seems to print the result of <code>repr()</code> on each elemenet separated by a comma and a space and surrounded by parenthesis.
So good so far.
If we can precisely emulate <code>repr()</code> on strings and <code>PosixPath</code> it's easy.
In the case of <code>PosixPath</code> it's really just <code>'PosixPath('+repr(str(path))+')'</code>;
so in that case it's down to <code>repr()</code> on strings - which is <code>str.__repr__()</code>,</p>
<p>There had been a previous attempt at this that would use OCaml's string escape functions and surround the string with single quotes (<code>'</code>).
This works for some cases, but not if the string has a double quote (<code>"</code>).
In that case OCaml would escape the double quote with a backslash (<code>\"</code>) while python would not escape it.
So a regular expression substitution was added to replace the escape sequence with just a double quote.
This pattern of finding small differences between Python and OCaml escaping had been repeated,
and eventually I decided to take a more rigorous approach to it.</p>
I can recommend reading Daniel Bünzli's <ahref="https://ocaml.org/p/uucp/13.0.0/doc/unicode.html#minimal">minimal introduction to Unicode</a>.
Already here there is a significant gap in semantics between Python and OCaml.
For many practical purposes we can get away with using the OCaml <code>string</code> type and treating it as a UTF-8 encoded Unicode string.
This is what I will do as in both the Python code and the OCaml code the data being read is a UTF-8 (or often only the US ASCII subset) encoded string.</p>
<h2id="what-does-a-string-literal-look-like"><aclass="anchor"aria-hidden="true"href="#what-does-a-string-literal-look-like"></a>What does a string literal look like?</h2>
<p>I will not dive too deep into the details of OCaml string literals, and focus mostly on how they are escaped by the language built-ins (<code>String.escaped</code>, <code>Printf.printf "%S"</code>).
Normal printable ASCII is printed as-is.
That is, letters, numbers and other symbols except for backslash and double quote.
There are the usual escape sequences <code>\n</code>, <code>\t</code>, <code>\r</code>, <code>\"</code> and <code>\\</code>.
Any byte value can be represented with decimal notation <code>\032</code> or octal notation '\o040' or hexadecimal notation <code>\x20</code>.
The escape functions in OCaml has a preference for the decimal notation over the hexadecimal notation.
Finally I also want to mention the Unicode code point escape sequence <code>\u{3bb}</code> which represents the UTF-8 encoding of U+3BB.
While the escape functions do not use it, it will become handy later on.
Illegal escape sequences (escape sequences that are not recognized) will emit a warning but otherwise result in the escape sequence as-is.
It is common to compile OCaml programs with warnings-as-errors, however.</p>
<p>Python has a number of different string literals and string-like literals.
They all use single quote or double quote to delimit the string (or string-like) literals.
There is a preference towards single quotes in <code>str.__repr__()</code>.
You can also triple the quotes if you like to write a string that uses a lot of both quote characters.
This format is not used by <code>str.__repr__()</code> so I will not cover it further, but you can read about it in the <ahref="https://docs.python.org/3/reference/lexical_analysis.html#strings">Python reference manual</a>.
The string literal can optionally have a prefix character that modifies what type the string literal is and how its content is interpreted.</p>
<p>The <code>r</code>-prefixed strings are called <em>raw strings</em>.
That means backslash escape sequences are not interpreted.
In my experiments they seem to be quasi-interpreted, however!
The string <code>r"\"</code> is considered unterminated!
<p>While studying Python string literals I discovered several odd corners of the syntax and semantics besides the raw string quasi-escape sequence mentioned earlier.
One fact is that Python doesn't have a separate character or Unicode code point type.
Instead, a character is a one element string.
This leads to some interesting indexing shenanigans: <code>"a"[0][0][0] == "a"</code>.
Furthermore, strings separated by spaces only are treated as one single concatenated string: <code>"a""b""c" == "abc"</code>.
These two combined makes it possible to write this unusual snippet: <code>"a""b""c"[0] == "a"</code>!
For byte sequences, or <code>b</code>-prefixed strings, things are different.
Indexing a bytes object returns the integer value of that byte (or character):</p>
<stdin>:1: SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable
</code></pre>
<p>For strings <code>\x32</code> can be said to be shorthand for <code>"\u0032"</code> (or <code>"\u00000032"</code>).
But for bytes <code>"\x32" != "\u0032"</code>!
Why is this?!
Well, bytes is a byte sequence and <code>b"\u0032"</code> is not interpreted as an escape sequence and is instead <strong>silently</strong> treated as <code>b"\\u0032"</code>!
Writing <code>"\xff".encode()</code> which encodes the string <code>"\xff"</code> to UTF-8 is <strong>not</strong> the same as <code>b"\xff"</code>.
The bytes <code>"\xff"</code> consist of a single byte with decimal value 255,
and the Unicode wizards reading will know that the Unicode code point 255 (or U+FF) is encoded in two bytes in UTF-8.</p>
<p>Finding the implementation of <code>str.__repr__()</code> turned out to not be so easy.
In the end I asked on the Internet and got a link to <ahref="https://github.com/python/cpython/blob/963904335e579bfe39101adf3fd6a0cf705975ff/Objects/unicodeobject.c#L12245-L12405">cpython's <code>Objects/unicodeobject.c</code></a>.
And holy cow!
That's some 160 lines of C code with two loops, a switch statement and I don't know how many chained and nested if statements!
Meanwhile the OCaml implementation is a much less daunting 52 lines of which about a fifth is a long comment.
It also has two loops which each contain one much more tame match expression (roughly a C switch statement).
In both cases they first loop over the string to compute the size of the output string.
The Python implementation also counts the number of double quotes and single quotes as well as the highest code point value.
The latter I'm not sure why they do, but my guess it's so they can choose an efficient internal representation.
Then the Python code decides what quote character to use with the following algorithm:<br>
Does the string contain single quotes but no double quotes? Then use double quotes. Otherwise use single quotes.
Then the output size estimate is adjusted with the number of backslashes to escape the quote character chosen and the two quotes surrounding the string.</p>
<p>Already here it's clear that a regular expression substitution is not enough by itself to fix OCaml escaping to be Python escaping.
My first step then was to implement the algorithm only for US ASCII.
This is simpler as we don't have to worry much about Unicode, and I could implement it relatively quickly.
The first 32 characters and the last US ASCII character (DEL or <code>\x7f</code>) are considered non-printable and must be escaped.
I then wrote some simple tests by hand.
Then I discovered the OCaml <ahref="https://github.com/zshipko/ocaml-py">py</a> library which provides bindings to Python from OCaml.
Great! This I can use to test my implementation against Python!</p>
<p>For the non-ascii characters (or code points rather) they are either considered <em>printable</em> or <em>non-printable</em>.
For now let's look at what that means for the output.
A printable character is copied as-is.
That is, there is no escaping done.
Non-printable characters must be escaped, and python wil use <code>\xHH</code>, <code>\uHHHH</code> or <code>\UHHHHHHHH</code> depending on how many hexadecimal digits are necessary to represent the code point.
That is, the latin-1 subset of ASCII (<code>0x80</code>-<code>0xff</code>) can be represented using <code>\xHH</code> and neither <code>\u00HH</code> nor <code>\U000000HH</code> will be used etc.</p>
<h3id="what-is-a-printable-unicode-character"><aclass="anchor"aria-hidden="true"href="#what-is-a-printable-unicode-character"></a>What is a printable Unicode character?</h3>
<p>In the cpython <ahref="https://github.com/python/cpython/blob/963904335e579bfe39101adf3fd6a0cf705975ff/Objects/unicodeobject.c#L12245-L12405">function</a> mentioned earlier they use the function <code>Py_UNICODE_ISPRINTABLE</code>.
I had a local clone of the cpython git repository where I ran <code>git grep Py_UNICODE_ISPRINTABLE</code> to find information about it.
In <ahref="https://github.com/python/cpython/blob/963904335e579bfe39101adf3fd6a0cf705975ff/Doc/c-api/unicode.rst?plain=1#L257-L265">unicode.rst</a> I found a documentation string for the function that describes it to return false if the character is nonprintable with the definition of nonprintable as the code point being in the categories "Other" or "Separator" in the Unicode character database <strong>with the exception of ASCII space</strong> (U+20 or <code></code>).</p>
<p>What are those "Other" and "Separator" categories?
Further searching for the function definition we find in <ahref="https://github.com/python/cpython/blob/963904335e579bfe39101adf3fd6a0cf705975ff/Include/cpython/unicodeobject.h#L683"><code>Include/cpython/unicodeobject.h</code></a> the definition.
Well, we find <code>#define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)</code>.
On to <code>git grep _PyUnicode_IsPrintable</code> then.
That function is defined in <ahref="https://github.com/python/cpython/blob/963904335e579bfe39101adf3fd6a0cf705975ff/Objects/unicodectype.c#L158-L163"><code>Objects/unicodectype.c</code></a>.</p>
<pre><codeclass="language-C">/* Returns 1 for Unicode characters to be hex-escaped when repr()ed,
0 otherwise.
All characters except those characters defined in the Unicode character
database as following categories are considered printable.
return (ctype->flags & PRINTABLE_MASK) != 0;
}
</code></pre>
<p>Ok, now we're getting close to something.
Searching for <code>PRINTABLE_MASK</code> we find in <ahref="https://github.com/python/cpython/blob/963904335e579bfe39101adf3fd6a0cf705975ff/Tools/unicode/makeunicodedata.py#L450-L451"><code>Tools/unicode/makeunicodedata.py</code></a> the following line of code:</p>
<pre><codeclass="language-Python">if char == ord("") or category[0] not in ("C", "Z"):
flags |= PRINTABLE_MASK
</code></pre>
<p>So the algorithm is really if the character is a space character or if its Unicode general category doesn't start with a <code>C</code> or <code>Z</code>.
This can be implemented in OCaml using the uucp library as follows:</p>
<p>After implementing unicode I expanded the tests to generate arbitrary OCaml strings and compare the results of calling my function and Python's <code>str.__repr__()</code> on the string.
Well, that didn't go quite well.
OCaml strings are just any byte sequence, and ocaml-py expects it to be a UTF-8 encoded string and fails on invalid UTF-8.
Then in qcheck you can "assume" a predicate which means if a predicate doesn't hold on the generated value then the test is skipped for that input.
So I implement a simple verification of UTF-8.
This is far from optimal because qcheck will generate a lot of invalid utf-8 strings.</p>
<p>The next test failure is some unassigned code point.
So I add to <code>py_unicode_isprintable</code> a check that the code point is assigned using <code>Uucp.Age.age uchar <> `Unassigned</code>.</p>
<p>Still, qcheck found a case I hadn't considered: U+61D.
My python version (Python 3.9.2 (default, Feb 28 2021, 17:03:44)) renders this as <code>'\u061'</code> while my OCaml function prints it as-is.
In other words my implementation considers it printable while python does not.
I try to enter this Unicode character in my terminal, but nothing shows up.
Then I look it up and its name is <code>ARABIC END OF TEXT MARKER</code>.
The general category according to uucp is <code>`Po</code>.
So this <strong>should</strong> be a printable character‽</p>
<p>After being stumped by this for a while I get the suspicion it may be dependent on the Python version.
I am still on Debian 11 and my Python version is far from being the latest and greatest.
I ask someone with a newer Python version to write <code>'\u061d'</code> in a python session.
And 'lo! It prints something that looks like <code>''</code>!
Online I figure out how to get the unicode version compiled into Python:</p>
<p>My reaction is this is seriously some ungodly mess we are in.
Not only is the code that instigated this journey highly dependent on Python-specifics - it's also dependent on the specific version of unicode and thus the version of Python!</p>
<p>I modify our <code>py_unicode_isprintable</code> function to take an optional <code>?unicode_version</code> argument and replace the "is this unassigned?" check with the following snippet:</p>
<pre><codeclass="language-OCaml">let age = Uucp.Age.age uchar in
(match (age, unicode_version) with
| `Unassigned, _ -> false
| `Version _, None -> true
| `Version (major, minor), Some (major', minor') ->
major < major' || (major = major' && minor <= minor'))
</code></pre>
<p>Great! I modify the test suite to first detect the unicode version python uses and then pass that version to the OCaml function.
<p>Language and (standard) library designers could consider whether the slightly nicer looking strings are worth the added complexity users eventually are going to rely on - inadvertently or not.
I do think strings and bytes in Python are a bit too complex.
In my opinion it is a mistake to not at least print a warning if there are illegal escape sequences - especially considering there are escape sequences that are valid in one string literal but not another.</p>
<p>Unfortunately it is often the case that to get a precise specification it is necessary to look at the implementation.
For testing your implementation hand-written tests are good.
Testing against the original implementation is great, and if combined with property-based testing or fuzzing you may find failing test cases you couldn't dream up!
I certainly didn't see it coming that the output depends on the Unicode version.
As is said, testing can only show the presence of bugs, but with a, in a sense, limited domain like this function you can get pretty close to showing absence of bugs.</p>
<p>I enjoyed working on this.
Sure, it was frustrating and at times I discovered some ungodly properties, but it's a great feeling to study and understand something at a deeper level.
It may be the last time I need to understand Python's <code>str.__repr__()</code> this well, but if I do I now have the OCaml code and this blog post to reread.</p>
<p>If you are curious to read the resulting code you may find it on github at <ahref="https://github.com/reynir/python-str-repr">github.com/reynir/python-str-repr</a>.
I have documented the code to make it more approachable and maintainable by others.
Hopefully it is not something that you need, but in case it is useful to you it is licensed under a permissive license.</p>
<p>If you have a project in OCaml or want to port something to OCaml and would like help from me and my colleagues at <ahref="https://robur.coop/">Robur</a> please <ahref="https://robur.coop/Contact">get in touch</a> with us and we will figure something out.</p>
<p>[^raw-escape-example]: Note I use single quotes for the output. This is what Python would do. It would be equivalent to <code>"\\\""</code>.</p>
<p>[^language-lawyer]: <ahref="http://catb.org/jargon/html/L/language-lawyer.html">A person, usually an experienced or senior software engineer, who is intimately familiar with many or most of the numerous restrictions and features (both useful and esoteric) applicable to one or more computer programming languages. A language lawyer is distinguished by the ability to show you the five sentences scattered through a 200-plus-page manual that together imply the answer to your question “if only you had thought to look there”.</a></p>