Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
46386f6f
Commit
46386f6f
authored
Nov 19, 2013
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #19513: repr(tuple) now uses _PyUnicodeWriter for better performances
parent
71bc8c29
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
27 deletions
+37
-27
Objects/tupleobject.c
Objects/tupleobject.c
+37
-27
No files found.
Objects/tupleobject.c
View file @
46386f6f
...
...
@@ -255,20 +255,12 @@ static PyObject *
tuplerepr
(
PyTupleObject
*
v
)
{
Py_ssize_t
i
,
n
;
PyObject
*
s
=
NULL
;
_PyAccu
acc
;
static
PyObject
*
sep
=
NULL
;
_PyUnicodeWriter
writer
;
n
=
Py_SIZE
(
v
);
if
(
n
==
0
)
return
PyUnicode_FromString
(
"()"
);
if
(
sep
==
NULL
)
{
sep
=
PyUnicode_FromString
(
", "
);
if
(
sep
==
NULL
)
return
NULL
;
}
/* While not mutable, it is still possible to end up with a cycle in a
tuple through an object that stores itself within a tuple (and thus
infinitely asks for the repr of itself). This should only be
...
...
@@ -278,40 +270,58 @@ tuplerepr(PyTupleObject *v)
return
i
>
0
?
PyUnicode_FromString
(
"(...)"
)
:
NULL
;
}
if
(
_PyAccu_Init
(
&
acc
))
goto
error
;
_PyUnicodeWriter_Init
(
&
writer
);
writer
.
overallocate
=
1
;
if
(
Py_SIZE
(
v
)
>
1
)
{
/* "(" + "1" + ", 2" * (len - 1) + ")" */
writer
.
min_length
=
1
+
1
+
(
2
+
1
)
*
(
Py_SIZE
(
v
)
-
1
)
+
1
;
}
else
{
/* "(1,)" */
writer
.
min_length
=
4
;
}
s
=
PyUnicode_FromString
(
"("
);
if
(
s
==
NULL
||
_PyAccu_Accumulate
(
&
acc
,
s
))
if
(
_PyUnicodeWriter_WriteChar
(
&
writer
,
'('
)
<
0
)
goto
error
;
Py_CLEAR
(
s
);
/* Do repr() on each element. */
for
(
i
=
0
;
i
<
n
;
++
i
)
{
PyObject
*
s
;
if
(
i
>
0
)
{
if
(
_PyUnicodeWriter_WriteASCIIString
(
&
writer
,
", "
,
2
)
<
0
)
goto
error
;
}
if
(
Py_EnterRecursiveCall
(
" while getting the repr of a tuple"
))
goto
error
;
s
=
PyObject_Repr
(
v
->
ob_item
[
i
]);
Py_LeaveRecursiveCall
();
if
(
i
>
0
&&
_PyAccu_Accumulate
(
&
acc
,
sep
)
)
if
(
s
==
NULL
)
goto
error
;
if
(
s
==
NULL
||
_PyAccu_Accumulate
(
&
acc
,
s
))
if
(
_PyUnicodeWriter_WriteStr
(
&
writer
,
s
)
<
0
)
{
Py_DECREF
(
s
);
goto
error
;
}
Py_DECREF
(
s
);
}
writer
.
overallocate
=
0
;
if
(
n
>
1
)
{
if
(
_PyUnicodeWriter_WriteChar
(
&
writer
,
')'
)
<
0
)
goto
error
;
}
else
{
if
(
_PyUnicodeWriter_WriteASCIIString
(
&
writer
,
",)"
,
2
)
<
0
)
goto
error
;
Py_CLEAR
(
s
);
}
if
(
n
>
1
)
s
=
PyUnicode_FromString
(
")"
);
else
s
=
PyUnicode_FromString
(
",)"
);
if
(
s
==
NULL
||
_PyAccu_Accumulate
(
&
acc
,
s
))
goto
error
;
Py_CLEAR
(
s
);
Py_ReprLeave
((
PyObject
*
)
v
);
return
_Py
Accu_Finish
(
&
acc
);
return
_Py
UnicodeWriter_Finish
(
&
writer
);
error:
_PyAccu_Destroy
(
&
acc
);
Py_XDECREF
(
s
);
_PyUnicodeWriter_Dealloc
(
&
writer
);
Py_ReprLeave
((
PyObject
*
)
v
);
return
NULL
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment