Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-119182: Use public PyUnicodeWriter in Python-ast.c#129209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
vstinner merged 1 commit intopython:mainfromvstinner:ast_writer
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
74 changes: 39 additions & 35 deletionsParser/asdl_c.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1462,10 +1462,11 @@ def visitModule(self, mod):
return PyObject_Repr(list);
}

_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
writer.overallocate = 1;
PyObject *items[2] = {NULL, NULL};
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
goto error;
}

items[0] = PySequence_GetItem(list, 0);
if (!items[0]) {
Expand All@@ -1479,52 +1480,54 @@ def visitModule(self, mod):
}

bool is_list = PyList_Check(list);
if (_PyUnicodeWriter_WriteChar(&writer, is_list ? '[' : '(') < 0) {
if (PyUnicodeWriter_WriteChar(writer, is_list ? '[' : '(') < 0) {
goto error;
}

for (Py_ssize_t i = 0; i < Py_MIN(length, 2); i++) {
PyObject *item = items[i];
PyObject *item_repr;
if (i > 0) {
if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) {
goto error;
}
}

PyObject *item = items[i];
if (PyType_IsSubtype(Py_TYPE(item), (PyTypeObject *)state->AST_type)) {
PyObject *item_repr;
item_repr = ast_repr_max_depth((AST_object*)item, depth - 1);
} else {
item_repr = PyObject_Repr(item);
}
if (!item_repr) {
goto error;
}
if (i > 0) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
if (!item_repr) {
goto error;
}
if (PyUnicodeWriter_WriteStr(writer, item_repr) < 0) {
Py_DECREF(item_repr);
goto error;
}
}
if (_PyUnicodeWriter_WriteStr(&writer, item_repr) < 0) {
Py_DECREF(item_repr);
goto error;
} else {
if (PyUnicodeWriter_WriteRepr(writer, item) < 0) {
goto error;
}
}

if (i == 0 && length > 2) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ...", 5) < 0) {
Py_DECREF(item_repr);
if (PyUnicodeWriter_WriteUTF8(writer, ", ...", 5) < 0) {
goto error;
}
}
Py_DECREF(item_repr);
}

if (_PyUnicodeWriter_WriteChar(&writer, is_list ? ']' : ')') < 0) {
if (PyUnicodeWriter_WriteChar(writer, is_list ? ']' : ')') < 0) {
goto error;
}

Py_XDECREF(items[0]);
Py_XDECREF(items[1]);
return_PyUnicodeWriter_Finish(&writer);
returnPyUnicodeWriter_Finish(writer);

error:
Py_XDECREF(items[0]);
Py_XDECREF(items[1]);
_PyUnicodeWriter_Dealloc(&writer);
PyUnicodeWriter_Discard(writer);
return NULL;
}

Expand DownExpand Up@@ -1568,14 +1571,15 @@ def visitModule(self, mod):
}

const char* tp_name = Py_TYPE(self)->tp_name;
_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
writer.overallocate = 1;
PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
if (writer == NULL) {
goto error;
}

if (_PyUnicodeWriter_WriteASCIIString(&writer, tp_name,strlen(tp_name)) < 0) {
if (PyUnicodeWriter_WriteUTF8(writer, tp_name,-1) < 0) {
goto error;
}
if (_PyUnicodeWriter_WriteChar(&writer, '(') < 0) {
if (PyUnicodeWriter_WriteChar(writer, '(') < 0) {
goto error;
}

Expand DownExpand Up@@ -1610,43 +1614,43 @@ def visitModule(self, mod):
}

if (i > 0) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
if (PyUnicodeWriter_WriteUTF8(writer, ", ", 2) < 0) {
Py_DECREF(name);
Py_DECREF(value_repr);
goto error;
}
}
if (_PyUnicodeWriter_WriteStr(&writer, name) < 0) {
if (PyUnicodeWriter_WriteStr(writer, name) < 0) {
Py_DECREF(name);
Py_DECREF(value_repr);
goto error;
}

Py_DECREF(name);

if (_PyUnicodeWriter_WriteChar(&writer, '=') < 0) {
if (PyUnicodeWriter_WriteChar(writer, '=') < 0) {
Py_DECREF(value_repr);
goto error;
}
if (_PyUnicodeWriter_WriteStr(&writer, value_repr) < 0) {
if (PyUnicodeWriter_WriteStr(writer, value_repr) < 0) {
Py_DECREF(value_repr);
goto error;
}

Py_DECREF(value_repr);
}

if (_PyUnicodeWriter_WriteChar(&writer, ')') < 0) {
if (PyUnicodeWriter_WriteChar(writer, ')') < 0) {
goto error;
}
Py_ReprLeave((PyObject *)self);
Py_DECREF(fields);
return_PyUnicodeWriter_Finish(&writer);
returnPyUnicodeWriter_Finish(writer);

error:
Py_ReprLeave((PyObject *)self);
Py_DECREF(fields);
_PyUnicodeWriter_Dealloc(&writer);
PyUnicodeWriter_Discard(writer);
return NULL;
}

Expand Down
74 changes: 39 additions & 35 deletionsPython/Python-ast.c
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp