Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ typedef struct {
(assert(PyUnicode_Check(op)), \
_Py_CAST(PyASCIIObject*, (op)))
#define _PyCompactUnicodeObject_CAST(op) \
(assert(PyUnicode_Check(op)), \
(assert(!_PyASCIIObject_CAST(op)->state.ascii || !_PyASCIIObject_CAST(op)->state.compact), \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: you might write the test as "make sure that it's not a compact ASCII string":

Suggested change
(assert(!_PyASCIIObject_CAST(op)->state.ascii || !_PyASCIIObject_CAST(op)->state.compact), \
(assert(!(_PyASCIIObject_CAST(op)->state.ascii \
&& _PyASCIIObject_CAST(op)->state.compact)), \

It's just a minor coding style suggestion, feel free to ignore it.

_Py_CAST(PyCompactUnicodeObject*, (op)))
#define _PyUnicodeObject_CAST(op) \
(assert(PyUnicode_Check(op)), \
(assert(!_PyASCIIObject_CAST(op)->state.compact), \
_Py_CAST(PyUnicodeObject*, (op)))


Expand Down
9 changes: 9 additions & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern "C" {
#include "pycore_fileutils.h" // _Py_error_handler
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
#include "pycore_runtime.h" // _Py_LATIN1_CHR()
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_ACQUIRE


// Maximum code point of Unicode 6.0: 0x10ffff (1,114,111).
Expand Down Expand Up @@ -108,6 +109,11 @@ _PyUnicode_EnsureUnicode(PyObject *obj)
return 0;
}

static inline char* _PyUnicode_UTF8(PyObject *op)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static inline char* _PyUnicode_UTF8(PyObject *op)
static inline char*
_PyUnicode_UTF8(PyObject *op)

{
return FT_ATOMIC_LOAD_PTR_ACQUIRE(_PyCompactUnicodeObject_CAST(op)->utf8);
}

#ifndef NDEBUG
static inline int
_PyUnicodeWriter_CanWrite(_PyUnicodeWriter *writer)
Expand All @@ -125,6 +131,9 @@ _PyUnicodeWriter_CanWrite(_PyUnicodeWriter *writer)
assert(PyUnstable_Unicode_GET_CACHED_HASH(buffer) == -1);
assert(!PyUnicode_CHECK_INTERNED(buffer));
assert(!_Py_IsImmortal(buffer));
assert(PyUnicode_IS_COMPACT_ASCII(buffer)
|| _PyUnicode_UTF8(buffer) == NULL
|| _PyUnicode_UTF8(buffer) == PyUnicode_DATA(buffer));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer moving _PyUnicode_HAS_UTF8_MEMORY() static inline function to pycore_unicodeobject.h and just test:

assert(!_PyUnicode_HAS_UTF8_MEMORY(buffer);

return 1;
}
#endif
Expand Down
8 changes: 3 additions & 5 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ NOTE: In the interpreter's initialization phase, some globals are currently
# define _PyUnicode_CHECK(op) PyUnicode_Check(op)
#endif

static inline char* _PyUnicode_UTF8(PyObject *op)
{
return FT_ATOMIC_LOAD_PTR_ACQUIRE(_PyCompactUnicodeObject_CAST(op)->utf8);
}

static inline char* PyUnicode_UTF8(PyObject *op)
{
assert(_PyUnicode_CHECK(op));
Expand Down Expand Up @@ -1767,6 +1762,9 @@ _PyUnicode_IsModifiable(PyObject *unicode)
return 0;
if (PyUnicode_CHECK_INTERNED(unicode))
return 0;
if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) {
return 0;
}
#ifdef Py_DEBUG
/* singleton refcount is greater than 1 */
assert(!unicode_is_singleton(unicode));
Expand Down
Loading