comparison configmix/_speedups.c @ 560:81238ea2dbe3

Implement and use more helper functions. Improve comments.
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 06 Jan 2022 19:37:03 +0100
parents bb160a1e67d7
children c51f484387ee
comparison
equal deleted inserted replaced
559:bb160a1e67d7 560:81238ea2dbe3
567 } 567 }
568 tmp = PyObject_CallMethod(filters, "strip", NULL); 568 tmp = PyObject_CallMethod(filters, "strip", NULL);
569 if (tmp == NULL) { 569 if (tmp == NULL) {
570 goto error; 570 goto error;
571 } 571 }
572 Py_DECREF(filters); 572 py_transfer_owned(&filters, &tmp);
573 filters = tmp; tmp = NULL;
574 573
575 if (PyObject_Not(filters)) { 574 if (PyObject_Not(filters)) {
576 py_clear_ref(&filters); 575 py_clear_ref(&filters);
577 576
578 res = PyTuple_New(2); 577 res = PyTuple_New(2);
584 filters = PyList_New(0); 583 filters = PyList_New(0);
585 if (filters == NULL) { 584 if (filters == NULL) {
586 goto error; 585 goto error;
587 } 586 }
588 PyTuple_SetItem(res, 1, filters); /* steals */ 587 PyTuple_SetItem(res, 1, filters); /* steals */
588 filters = NULL;
589 return res; 589 return res;
590 } 590 }
591 591
592 if (sstate == NULL) { 592 if (sstate == NULL) {
593 sstate = PyModule_GetState(self); 593 sstate = PyModule_GetState(self);
599 599
600 tmp = PyUnicode_Split(filters, sstate->FILTER_SEPARATOR, -1); 600 tmp = PyUnicode_Split(filters, sstate->FILTER_SEPARATOR, -1);
601 if (tmp == NULL) { 601 if (tmp == NULL) {
602 goto error; 602 goto error;
603 } 603 }
604 Py_DECREF(filters); 604 py_transfer_owned(&filters, &tmp);
605 filters = tmp; tmp = NULL;
606 605
607 res = PyTuple_New(2); 606 res = PyTuple_New(2);
608 if (res == NULL) { 607 if (res == NULL) {
609 goto error; 608 goto error;
610 } 609 }
818 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected"); 817 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected");
819 py_clear_ref(&tmp); 818 py_clear_ref(&tmp);
820 goto error; 819 goto error;
821 } 820 }
822 /* Unpack the result tuple */ 821 /* Unpack the result tuple */
823 tmp2 = PyTuple_GetItem(tmp, 0); /* borrowed -- cannot fail */ 822 /* borrowed -- cannot fail -- need ownership */
824 Py_DECREF(varname); 823 varname = Py_NewRef(PyTuple_GetItem(tmp, 0));
825 varname = Py_NewRef(tmp2); tmp2 = NULL; 824 /* borrowed -- cannot fail -- want ownership */
826 /* borrowed -- cannot fail -- want own */
827 filters = Py_NewRef(PyTuple_GetItem(tmp, 1)); 825 filters = Py_NewRef(PyTuple_GetItem(tmp, 1));
828 py_clear_ref(&tmp); 826 py_clear_ref(&tmp);
829 827
830 tmp = PyObject_CallMethod( 828 tmp = PyObject_CallMethod(
831 config, "_getvar_s_with_cache_info", "O", varname); 829 config, "_getvar_s_with_cache_info", "O", varname);
861 py_clear_ref(&tmp); 859 py_clear_ref(&tmp);
862 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected"); 860 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected");
863 goto error; 861 goto error;
864 } 862 }
865 /* unpack the result */ 863 /* unpack the result */
866 /* borrowed -- but want own */ 864 /* borrowed -- cannot fail -- but want ownership */
867 varvalue = Py_NewRef(PyTuple_GetItem(tmp, 0)); 865 varvalue = Py_NewRef(PyTuple_GetItem(tmp, 0));
868 cacheable = PyObject_IsTrue(PyTuple_GetItem(tmp, 1)); 866 cacheable = PyObject_IsTrue(PyTuple_GetItem(tmp, 1));
869 py_clear_ref(&tmp); 867 py_clear_ref(&tmp);
870 } 868 }
871 869
951 PyObject *cache; 949 PyObject *cache;
952 950
953 Py_ssize_t s_len; 951 Py_ssize_t s_len;
954 Py_ssize_t start, rest, end; 952 Py_ssize_t start, rest, end;
955 PyObject *tmp; 953 PyObject *tmp;
956 PyObject *tmp2;
957 PyObject *result = NULL; 954 PyObject *result = NULL;
958 PyObject *varname = NULL; 955 PyObject *varname = NULL;
959 PyObject *varvalue = NULL; 956 PyObject *varvalue = NULL;
960 PyObject *filters = NULL; 957 PyObject *filters = NULL;
961 PyObject *err_type; 958 PyObject *err_type;
1018 } 1015 }
1019 if (PyList_Append(result, tmp) < 0) { 1016 if (PyList_Append(result, tmp) < 0) {
1020 py_clear_ref(&tmp); 1017 py_clear_ref(&tmp);
1021 goto error; 1018 goto error;
1022 } 1019 }
1023 py_clear_ref(&tmp); 1020 py_clear_ref(&tmp);
1024 } 1021 }
1025 end = PyUnicode_Find(s, sstate->ENDTOK, start+2, s_len, 1); 1022 end = PyUnicode_Find(s, sstate->ENDTOK, start+2, s_len, 1);
1026 if (end == -2) { 1023 if (end == -2) {
1027 goto error; 1024 goto error;
1028 } 1025 }
1044 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected"); 1041 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected");
1045 py_clear_ref(&tmp); 1042 py_clear_ref(&tmp);
1046 goto error; 1043 goto error;
1047 } 1044 }
1048 /* Unpack the result tuple */ 1045 /* Unpack the result tuple */
1049 tmp2 = PyTuple_GetItem(tmp, 0); /* borrowed -- cannot fail */ 1046 /* borrowed -- cannot fail -- need ownership */
1050 Py_DECREF(varname); 1047 varname = Py_NewRef(PyTuple_GetItem(tmp, 0));
1051 varname = Py_NewRef(tmp2); tmp2 = NULL; 1048 /* borrowed -- cannot fail -- need ownership */
1052 /* borrowed -- cannot fail -- need ownership */
1053 filters = Py_NewRef(PyTuple_GetItem(tmp, 1)); 1049 filters = Py_NewRef(PyTuple_GetItem(tmp, 1));
1054 py_clear_ref(&tmp); 1050 py_clear_ref(&tmp);
1055 1051
1056 tmp = PyObject_CallMethod( 1052 tmp = PyObject_CallMethod(
1057 config, "_getvar_s_with_cache_info", "O", varname); 1053 config, "_getvar_s_with_cache_info", "O", varname);
1086 py_clear_ref(&tmp); 1082 py_clear_ref(&tmp);
1087 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected"); 1083 PyErr_SetString(PyExc_TypeError, "tuple of size 2 expected");
1088 goto error; 1084 goto error;
1089 } 1085 }
1090 /* unpack the result */ 1086 /* unpack the result */
1091 /* borrowed -- but want own */ 1087 /* borrowed -- but want own */
1092 varvalue = Py_NewRef(PyTuple_GetItem(tmp, 0)); 1088 varvalue = Py_NewRef(PyTuple_GetItem(tmp, 0));
1093 cacheable = PyObject_IsTrue(PyTuple_GetItem(tmp, 1)); 1089 cacheable = PyObject_IsTrue(PyTuple_GetItem(tmp, 1));
1094 py_clear_ref(&tmp); 1090 py_clear_ref(&tmp);
1095 } 1091 }
1096 1092
1103 tmp = PyObject_CallMethod( 1099 tmp = PyObject_CallMethod(
1104 config, "_apply_filters", "OO", filters, varvalue); 1100 config, "_apply_filters", "OO", filters, varvalue);
1105 if (tmp == NULL) { 1101 if (tmp == NULL) {
1106 goto error; 1102 goto error;
1107 } 1103 }
1108 Py_DECREF(varvalue); 1104 py_transfer_owned(&varvalue, &tmp);
1109 varvalue = tmp; tmp = NULL;
1110 1105
1111 py_clear_ref(&filters); 1106 py_clear_ref(&filters);
1112 1107
1113 rest = end + 2; /* 2 == len(ENDTOK) */ 1108 rest = end + 2; /* 2 == len(ENDTOK) */
1114 1109
1115 /* 1110 /*
1116 * Dont apply and type conversions to the variable value if 1111 * Dont apply and type conversions to the variable value if
1117 * the whole `s` is just one expansion 1112 * the whole `s` is just one expansion
1118 */ 1113 */
1119 if ((start == 0) && (rest == s_len)) { 1114 if ((start == 0) && (rest == s_len)) {
1120 Py_DECREF(result); 1115 py_transfer_owned(&result, &varvalue);
1121 result = varvalue; varvalue = NULL;
1122 goto success; /* break out early */ 1116 goto success; /* break out early */
1123 } 1117 }
1124 1118
1125 /* Handle None like the empty string */ 1119 /* Handle None like the empty string */
1126 if (py_object_isnot(varvalue, Py_None)) { 1120 if (py_object_isnot(varvalue, Py_None)) {
1149 } 1143 }
1150 if (PyList_Append(result, tmp) < 0) { 1144 if (PyList_Append(result, tmp) < 0) {
1151 py_clear_ref(&tmp); 1145 py_clear_ref(&tmp);
1152 goto error; 1146 goto error;
1153 } 1147 }
1154 py_clear_ref(&tmp); 1148 py_clear_ref(&tmp);
1155 } 1149 }
1156 1150
1157 tmp = PyUnicode_Join(sstate->EMPTY_STR, result); 1151 tmp = PyUnicode_Join(sstate->EMPTY_STR, result);
1158 if (tmp == NULL) { 1152 if (tmp == NULL) {
1159 goto error; 1153 goto error;
1160 } 1154 }
1161 Py_DECREF(result); 1155 py_transfer_owned(&result, &tmp);
1162 result = tmp; tmp = NULL;
1163 1156
1164 success: 1157 success:
1165 if (use_cache) { 1158 if (use_cache) {
1166 if (PyDict_SetItem(cache, s, result) < 0) { 1159 if (PyDict_SetItem(cache, s, result) < 0) {
1167 PyErr_Clear(); /* clear any cache-related error */ 1160 PyErr_Clear(); /* clear any cache-related error */