Skip to content

Commit 44da93d

Browse files
committed
fix: do not use Py_BuildValue() for simpler cases
1 parent e003ffb commit 44da93d

3 files changed

Lines changed: 29 additions & 34 deletions

File tree

src/_igraph/edgeseqobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ PyObject* igraphmodule_EdgeSeq_get_attribute_values_mapping(igraphmodule_EdgeSeq
338338
/* Handle iterables and slices by calling the select() method */
339339
if (PySlice_Check(o) || PyObject_HasAttrString(o, "__iter__")) {
340340
PyObject *result, *args;
341-
args = Py_BuildValue("(O)", o);
341+
args = PyTuple_Pack(1, o);
342342

343343
if (!args) {
344344
return NULL;

src/_igraph/graphobject.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,7 @@ PyObject *igraphmodule_Graph_copy(igraphmodule_GraphObject * self)
385385
*/
386386
PyObject *igraphmodule_Graph_vcount(igraphmodule_GraphObject * self)
387387
{
388-
PyObject *result;
389-
result = Py_BuildValue("l", (long)igraph_vcount(&self->g));
390-
return result;
388+
return PyLong_FromLong(igraph_vcount(&self->g));
391389
}
392390

393391
/** \ingroup python_interface_graph
@@ -397,9 +395,7 @@ PyObject *igraphmodule_Graph_vcount(igraphmodule_GraphObject * self)
397395
*/
398396
PyObject *igraphmodule_Graph_ecount(igraphmodule_GraphObject * self)
399397
{
400-
PyObject *result;
401-
result = Py_BuildValue("l", (long)igraph_ecount(&self->g));
402-
return result;
398+
return PyLong_FromLong(igraph_ecount(&self->g));
403399
}
404400

405401
/** \ingroup python_interface_graph
@@ -938,7 +934,7 @@ PyObject *igraphmodule_Graph_density(igraphmodule_GraphObject * self,
938934
return NULL;
939935
}
940936

941-
return Py_BuildValue("d", (double)result);
937+
return PyFloat_FromDouble(result);
942938
}
943939

944940
/** \ingroup python_interface_graph
@@ -1319,7 +1315,7 @@ PyObject *igraphmodule_Graph_reciprocity(igraphmodule_GraphObject * self,
13191315
return NULL;
13201316
}
13211317

1322-
return Py_BuildValue("d", (double)result);
1318+
return PyFloat_FromDouble(result);
13231319
}
13241320

13251321
/** \ingroup python_interface_graph
@@ -1483,7 +1479,7 @@ PyObject *igraphmodule_Graph_get_eid(igraphmodule_GraphObject * self,
14831479
PyObject_IsTrue(directed), PyObject_IsTrue(error)))
14841480
return igraphmodule_handle_igraph_error();
14851481

1486-
return Py_BuildValue("l", (long)result);
1482+
return PyLong_FromLong(result);
14871483
}
14881484

14891485
/** \ingroup python_interface_graph
@@ -3700,7 +3696,7 @@ PyObject *igraphmodule_Graph_assortativity_nominal(igraphmodule_GraphObject *sel
37003696
return NULL;
37013697
}
37023698

3703-
return Py_BuildValue("d", (double)(res));
3699+
return PyFloat_FromDouble(res);
37043700
}
37053701

37063702
/** \ingroup python_interface_graph
@@ -3735,7 +3731,7 @@ PyObject *igraphmodule_Graph_assortativity(igraphmodule_GraphObject *self, PyObj
37353731
return NULL;
37363732
}
37373733

3738-
return Py_BuildValue("d", (double)(res));
3734+
return PyFloat_FromDouble(res);
37393735
}
37403736

37413737
/** \ingroup python_interface_graph
@@ -3756,7 +3752,7 @@ PyObject *igraphmodule_Graph_assortativity_degree(igraphmodule_GraphObject *self
37563752
return NULL;
37573753
}
37583754

3759-
return Py_BuildValue("d", (double)(res));
3755+
return PyFloat_FromDouble(res);
37603756
}
37613757

37623758
/** \ingroup python_interface_graph
@@ -4820,7 +4816,7 @@ PyObject *igraphmodule_Graph_edge_connectivity(igraphmodule_GraphObject *self,
48204816
PyObject *args, PyObject *kwds) {
48214817
static char *kwlist[] = { "source", "target", "checks", NULL };
48224818
PyObject *checks = Py_True;
4823-
long int source = -1, target = -1, result;
4819+
long int source = -1, target = -1;
48244820
igraph_integer_t res;
48254821

48264822
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|llO", kwlist,
@@ -4840,12 +4836,10 @@ PyObject *igraphmodule_Graph_edge_connectivity(igraphmodule_GraphObject *self,
48404836
}
48414837
} else {
48424838
PyErr_SetString(PyExc_ValueError, "if source or target is given, the other one must also be specified");
4843-
return NULL;
4839+
return NULL;
48444840
}
48454841

4846-
result = res;
4847-
4848-
return Py_BuildValue("l", result);
4842+
return PyLong_FromLong(res);
48494843
}
48504844

48514845
/** \ingroup python_interface_graph
@@ -6235,7 +6229,7 @@ PyObject *igraphmodule_Graph_transitivity_undirected(igraphmodule_GraphObject
62356229
{
62366230
static char *kwlist[] = { "mode", NULL };
62376231
igraph_real_t res;
6238-
PyObject *r, *mode_o = Py_None;
6232+
PyObject *mode_o = Py_None;
62396233
igraph_transitivity_mode_t mode = IGRAPH_TRANSITIVITY_NAN;
62406234

62416235
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &mode_o))
@@ -6250,8 +6244,7 @@ PyObject *igraphmodule_Graph_transitivity_undirected(igraphmodule_GraphObject
62506244
return NULL;
62516245
}
62526246

6253-
r = Py_BuildValue("d", (double)(res));
6254-
return r;
6247+
return PyFloat_FromDouble(res);
62556248
}
62566249

62576250
/** \ingroup python_interface_graph
@@ -6264,7 +6257,7 @@ PyObject *igraphmodule_Graph_transitivity_avglocal_undirected(igraphmodule_Graph
62646257
{
62656258
static char *kwlist[] = { "mode", NULL };
62666259
igraph_real_t res;
6267-
PyObject *r, *mode_o = Py_None;
6260+
PyObject *mode_o = Py_None;
62686261
igraph_transitivity_mode_t mode = IGRAPH_TRANSITIVITY_NAN;
62696262

62706263
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O", kwlist, &mode_o))
@@ -6278,8 +6271,7 @@ PyObject *igraphmodule_Graph_transitivity_avglocal_undirected(igraphmodule_Graph
62786271
return NULL;
62796272
}
62806273

6281-
r = Py_BuildValue("d", (double)(res));
6282-
return r;
6274+
return PyFloat_FromDouble(res);
62836275
}
62846276

62856277
/** \ingroup python_interface_graph
@@ -6409,7 +6401,7 @@ PyObject *igraphmodule_Graph_vertex_connectivity(igraphmodule_GraphObject *self,
64096401
PyObject *args, PyObject *kwds) {
64106402
static char *kwlist[] = { "source", "target", "checks", "neighbors", NULL };
64116403
PyObject *checks = Py_True, *neis = Py_None;
6412-
long int source = -1, target = -1, result;
6404+
long int source = -1, target = -1;
64136405
igraph_integer_t res;
64146406
igraph_vconn_nei_t neighbors = IGRAPH_VCONN_NEI_ERROR;
64156407

@@ -6435,10 +6427,11 @@ PyObject *igraphmodule_Graph_vertex_connectivity(igraphmodule_GraphObject *self,
64356427
return NULL;
64366428
}
64376429

6438-
if (!IGRAPH_FINITE(res)) return Py_BuildValue("d", (double)res);
6430+
if (!IGRAPH_FINITE(res)) {
6431+
return PyFloat_FromDouble(res);
6432+
}
64396433

6440-
result = (long)res;
6441-
return Py_BuildValue("l", result);
6434+
return PyLong_FromLong(res);
64426435
}
64436436

64446437
/**********************************************************************
@@ -9303,7 +9296,7 @@ PyObject *igraphmodule_Graph_count_isomorphisms_vf2(igraphmodule_GraphObject *se
93039296
if (edge_color1) { igraph_vector_int_destroy(edge_color1); free(edge_color1); }
93049297
if (edge_color2) { igraph_vector_int_destroy(edge_color2); free(edge_color2); }
93059298

9306-
return Py_BuildValue("l", (long)result);
9299+
return PyLong_FromLong(result);
93079300
}
93089301

93099302
/** \ingroup python_interface_graph
@@ -9640,7 +9633,7 @@ PyObject *igraphmodule_Graph_count_subisomorphisms_vf2(igraphmodule_GraphObject
96409633
if (edge_color1) { igraph_vector_int_destroy(edge_color1); free(edge_color1); }
96419634
if (edge_color2) { igraph_vector_int_destroy(edge_color2); free(edge_color2); }
96429635

9643-
return Py_BuildValue("l", (long)result);
9636+
return PyLong_FromLong(result);
96449637
}
96459638

96469639
/** \ingroup python_interface_graph
@@ -10334,7 +10327,8 @@ PyObject *igraphmodule_Graph_maxflow_value(igraphmodule_GraphObject * self,
1033410327
}
1033510328

1033610329
igraph_vector_destroy(&capacity_vector);
10337-
return Py_BuildValue("d", (double)result);
10330+
10331+
return PyFloat_FromDouble(result);
1033810332
}
1033910333

1034010334
/** \ingroup python_interface_graph
@@ -10614,7 +10608,8 @@ PyObject *igraphmodule_Graph_mincut_value(igraphmodule_GraphObject * self,
1061410608
}
1061510609

1061610610
igraph_vector_destroy(&capacity_vector);
10617-
return Py_BuildValue("d", (double)result);
10611+
10612+
return PyFloat_FromDouble(result);
1061810613
}
1061910614

1062010615
/** \ingroup python_interface_graph
@@ -11515,7 +11510,7 @@ PyObject *igraphmodule_Graph_modularity(igraphmodule_GraphObject *self,
1151511510
igraph_vector_destroy(weights); free(weights);
1151611511
}
1151711512

11518-
return Py_BuildValue("d", (double)modularity);
11513+
return PyFloat_FromDouble(modularity);
1151911514
}
1152011515

1152111516
/**

src/_igraph/vertexseqobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ PyObject* igraphmodule_VertexSeq_get_attribute_values_mapping(igraphmodule_Verte
324324
/* Handle iterables and slices by calling the select() method */
325325
if (PySlice_Check(o) || PyObject_HasAttrString(o, "__iter__")) {
326326
PyObject *result, *args;
327-
args = Py_BuildValue("(O)", o);
327+
args = PyTuple_Pack(1, o);
328328

329329
if (!args) {
330330
return NULL;

0 commit comments

Comments
 (0)