comparison configmix/_speedups.c @ 610:764d4185c76a

C-implementations of Configuration.getvarl_s() and Configuration.getvar()
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 12 Jan 2022 00:44:02 +0100
parents 9ad860d6ddc9
children db5a20f18030
comparison
equal deleted inserted replaced
609:9ad860d6ddc9 610:764d4185c76a
1255 } 1255 }
1256 return _fast_getvarl(config, path, namespace, default_, sstate); 1256 return _fast_getvarl(config, path, namespace, default_, sstate);
1257 } 1257 }
1258 1258
1259 1259
1260 static
1261 PyObject *
1262 fast_getvarl_s(PyObject *self, PyObject *args, PyObject *kwds)
1263 {
1264 static char *kwlist[] = {"config", "path", "namespace", "default", NULL};
1265
1266 PyObject *config;
1267 PyObject *path;
1268 PyObject *namespace = NULL;
1269 PyObject *default_ = NULL;
1270
1271 PyObject *res = NULL;
1272 PyObject *tmp;
1273
1274 struct speedups_state *sstate;
1275
1276 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO!|$OO", kwlist, &config, &PyTuple_Type, &path, &namespace, &default_)) {
1277
1278 return NULL;
1279 }
1280 sstate = PyModule_GetState(self);
1281 if (sstate == NULL) {
1282 PyErr_SetString(PyExc_RuntimeError, "no module state available");
1283 return NULL;
1284 }
1285
1286 tmp = _fast_getvarl(config, path, namespace, NULL, sstate);
1287 if (tmp == NULL) {
1288 goto handle_possible_keyerror;
1289 }
1290 res = PyObject_CallMethod(config, "substitute_variables_in_obj", "O", tmp);
1291 if (res == NULL) {
1292 py_clear_ref(&tmp);
1293 goto handle_possible_keyerror;
1294 }
1295 py_clear_ref(&tmp);
1296 return res;
1297
1298 handle_possible_keyerror:
1299 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1300 if ((default_ == NULL) || py_object_is(default_, sstate->MARKER)) {
1301 /* fall through */
1302 }
1303 else {
1304 PyErr_Clear();
1305 return Py_NewRef(default_);
1306 }
1307 }
1308 /* fall through */
1309
1310 error:
1311 return NULL;
1312 }
1313
1314
1260 /** 1315 /**
1261 * Combination of py_getvar_s and _py_getvar_s_with_cache_info 1316 * Combination of py_getvar_s and _py_getvar_s_with_cache_info
1262 */ 1317 */
1263 static 1318 static
1264 PyObject * 1319 PyObject *
1265 _fast_getvar_s(PyObject *config, PyObject *varname, PyObject *default_, PyObject *self, struct speedups_state *sstate, int *cacheable) 1320 _fast_getvar_s(PyObject *config, PyObject *varname, PyObject *default_, PyObject *self, struct speedups_state *sstate, int *cacheable)
1266 { 1321 {
1267 PyObject *varname_b; /* always borrowed */ 1322 PyObject *varname_b; /* always borrowed */
1268 PyObject *namespace_b; /* always borrowed */ 1323 PyObject *namespace_b; /* always borrowed */
1269 PyObject *splitted = NULL; 1324 PyObject *splitted = NULL;
1270 PyObject *res = NULL; 1325 PyObject *res;
1271 PyObject *tmp1; 1326 PyObject *tmp1;
1272 PyObject *tmp2; 1327 PyObject *tmp2;
1273 1328
1274 splitted = fast_split_ns(self, varname); 1329 splitted = fast_split_ns(self, varname);
1275 if (splitted == NULL) { 1330 if (splitted == NULL) {
1329 if ((default_ == NULL) || py_object_is(default_, sstate->MARKER)) { 1384 if ((default_ == NULL) || py_object_is(default_, sstate->MARKER)) {
1330 /* fall through */ 1385 /* fall through */
1331 } 1386 }
1332 else { 1387 else {
1333 PyErr_Clear(); 1388 PyErr_Clear();
1334 Py_XDECREF(res);
1335 Py_XDECREF(splitted); 1389 Py_XDECREF(splitted);
1336 return Py_NewRef(default_); 1390 return Py_NewRef(default_);
1337 } 1391 }
1338 } 1392 }
1339 /* fall through */ 1393 /* fall through */
1340 1394
1341 error: 1395 error:
1342 Py_XDECREF(res); 1396 Py_XDECREF(splitted);
1397 return NULL;
1398 }
1399
1400
1401 static
1402 PyObject *
1403 fast_getvar(PyObject *self, PyObject *args)
1404 {
1405 PyObject *config;
1406 PyObject *varname;
1407 PyObject *default_;
1408
1409 PyObject *varname_b; /* always borrowed */
1410 PyObject *namespace_b; /* always borrowed */
1411 PyObject *splitted = NULL;
1412 PyObject *res;
1413 PyObject *tmp1;
1414 struct speedups_state *sstate;
1415
1416 if (!PyArg_UnpackTuple(args, "config", 3, 3, &config, &varname, &default_)) {
1417 return NULL;
1418 }
1419
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 if (sstate == NULL) {
1429 PyErr_SetString(PyExc_RuntimeError, "no module state available");
1430 goto error;
1431 }
1432
1433 if (PyObject_Not(namespace_b)) {
1434 tmp1 = _fast_pathstr2path(varname_b, NULL, sstate);
1435 if (tmp1 == NULL) {
1436 goto error;
1437 }
1438 res = _fast_getvarl(config, tmp1, NULL, default_, sstate);
1439 if (res == NULL) {
1440 py_clear_ref(&tmp1);
1441 goto error;
1442 }
1443 py_clear_ref(&tmp1);
1444 }
1445 else {
1446 tmp1 = PyTuple_New(1);
1447 if (tmp1 == NULL) {
1448 goto error;
1449 }
1450 PyTuple_SetItem(tmp1, 0, Py_NewRef(varname_b));
1451 res = _fast_getvarl(config, tmp1, namespace_b, default_, sstate);
1452 if (res == NULL) {tmp1 = _fast_pathstr2path(varname_b, NULL, sstate);
1453 if (tmp1 == NULL) {
1454 goto error;
1455 }
1456 py_clear_ref(&tmp1);
1457 goto error;
1458 }
1459 py_clear_ref(&tmp1);
1460 }
1461 Py_DECREF(splitted);
1462 return res;
1463
1464 error:
1343 Py_XDECREF(splitted); 1465 Py_XDECREF(splitted);
1344 return NULL; 1466 return NULL;
1345 } 1467 }
1346 1468
1347 1469
1414 {"fast_pathstr2path", fast_pathstr2path, METH_O, PyDoc_STR("C-implementation of configmix.pathstr2path")}, 1536 {"fast_pathstr2path", fast_pathstr2path, METH_O, PyDoc_STR("C-implementation of configmix.pathstr2path")},
1415 {"_fast_split_filters", fast_split_filters, METH_O, PyDoc_STR("C-implementation of configmix.config._split_filters")}, 1537 {"_fast_split_filters", fast_split_filters, METH_O, PyDoc_STR("C-implementation of configmix.config._split_filters")},
1416 {"_fast_split_ns", fast_split_ns, METH_O, PyDoc_STR("C-implementation of configmix.config._split_ns")}, 1538 {"_fast_split_ns", fast_split_ns, METH_O, PyDoc_STR("C-implementation of configmix.config._split_ns")},
1417 {"_fast_interpolate_variables", fast_interpolate_variables, METH_VARARGS, PyDoc_STR("C-implementation of configmix.config.Configuration.interpolate_variables")}, 1539 {"_fast_interpolate_variables", fast_interpolate_variables, METH_VARARGS, PyDoc_STR("C-implementation of configmix.config.Configuration.interpolate_variables")},
1418 {"_fast_getvarl", (PyCFunction)fast_getvarl, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("C-Implementation of configmix.config.Configuration.getvarl")}, 1540 {"_fast_getvarl", (PyCFunction)fast_getvarl, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("C-Implementation of configmix.config.Configuration.getvarl")},
1419 {"_fast_getvar_s", fast_getvar_s, METH_VARARGS, PyDoc_STR("C-Implementation of configmix.config.Configuration.getvar_s")}, 1541 {"_fast_getvarl_s", (PyCFunction)fast_getvarl_s, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("C-Implementation of configmix.config.Configuration.getvarl_s")},
1542 {"_fast_getvar", fast_getvar, METH_VARARGS, PyDoc_STR("C-Implementation of configmix.config.Configuration.getvar")},
1543 {"_fast_getvar_s", fast_getvar_s, METH_VARARGS, PyDoc_STR("C-Implementation of configmix.config.Configuration.getvar_s")},
1420 {"_sync_MISSING", sync_MISSING, METH_O, PyDoc_STR("Internal function to easily sync the _MISSING object with configmix.config")}, 1544 {"_sync_MISSING", sync_MISSING, METH_O, PyDoc_STR("Internal function to easily sync the _MISSING object with configmix.config")},
1421 {"_sync_MARKER", sync_MARKER, METH_O, PyDoc_STR("Internal function to easily sync the _MARKER object with configmix.config")}, 1545 {"_sync_MARKER", sync_MARKER, METH_O, PyDoc_STR("Internal function to easily sync the _MARKER object with configmix.config")},
1422 {NULL, NULL, 0, NULL} 1546 {NULL, NULL, 0, NULL}
1423 }; 1547 };
1424 1548