Skip to content

Commit 385615a

Browse files
committed
chore: Fix autogeneration for writing functions
1 parent 6be3bf9 commit 385615a

File tree

7 files changed

+78
-37
lines changed

7 files changed

+78
-37
lines changed

R/aaa-auto.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6532,6 +6532,7 @@ read_graph_pajek_impl <- function(
65326532
instream
65336533
) {
65346534
# Argument checks
6535+
check_string(instream)
65356536

65366537

65376538
on.exit(.Call(R_igraph_finalizer))
@@ -6549,6 +6550,8 @@ read_graph_graphml_impl <- function(
65496550
index = 0
65506551
) {
65516552
# Argument checks
6553+
check_string(instream)
6554+
65526555
index <- as.numeric(index)
65536556

65546557
on.exit(.Call(R_igraph_finalizer))
@@ -6567,6 +6570,8 @@ read_graph_graphdb_impl <- function(
65676570
directed = FALSE
65686571
) {
65696572
# Argument checks
6573+
check_string(instream)
6574+
65706575
directed <- as.logical(directed)
65716576

65726577
on.exit(.Call(R_igraph_finalizer))
@@ -6584,6 +6589,7 @@ read_graph_gml_impl <- function(
65846589
instream
65856590
) {
65866591
# Argument checks
6592+
check_string(instream)
65876593

65886594

65896595
on.exit(.Call(R_igraph_finalizer))
@@ -6601,6 +6607,8 @@ read_graph_dl_impl <- function(
66016607
directed = TRUE
66026608
) {
66036609
# Argument checks
6610+
check_string(instream)
6611+
66046612
directed <- as.logical(directed)
66056613

66066614
on.exit(.Call(R_igraph_finalizer))

src/rinterface.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9472,7 +9472,8 @@ SEXP R_igraph_read_graph_pajek(SEXP instream) {
94729472

94739473
SEXP r_result;
94749474
/* Convert input */
9475-
9475+
c_instream = R_igraph_fopen_read(instream);
9476+
IGRAPH_FINALLY(fclose, c_instream);
94769477
/* Call igraph */
94779478
IGRAPH_R_CHECK(igraph_read_graph_pajek(&c_graph, c_instream));
94789479

@@ -9499,6 +9500,8 @@ SEXP R_igraph_read_graph_graphml(SEXP instream, SEXP index) {
94999500

95009501
SEXP r_result;
95019502
/* Convert input */
9503+
c_instream = R_igraph_fopen_read(instream);
9504+
IGRAPH_FINALLY(fclose, c_instream);
95029505
IGRAPH_R_CHECK_INT(index);
95039506
c_index = (igraph_integer_t) REAL(index)[0];
95049507
/* Call igraph */
@@ -9527,6 +9530,8 @@ SEXP R_igraph_read_graph_graphdb(SEXP instream, SEXP directed) {
95279530

95289531
SEXP r_result;
95299532
/* Convert input */
9533+
c_instream = R_igraph_fopen_read(instream);
9534+
IGRAPH_FINALLY(fclose, c_instream);
95309535
IGRAPH_R_CHECK_BOOL(directed);
95319536
c_directed = LOGICAL(directed)[0];
95329537
/* Call igraph */
@@ -9554,7 +9559,8 @@ SEXP R_igraph_read_graph_gml(SEXP instream) {
95549559

95559560
SEXP r_result;
95569561
/* Convert input */
9557-
9562+
c_instream = R_igraph_fopen_read(instream);
9563+
IGRAPH_FINALLY(fclose, c_instream);
95589564
/* Call igraph */
95599565
IGRAPH_R_CHECK(igraph_read_graph_gml(&c_graph, c_instream));
95609566

@@ -9581,6 +9587,8 @@ SEXP R_igraph_read_graph_dl(SEXP instream, SEXP directed) {
95819587

95829588
SEXP r_result;
95839589
/* Convert input */
9590+
c_instream = R_igraph_fopen_read(instream);
9591+
IGRAPH_FINALLY(fclose, c_instream);
95849592
IGRAPH_R_CHECK_BOOL(directed);
95859593
c_directed = LOGICAL(directed)[0];
95869594
/* Call igraph */
@@ -9604,8 +9612,7 @@ SEXP R_igraph_write_graph_edgelist(SEXP graph, SEXP outstream) {
96049612
/* Declarations */
96059613
igraph_t c_graph;
96069614
FILE* c_outstream;
9607-
igraph_error_t c_result;
9608-
SEXP r_result;
9615+
96099616
/* Convert input */
96109617
R_SEXP_to_igraph(graph, &c_graph);
96119618
/* Call igraph */
@@ -9615,8 +9622,7 @@ SEXP R_igraph_write_graph_edgelist(SEXP graph, SEXP outstream) {
96159622

96169623

96179624

9618-
UNPROTECT(1);
9619-
return(r_result);
9625+
return(R_NilValue);
96209626
}
96219627

96229628
/*-------------------------------------------/
@@ -9628,8 +9634,7 @@ SEXP R_igraph_write_graph_leda(SEXP graph, SEXP outstream, SEXP names, SEXP weig
96289634
FILE* c_outstream;
96299635
const char* c_names;
96309636
const char* c_weights;
9631-
igraph_error_t c_result;
9632-
SEXP r_result;
9637+
96339638
/* Convert input */
96349639
R_SEXP_to_igraph(graph, &c_graph);
96359640
c_names = Rf_translateCharUTF8(STRING_ELT(names, 0));
@@ -9641,8 +9646,7 @@ SEXP R_igraph_write_graph_leda(SEXP graph, SEXP outstream, SEXP names, SEXP weig
96419646

96429647

96439648

9644-
UNPROTECT(1);
9645-
return(r_result);
9649+
return(R_NilValue);
96469650
}
96479651

96489652
/*-------------------------------------------/
@@ -9653,8 +9657,7 @@ SEXP R_igraph_write_graph_graphml(SEXP graph, SEXP outstream, SEXP prefixattr) {
96539657
igraph_t c_graph;
96549658
FILE* c_outstream;
96559659
igraph_bool_t c_prefixattr;
9656-
igraph_error_t c_result;
9657-
SEXP r_result;
9660+
96589661
/* Convert input */
96599662
R_SEXP_to_igraph(graph, &c_graph);
96609663
IGRAPH_R_CHECK_BOOL(prefixattr);
@@ -9666,8 +9669,7 @@ SEXP R_igraph_write_graph_graphml(SEXP graph, SEXP outstream, SEXP prefixattr) {
96669669

96679670

96689671

9669-
UNPROTECT(1);
9670-
return(r_result);
9672+
return(R_NilValue);
96719673
}
96729674

96739675
/*-------------------------------------------/
@@ -9677,8 +9679,7 @@ SEXP R_igraph_write_graph_pajek(SEXP graph, SEXP outstream) {
96779679
/* Declarations */
96789680
igraph_t c_graph;
96799681
FILE* c_outstream;
9680-
igraph_error_t c_result;
9681-
SEXP r_result;
9682+
96829683
/* Convert input */
96839684
R_SEXP_to_igraph(graph, &c_graph);
96849685
/* Call igraph */
@@ -9688,8 +9689,7 @@ SEXP R_igraph_write_graph_pajek(SEXP graph, SEXP outstream) {
96889689

96899690

96909691

9691-
UNPROTECT(1);
9692-
return(r_result);
9692+
return(R_NilValue);
96939693
}
96949694

96959695
/*-------------------------------------------/
@@ -9702,8 +9702,7 @@ SEXP R_igraph_write_graph_gml(SEXP graph, SEXP outstream, SEXP options, SEXP id,
97029702
igraph_write_gml_sw_t c_options;
97039703
igraph_vector_t c_id;
97049704
const char* c_creator;
9705-
igraph_error_t c_result;
9706-
SEXP r_result;
9705+
97079706
/* Convert input */
97089707
R_SEXP_to_igraph(graph, &c_graph);
97099708
c_options = (igraph_write_gml_sw_t) Rf_asInteger(options);
@@ -9718,8 +9717,7 @@ SEXP R_igraph_write_graph_gml(SEXP graph, SEXP outstream, SEXP options, SEXP id,
97189717

97199718

97209719

9721-
UNPROTECT(1);
9722-
return(r_result);
9720+
return(R_NilValue);
97239721
}
97249722

97259723
/*-------------------------------------------/
@@ -9729,8 +9727,7 @@ SEXP R_igraph_write_graph_dot(SEXP graph, SEXP outstream) {
97299727
/* Declarations */
97309728
igraph_t c_graph;
97319729
FILE* c_outstream;
9732-
igraph_error_t c_result;
9733-
SEXP r_result;
9730+
97349731
/* Convert input */
97359732
R_SEXP_to_igraph(graph, &c_graph);
97369733
/* Call igraph */
@@ -9740,8 +9737,7 @@ SEXP R_igraph_write_graph_dot(SEXP graph, SEXP outstream) {
97409737

97419738

97429739

9743-
UNPROTECT(1);
9744-
return(r_result);
9740+
return(R_NilValue);
97459741
}
97469742

97479743
/*-------------------------------------------/

src/rinterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ void igraph_vector_int_list_destroy_pv(void *pv_ptr);
145145
void R_check_int_scalar(SEXP value);
146146
void R_check_real_scalar(SEXP value);
147147
void R_check_bool_scalar(SEXP value);
148+
FILE* R_igraph_fopen_read(SEXP instream);
148149

149150
igraph_error_t R_get_int_scalar(SEXP sexp, R_xlen_t index, igraph_integer_t *res);
150151
igraph_error_t R_get_real_scalar(SEXP sexp, R_xlen_t index, igraph_real_t *res);

src/rinterface_extra.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ igraph_error_t R_get_bool_scalar(SEXP sexp, R_xlen_t index, igraph_bool_t *res)
162162
return IGRAPH_SUCCESS;
163163
}
164164

165+
FILE* R_igraph_fopen_read(SEXP instream) {
166+
FILE *file;
167+
168+
file=fopen(CHAR(STRING_ELT(instream, 0)), "r");
169+
if (file==0) { igraph_error("Cannot open file for reading", __FILE__, __LINE__,
170+
IGRAPH_EFILE); }
171+
return file;
172+
}
173+
165174
SEXP R_igraph_i_lang7(SEXP s, SEXP t, SEXP u, SEXP v, SEXP w, SEXP x, SEXP y)
166175
{
167176
PROTECT(s);

tools/py-stimulus/src/stimulus/generators/r.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ def generate_function(self, function: str, out: IO[str]) -> None:
457457
res["inconv"] = self.chunk_inconv(desc)
458458
res["call"] = self.chunk_call(desc)
459459
res["outconv"] = self.chunk_outconv(desc)
460+
res["ret"] = self.chunk_ret(desc)
460461

461462
# Replace into the template
462463
text = (
@@ -474,8 +475,7 @@ def generate_function(self, function: str, out: IO[str]) -> None:
474475
/* Convert output */
475476
%(outconv)s
476477
477-
UNPROTECT(1);
478-
return(r_result);
478+
%(ret)s
479479
}\n"""
480480
% res
481481
)
@@ -501,6 +501,8 @@ def do_par(spec: ParamSpec) -> str:
501501

502502
inout = [do_par(spec) for spec in desc.iter_input_parameters()]
503503
inout = ["SEXP " + n for n in inout if n != ""]
504+
505+
# cpp11 requires an SEXP return value
504506
return "SEXP R_" + desc.name + "(" + (", ".join(inout) or "void") + ")"
505507

506508
def chunk_declaration(self, desc: FunctionDescriptor) -> str:
@@ -531,9 +533,14 @@ def do_par(spec: ParamSpec) -> str:
531533
retpars = [spec.name for spec in desc.iter_output_parameters()]
532534

533535
return_type_desc = self.get_type_descriptor(desc.return_type)
534-
retdecl = return_type_desc.declare_c_variable("c_result") if not retpars else ""
536+
if retpars or return_type_desc.name in ("VOID", "ERROR"):
537+
retdecl = ""
538+
else:
539+
retdecl = return_type_desc.declare_c_variable("c_result")
535540

536-
if len(retpars) <= 1:
541+
if len(retpars) == 0 and desc.return_type == "ERROR":
542+
res = "\n".join(inout + out + [retdecl])
543+
elif len(retpars) <= 1:
537544
res = "\n".join(inout + out + [retdecl] + ["SEXP r_result;"])
538545
else:
539546
res = "\n".join(inout + out + [retdecl] + ["SEXP r_result, r_names;"])
@@ -620,7 +627,7 @@ def chunk_call(self, desc: FunctionDescriptor) -> str:
620627

621628
return res
622629

623-
def chunk_outconv(self, spec: FunctionDescriptor) -> str:
630+
def chunk_outconv(self, desc: FunctionDescriptor) -> str:
624631
"""The output conversions, this is quite difficult. A function
625632
may report its results in two ways: by returning it directly
626633
or by setting a variable to which a pointer was passed. igraph
@@ -655,20 +662,20 @@ def do_par(param: ParamSpec) -> str:
655662

656663
return outconv.replace("%C%", cname).replace("%I%", param.name)
657664

658-
outconv = [do_par(param) for param in spec.iter_parameters()]
665+
outconv = [do_par(param) for param in desc.iter_parameters()]
659666
outconv = [o for o in outconv if o != ""]
660667

661668
# Consider only those parameters that have a corresponding declaration
662669
# in C.
663670
retpars = []
664-
for param in spec.iter_output_parameters():
671+
for param in desc.iter_output_parameters():
665672
type_desc = self.get_type_descriptor(param.type)
666673
if type_desc.get_c_type(param.mode) is not None:
667674
retpars.append(param)
668675

669676
if not retpars:
670677
# return the return value of the function
671-
rt = self.get_type_descriptor(spec.return_type)
678+
rt = self.get_type_descriptor(desc.return_type)
672679
retconv = indent(rt.get_output_conversion_template_for(ParamMode.OUT))
673680
retconv = retconv.replace("%C%", "c_result").replace("%I%", "r_result")
674681
ret = "\n".join(outconv) + "\n" + retconv
@@ -699,6 +706,16 @@ def do_par(param: ParamSpec) -> str:
699706

700707
return ret
701708

709+
def chunk_ret(self, desc: FunctionDescriptor) -> str:
710+
"""Create the return statement. Only unprotect and return if one or more output arguments.
711+
"""
712+
713+
if len(list(desc.iter_output_parameters())) == 0 and desc.return_type == "ERROR":
714+
# cpp11 requires an SEXP return value
715+
return " return(R_NilValue);"
716+
717+
return " UNPROTECT(1);\n return(r_result);"
718+
702719

703720
class RInitCodeGenerator(BlockBasedCodeGenerator):
704721
def _count_arguments(self, name: str) -> Tuple[int, int]:

tools/stimulus/types-RC.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ CSTRING:
7474
OUT: |-
7575
PROTECT(%I% = Rf_ScalarString(Rf_mkCharLenCE(%C%, strlen(%C%), CE_UTF8)));
7676
77+
INFILE:
78+
CTYPE: FILE*
79+
CALL:
80+
IN: '%C%'
81+
INCONV:
82+
IN: |-
83+
%C% = R_igraph_fopen_read(%I%);
84+
IGRAPH_FINALLY(fclose, %C%);
85+
7786
REAL:
7887
CTYPE: igraph_real_t
7988
CALL:
@@ -104,9 +113,6 @@ BOOLEAN:
104113
PROTECT(%I%=NEW_LOGICAL(1));
105114
LOGICAL(%I%)[0]=%C%;
106115
107-
ERROR:
108-
CTYPE: igraph_error_t
109-
110116
INDEX_VECTOR:
111117
CALL: '&%C%'
112118
CTYPE: igraph_vector_int_t

tools/stimulus/types-RR.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
GRAPH:
33
INCONV: ensure_igraph(%I%)
44

5+
INFILE:
6+
INCONV: |
7+
check_string(%I%)
8+
59
INTEGER:
610
DEFAULT:
711
FINEITER: max(10, log2(vcount(graph)))

0 commit comments

Comments
 (0)