Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45c0787

Browse files
committed
Simplified recursion logic. Modified variable name to match string.Formatter.
1 parent 1152919 commit 45c0787

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

Objects/stringlib/string_format.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct {
2929
/* forward declaration for recursion */
3030
static PyObject *
3131
build_string(SubString *input, PyObject *args, PyObject *kwargs,
32-
int *recursion_level);
32+
int recursion_depth);
3333

3434

3535

@@ -792,7 +792,7 @@ static int
792792
output_markup(SubString *field_name, SubString *format_spec,
793793
int format_spec_needs_expanding, STRINGLIB_CHAR conversion,
794794
OutputString *output, PyObject *args, PyObject *kwargs,
795-
int *recursion_level)
795+
int recursion_depth)
796796
{
797797
PyObject *tmp = NULL;
798798
PyObject *fieldobj = NULL;
@@ -818,7 +818,7 @@ output_markup(SubString *field_name, SubString *format_spec,
818818

819819
/* if needed, recurively compute the format_spec */
820820
if (format_spec_needs_expanding) {
821-
tmp = build_string(format_spec, args, kwargs, recursion_level);
821+
tmp = build_string(format_spec, args, kwargs, recursion_depth-1);
822822
if (tmp == NULL)
823823
goto done;
824824

@@ -852,7 +852,7 @@ output_markup(SubString *field_name, SubString *format_spec,
852852
*/
853853
static int
854854
do_markup(SubString *input, PyObject *args, PyObject *kwargs,
855-
OutputString *output, int *recursion_level)
855+
OutputString *output, int recursion_depth)
856856
{
857857
MarkupIterator iter;
858858
int format_spec_needs_expanding;
@@ -871,7 +871,7 @@ do_markup(SubString *input, PyObject *args, PyObject *kwargs,
871871
if (field_name.ptr != field_name.end)
872872
if (!output_markup(&field_name, &format_spec,
873873
format_spec_needs_expanding, conversion, output,
874-
args, kwargs, recursion_level))
874+
args, kwargs, recursion_depth))
875875
return 0;
876876
}
877877
return result;
@@ -884,7 +884,7 @@ do_markup(SubString *input, PyObject *args, PyObject *kwargs,
884884
*/
885885
static PyObject *
886886
build_string(SubString *input, PyObject *args, PyObject *kwargs,
887-
int *recursion_level)
887+
int recursion_depth)
888888
{
889889
OutputString output;
890890
PyObject *result = NULL;
@@ -893,8 +893,7 @@ build_string(SubString *input, PyObject *args, PyObject *kwargs,
893893
output.obj = NULL; /* needed so cleanup code always works */
894894

895895
/* check the recursion level */
896-
(*recursion_level)--;
897-
if (*recursion_level < 0) {
896+
if (recursion_depth <= 0) {
898897
PyErr_SetString(PyExc_ValueError,
899898
"Max string recursion exceeded");
900899
goto done;
@@ -907,7 +906,7 @@ build_string(SubString *input, PyObject *args, PyObject *kwargs,
907906
INITIAL_SIZE_INCREMENT))
908907
goto done;
909908

910-
if (!do_markup(input, args, kwargs, &output, recursion_level)) {
909+
if (!do_markup(input, args, kwargs, &output, recursion_depth)) {
911910
goto done;
912911
}
913912

@@ -921,7 +920,6 @@ build_string(SubString *input, PyObject *args, PyObject *kwargs,
921920
output.obj = NULL;
922921

923922
done:
924-
(*recursion_level)++;
925923
Py_XDECREF(output.obj);
926924
return result;
927925
}
@@ -940,10 +938,10 @@ do_string_format(PyObject *self, PyObject *args, PyObject *kwargs)
940938
"{0:{1}}".format('abc', 's') # works
941939
"{0:{1:{2}}}".format('abc', 's', '') # fails
942940
*/
943-
int recursion_level = 2;
941+
int recursion_depth = 2;
944942

945943
SubString_init(&input, STRINGLIB_STR(self), STRINGLIB_LEN(self));
946-
return build_string(&input, args, kwargs, &recursion_level);
944+
return build_string(&input, args, kwargs, recursion_depth);
947945
}
948946

949947

0 commit comments

Comments
 (0)