FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-157710: Add _PyUnicodeWriter_CanWrite() function by vstinner · Pull Request #157712 · python/cpython · GitHub

Repository navigation

gh-157710: Add _PyUnicodeWriter_CanWrite() function - #157712

Merged
vstinner merged 4 commits into
python:mainfrom
vstinner:unicode_writer
Sep 18, 2026
Merged

vstinner merged 4 commits into
python:mainfrom
vstinner:unicode_writer

Conversation

vstinner commented Sep 17, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Make sure that a writer can be modified before writing into it. On Free Threading, _PyUnicodeWriter_CanWrite() fails if called from a thread other than the one which created the writer.

Document that PyUnicodeWriter is not thread safe.

Add also singletons tests to test_capi.test_unicode.

Make sure that a writer can be modified before writing into it.  On
Free Threading, _PyUnicodeWriter_CanWrite() fails if called from a
thread other than the one which created the writer.

Document that PyUnicodeWriter is not thread safe.

Add also singletons tests to test_capi.test_unicode.

read-the-docs-community Bot commented Sep 17, 2026 •
edited
Loading

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34624058 | 📁 Comparing 688a3ed against main (5539c2a)

  🔍 Preview build  

1 file changed
± c-api/unicode.html

Copy link
Copy Markdown
Member Author

Oh, on Free Threading, PyUnicodeWriter_CanWrite() assertion fails in TestCSV.test_concurrent_writer_writerow() of test_free_threading.test_csv:

../cpython-ro-srcdir/Objects/unicode_writer.c:354: _PyUnicodeWriter_WriteStr: Assertion `_PyUnicodeWriter_CanWrite(writer)' failed.

Code:

    def test_concurrent_writer_writerow(self):
        output_stream = io.StringIO()
        writer = csv.writer(output_stream)
        row_per_thread = 10
        expected_rows = []

        def write_row():
            for i in range(row_per_thread):
                writer.writerow([i, i, i])
                expected_rows.append(f"{i},{i},{i}")

        run_concurrently(worker_func=write_row, nthreads=NTHREADS)

        # Rewind to the start of the stream and parse the rows
        output_stream.seek(0)
        output_rows = [line.strip() for line in output_stream.readlines()]

        self.assertEqual(len(output_rows), NTHREADS * row_per_thread)
        self.assertListEqual(sorted(output_rows), sorted(expected_rows))

csv is implemented with io.StringIO which uses PyUnicodeWriter but uses a critical section to make sure that the PyUnicodeWriter is not used by two threads at the same time.

Check PyUnicode_CheckExact() earlier in _PyUnicode_IsModifiable().

Copy link
Copy Markdown
Member Author

I pushed a fix for test_free_threading: do not check _PyObject_IsUniquelyReferenced() in _PyUnicodeWriter_CanWrite(). It's ok to use PyUnicodeWriter from different threads, since the caller can have its own lock, as io.StringIO does.

vstinner enabled auto-merge (squash) September 17, 2026 23:58
vstinner merged commit 8542958 into python:main Sep 18, 2026
54 checks passed
vstinner deleted the unicode_writer branch September 18, 2026 00:32
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL