comparison configmix/_speedups.c @ 611:db5a20f18030

Provide an internal version of fast_split_ns() to use an already module state available
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 12 Jan 2022 01:04:24 +0100
parents 764d4185c76a
children f797f798edfb
comparison
equal deleted inserted replaced
610:764d4185c76a 611:db5a20f18030
35 PyObject *ENDTOK; 35 PyObject *ENDTOK;
36 PyObject *REF_NAMESPACE; 36 PyObject *REF_NAMESPACE;
37 }; 37 };
38 38
39 39
40 static PyObject * _fast_getvar_s(PyObject *, PyObject *, PyObject *, PyObject *, struct speedups_state *, int*); 40 static PyObject * _fast_getvar_s(PyObject *, PyObject *, PyObject *, struct speedups_state *, int*);
41 41
42 42
43 static 43 static
44 int 44 int
45 _hex2ucs4(PyObject *s, Py_ssize_t end, Py_UCS4 *result) 45 _hex2ucs4(PyObject *s, Py_ssize_t end, Py_UCS4 *result)
642 } 642 }
643 643
644 644
645 static 645 static
646 PyObject * 646 PyObject *
647 fast_split_ns(PyObject *self, PyObject *varname) 647 _fast_split_ns(PyObject *varname, PyObject *self, struct speedups_state *sstate)
648 { 648 {
649 PyObject *res = NULL; 649 PyObject *res = NULL;
650 Py_ssize_t ns_idx; 650 Py_ssize_t ns_idx;
651 Py_ssize_t varname_len; 651 Py_ssize_t varname_len;
652 PyObject *o1; 652 PyObject *o1;
677 o1 = PyUnicode_Substring(varname, 0, ns_idx); 677 o1 = PyUnicode_Substring(varname, 0, ns_idx);
678 if (o1 == NULL) { 678 if (o1 == NULL) {
679 Py_DECREF(res); 679 Py_DECREF(res);
680 return NULL; 680 return NULL;
681 } 681 }
682 o2 = _fast_unquote(o1, ns_idx, self, NULL); 682 o2 = _fast_unquote(o1, ns_idx, self, sstate);
683 if (o2 == NULL) { 683 if (o2 == NULL) {
684 Py_DECREF(o1); 684 Py_DECREF(o1);
685 Py_DECREF(res); 685 Py_DECREF(res);
686 return NULL; 686 return NULL;
687 } 687 }
692 Py_DECREF(res); 692 Py_DECREF(res);
693 return NULL; 693 return NULL;
694 } 694 }
695 PyTuple_SetItem(res, 1, o1); /* steals */ 695 PyTuple_SetItem(res, 1, o1); /* steals */
696 return res; 696 return res;
697 }
698
699
700 static
701 PyObject *
702 fast_split_ns(PyObject *self, PyObject *varname)
703 {
704 return _fast_split_ns(varname, self, NULL);
697 } 705 }
698 706
699 707
700 static 708 static
701 PyObject * 709 PyObject *
838 varname = Py_NewRef(PyTuple_GetItem(tmp, 0)); 846 varname = Py_NewRef(PyTuple_GetItem(tmp, 0));
839 /* borrowed -- cannot fail -- want ownership */ 847 /* borrowed -- cannot fail -- want ownership */
840 filters = Py_NewRef(PyTuple_GetItem(tmp, 1)); 848 filters = Py_NewRef(PyTuple_GetItem(tmp, 1));
841 py_clear_ref(&tmp); 849 py_clear_ref(&tmp);
842 850
843 varvalue = _fast_getvar_s(config, varname, NULL, self, sstate, &cacheable); 851 varvalue = _fast_getvar_s(config, varname, NULL, sstate, &cacheable);
844 if (varvalue == NULL) { 852 if (varvalue == NULL) {
845 if (PyErr_ExceptionMatches(PyExc_KeyError)) { 853 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
846 cacheable = 1; 854 cacheable = 1;
847 if (PySequence_Contains(filters, sstate->NONE_FILTER) == 1) { 855 if (PySequence_Contains(filters, sstate->NONE_FILTER) == 1) {
848 PyErr_Clear(); 856 PyErr_Clear();
1055 varname = Py_NewRef(PyTuple_GetItem(tmp, 0)); 1063 varname = Py_NewRef(PyTuple_GetItem(tmp, 0));
1056 /* borrowed -- cannot fail -- need ownership */ 1064 /* borrowed -- cannot fail -- need ownership */
1057 filters = Py_NewRef(PyTuple_GetItem(tmp, 1)); 1065 filters = Py_NewRef(PyTuple_GetItem(tmp, 1));
1058 py_clear_ref(&tmp); 1066 py_clear_ref(&tmp);
1059 1067
1060 varvalue = _fast_getvar_s(config, varname, NULL, self, sstate, &cacheable); 1068 varvalue = _fast_getvar_s(config, varname, NULL, sstate, &cacheable);
1061 1069
1062 if (varvalue == NULL) { 1070 if (varvalue == NULL) {
1063 if (PyErr_ExceptionMatches(PyExc_KeyError)) { 1071 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1064 cacheable = 1; 1072 cacheable = 1;
1065 if (PySequence_Contains(filters, sstate->NONE_FILTER) == 1) { 1073 if (PySequence_Contains(filters, sstate->NONE_FILTER) == 1) {
1315 /** 1323 /**
1316 * Combination of py_getvar_s and _py_getvar_s_with_cache_info 1324 * Combination of py_getvar_s and _py_getvar_s_with_cache_info
1317 */ 1325 */
1318 static 1326 static
1319 PyObject * 1327 PyObject *
1320 _fast_getvar_s(PyObject *config, PyObject *varname, PyObject *default_, PyObject *self, struct speedups_state *sstate, int *cacheable) 1328 _fast_getvar_s(PyObject *config, PyObject *varname, PyObject *default_, struct speedups_state *sstate, int *cacheable)
1321 { 1329 {
1322 PyObject *varname_b; /* always borrowed */ 1330 PyObject *varname_b; /* always borrowed */
1323 PyObject *namespace_b; /* always borrowed */ 1331 PyObject *namespace_b; /* always borrowed */
1324 PyObject *splitted = NULL; 1332 PyObject *splitted = NULL;
1325 PyObject *res; 1333 PyObject *res;
1326 PyObject *tmp1; 1334 PyObject *tmp1;
1327 PyObject *tmp2; 1335 PyObject *tmp2;
1328 1336
1329 splitted = fast_split_ns(self, varname); 1337 splitted = _fast_split_ns(varname, NULL, sstate);
1330 if (splitted == NULL) { 1338 if (splitted == NULL) {
1331 goto error; 1339 goto error;
1332 } 1340 }
1333 namespace_b = PyTuple_GetItem(splitted, 0); /* borrowed */ 1341 namespace_b = PyTuple_GetItem(splitted, 0); /* borrowed */
1334 varname_b = PyTuple_GetItem(splitted, 1); /* borrowed */ 1342 varname_b = PyTuple_GetItem(splitted, 1); /* borrowed */
1415 1423
1416 if (!PyArg_UnpackTuple(args, "config", 3, 3, &config, &varname, &default_)) { 1424 if (!PyArg_UnpackTuple(args, "config", 3, 3, &config, &varname, &default_)) {
1417 return NULL; 1425 return NULL;
1418 } 1426 }
1419 1427
1420 splitted = fast_split_ns(self, varname);
1421 if (splitted == NULL) {
1422 goto error;
1423 }
1424 namespace_b = PyTuple_GetItem(splitted, 0); /* borrowed */
1425 varname_b = PyTuple_GetItem(splitted, 1); /* borrowed */
1426
1427 sstate = PyModule_GetState(self); 1428 sstate = PyModule_GetState(self);
1428 if (sstate == NULL) { 1429 if (sstate == NULL) {
1429 PyErr_SetString(PyExc_RuntimeError, "no module state available"); 1430 PyErr_SetString(PyExc_RuntimeError, "no module state available");
1430 goto error; 1431 return NULL;
1431 } 1432 }
1433
1434 splitted = _fast_split_ns(varname, NULL, sstate);
1435 if (splitted == NULL) {
1436 goto error;
1437 }
1438 namespace_b = PyTuple_GetItem(splitted, 0); /* borrowed */
1439 varname_b = PyTuple_GetItem(splitted, 1); /* borrowed */
1432 1440
1433 if (PyObject_Not(namespace_b)) { 1441 if (PyObject_Not(namespace_b)) {
1434 tmp1 = _fast_pathstr2path(varname_b, NULL, sstate); 1442 tmp1 = _fast_pathstr2path(varname_b, NULL, sstate);
1435 if (tmp1 == NULL) { 1443 if (tmp1 == NULL) {
1436 goto error; 1444 goto error;
1484 sstate = PyModule_GetState(self); 1492 sstate = PyModule_GetState(self);
1485 if (sstate == NULL) { 1493 if (sstate == NULL) {
1486 PyErr_SetString(PyExc_RuntimeError, "no module state available"); 1494 PyErr_SetString(PyExc_RuntimeError, "no module state available");
1487 return NULL; 1495 return NULL;
1488 } 1496 }
1489 return _fast_getvar_s(config, varname, default_, self, sstate, &cacheable); 1497 return _fast_getvar_s(config, varname, default_, sstate, &cacheable);
1490 } 1498 }
1491 1499
1492 1500
1493 static 1501 static
1494 PyObject * 1502 PyObject *