Repository navigation
Add Python-compatible decimal digits and temp file helpers - #81
Merged
Merged
Conversation
Python's float() and int() turn each character that has a decimal value into the `ASCII` digit of that value before they parse the text. So float() of the Arabic-Indic digit three is 3.0, and of the fullwidth digits one and zero is 10.0. A port that tests for `ASCII` digits only rejects text that Python takes. pytext gains decimalDigit, which returns the value of a code point or null. It also gains decimalToAscii, which returns a copy of a byte string with each decimal digit written as its `ASCII` digit. Everything else stays as it was, and that includes invalid UTF-8 bytes. A tool that already parses `ASCII` text runs this first and keeps its parser. The digits are the runs of ten code points that start at each digit zero. A script under tools generates the table of zeros from the unicodedata module, so nobody types it by hand. The script stops when the data is not laid out as runs of ten. A build step rewrites the table in the source tree. The library build does not run Python. The table comes from Python 3.14.10, whose Unicode version is 16.0.0. It has 76 runs of ten digits. The header of the checked-in table names that version, and the table is pinned to it. Another Python carries other Unicode data, so its unicodedata is not the reference. The check mode of the generator compares the file with unicodedata only when the two versions agree. Otherwise it compares nothing, exits with status 3 and names both versions. The parity test runs that check mode. It also compares decimalToAscii with Python over every code point that UTF-8 can carry, in blocks of 1024, and over strings with bytes that are not UTF-8. Both comparisons skip with a message that names the two versions under a Python of another Unicode version. The CI test job sets up Python 3.14, so they run there. A further test gives the check mode a copy of the table that names another version and expects status 3 and the message.
A caller that mirrors Python's tempfile.mkdtemp has to word its failure as Python does, and a failed create left it with an errno and no path. mkdtempReport is the same create with a Why argument. The loop reports nothing while it retries a name that exists. On failure, the Why gets the OSError text in the form Python gives. Python joins the directory and the name it chose, then calls mkdir on the result, so its error quotes the whole path, random part included, as 'dir/name'. mkdtempReport joins them the same way and words the text with pytext.osError, the call that words the other path errors. A failure before any name was tried reports the directory alone. mkdtemp keeps its shape as a wrapper with its own quiet Why. Existing callers do not change. The unit test fails a create under a missing parent, under a parent whose name holds a single quote, and under a file in the parent's place. It checks the error. It takes the eight random characters from the end of the message and builds the expected text with pytext.osError of the joined path, so the quote that repr picks comes from the same rule. The quoted parent makes repr pick double quotes.
A tool that mirrors Python's temporary files needs the directory that tempfile.gettempdir picks. Picking it is more than testing for a writable directory. Python takes the first candidate that accepts a new file with a random name and four bytes written to it. The environment supplies the first candidates in a fixed order. The system directories follow, and the working directory is the last resort. Python tries up to 100 names for a candidate. It gives up on a candidate at the first error other than a name that exists. The answer is made absolute and normalized, and it is kept for the life of the process. When no candidate works, Python raises a FileNotFoundError whose text lists the candidates in repr form. fsx had no candidate chooser to extend, because its mkdtemp takes the directory from its caller. The new chooser and mkdtemp share one random name routine. fsx.TempDir keeps the answer and the text of the last failure. Its get method takes any environment with a get method, so the process environment and a string map both fit. A relative value is joined to the working directory as os.path.abspath does. A working directory that was removed ends the call with the missing file error that Python gives. fsx.tempDirCandidates returns the list alone, and fsx.freeTempDirCandidates frees it. The TempDir owns its answer and its message, and deinit frees both. A call frees the candidate list, each join of a relative value and each candidate that failed, and a new message frees the one it replaces. A caller with a general allocator can retry without a leak. The unit tests run on std.testing.allocator, which fails a test that leaks. One test makes every candidate fail twice under a file size limit of zero, then fails a relative value under a removed working directory, then succeeds, then frees the TempDir. A Python test builds its scenarios when it runs. A probe program and a tempfile call run in fresh processes under the same settings, and the test compares their output byte for byte. The scenarios vary the environment and the state of the candidate directories. Size limits make the probe write short or fail. A directory of mode 0500 is compared for every user, and the case that needs a candidate no user can write to uses /proc. The CI test job builds the probe, and the test runs against it.
Python's tempfile.mkstemp makes a file in the directory that gettempdir picks. The name is the caller's prefix with a random suffix of eight characters. The file has mode 0600 and is opened for reading and writing without following a link. Python tries another name after one that exists. Any other refusal becomes an OSError that names the path it tried, random part included. Since Python 3.12 it passes the directory through os.path.abspath first, so the path it returns and the path its error names are absolute even for a relative directory. A caller that mirrors it had to copy the name loop and the open flags. fsx.mkstemp returns the open descriptor with the path. It has the shape of mkdtemp, plus one argument that receives the path of a refused open so that the caller can format the error with pytext.osError. The argument stays null when no name was tried and when every name existed. The directory goes through fsx.absPath first, the port of os.path.abspath. The name routine is the one that mkdtemp and the temporary directory probe use. The unit test makes a file and checks that it is a regular file of mode 0600 with the expected name. A write to it works. A second call under a missing directory checks the path that comes back. Another test passes a relative directory with dot segments. The path that comes back and the path of a refused open are both absolute and normalized.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds four changes to the library:
Tests cover each change, and the parity tests compare against Python.