Browse Source

Enhancement in method declaration

Write a macro allowing to "declare" method header's argument list
Yann Weber 1 year ago
parent
commit
f3b8cc817c
8 changed files with 106 additions and 72 deletions
  1. 10
    0
      config.h
  2. 34
    20
      python_if.c
  3. 0
    6
      python_if.h
  4. 9
    5
      python_pyrpn.c
  5. 1
    3
      python_pyrpn.h
  6. 39
    23
      python_rpnexpr.c
  7. 0
    6
      python_rpnexpr.h
  8. 13
    9
      python_rpntoken.c

+ 10
- 0
config.h View File

@@ -18,5 +18,15 @@
18 18
  */
19 19
 #ifndef __RPN_CONFIG__
20 20
 #define __RPN_CONFIG__
21
+
21 22
 #define _GNU_SOURCE
23
+
24
+
25
+#define PYRPN_doc_meth(NAME, header, docstring) PyDoc_STRVAR(NAME ## _doc,\
26
+		header "\n--\n\n" docstring);
27
+
28
+#define PYRPN_method(name, callback, flags, header, docstring) \
29
+{name, (PyCFunction)callback, flags, \
30
+	PyDoc_STR(name "("header ")\n--\n\n" docstring)}
31
+
22 32
 #endif

+ 34
- 20
python_if.c View File

@@ -1,43 +1,57 @@
1 1
 #include "python_if.h"
2 2
 
3
-PyMethodDef RPNIterExpr_methods[] = {
4
-	{"get_params", (PyCFunction)rpnif_get_params, METH_NOARGS,
5
-		"Get a named tuple with parameters"},
6
-	{"keys", (PyCFunction)rpnif_keys, METH_NOARGS,
7
-		"Get the list of keys identifing the expressions"},
8
-	{"values", (PyCFunction)rpnif_values, METH_NOARGS,
9
-		"Get the list of expressions"},
10
-	{"items", (PyCFunction)rpnif_items, METH_NOARGS,
11
-		"Iterate on couple of (key, expr)"},
12
-	{"step", (PyCFunction)rpnif_step, METH_O,
13
-		"Run an IF given a position and return the new position"},
14
-	{"__getstate__", (PyCFunction)rpnif_getstate, METH_NOARGS,
15
-		"Pickling method. Return a bytes repr of tokenized expression \
16
-and the stack state."},
17
-	{"__setstate__", (PyCFunction)rpnif_setstate, METH_O,
18
-		"Unpickling method"},
3
+static PyMethodDef RPNIterExpr_methods[] = {
4
+	PYRPN_method("get_params", rpnif_get_params,
5
+			METH_NOARGS,
6
+			"self, /",
7
+			"Get a name tuple with parameters"),
8
+	PYRPN_method("keys", rpnif_keys,
9
+			METH_NOARGS,
10
+			"self, /",
11
+			"Return items keys (see dict.keys)"),
12
+	PYRPN_method("values", rpnif_values,
13
+			METH_NOARGS,
14
+			"self, /",
15
+			"Return items values (see dict.values)"),
16
+	PYRPN_method("items", rpnif_items,
17
+			METH_NOARGS,
18
+			"self, /",
19
+			"Return items (key, value) list (see dict.items)"),
20
+	PYRPN_method("step", rpnif_step,
21
+			METH_O,
22
+			"self, position, /",
23
+			"Run an IF given a position and return a new position"),
24
+	PYRPN_method("__getstate__", rpnif_getstate,
25
+			METH_NOARGS,
26
+			"self, /",
27
+			"Pickling method (see pickle module).\n"
28
+			"Return a bytes representation of the expression state."),
29
+	PYRPN_method("__setstate__", rpnif_setstate,
30
+			METH_O,
31
+			"self, state, /",
32
+			"Unpickling method (see pickle module)."),
19 33
 	{NULL} //Sentinel
20 34
 };
21 35
 
22
-PyMemberDef RPNIterExpr_members[] = {
36
+static PyMemberDef RPNIterExpr_members[] = {
23 37
 	{"expressions", T_OBJECT, offsetof(PyRPNIterExpr_t, expr), READONLY,
24 38
 		"The tuple of expressions"},
25 39
 	{NULL}
26 40
 };
27 41
 
28
-PySequenceMethods RPNIterExpr_seq_methods = {
42
+static PySequenceMethods RPNIterExpr_seq_methods = {
29 43
 	.sq_length = rpnif_len,
30 44
 	.sq_item = rpnif_expr_item,
31 45
 	.sq_ass_item = rpnif_expr_ass_item,
32 46
 };
33 47
 
34
-PyMappingMethods RPNIterExpr_mapping_methods = {
48
+static PyMappingMethods RPNIterExpr_mapping_methods = {
35 49
 	.mp_length = rpnif_len,
36 50
 	.mp_subscript = rpnif_subscript,
37 51
 	.mp_ass_subscript = rpnif_ass_subscript,
38 52
 };
39 53
 
40
-PyGetSetDef RPNIterExpr_getset[] = {
54
+static PyGetSetDef RPNIterExpr_getset[] = {
41 55
 	{NULL}
42 56
 };
43 57
 

+ 0
- 6
python_if.h View File

@@ -50,12 +50,6 @@
50 50
  * This file is the header of the RPNIterExpr Python class
51 51
  */
52 52
 
53
-/**@brief RPNIterExpr Python class methods list
54
- * @ingroup python_if */
55
-extern PyMethodDef RPNIterExpr_methods[];
56
-/**@brief RPNIterExpr Python class members list
57
- * @ingroup python_if */
58
-extern PyMemberDef RPNIterExpr_members[];
59 53
 /**@brief RPNIterExpr Python class type definition
60 54
  * @ingroup python_if */
61 55
 extern PyTypeObject RPNIterExprType;

+ 9
- 5
python_pyrpn.c View File

@@ -25,11 +25,15 @@
25 25
  * This file contains pyrpn Python module definition
26 26
  */
27 27
 
28
-PyMethodDef rpnmodule_methods[] = {
29
-	{"get_ops", (PyCFunction)pyrpn_ops, METH_NOARGS,
30
-		"Returns a valid operands dict"},
31
-	{"random_expr", (PyCFunction)pyrpn_random, METH_VARARGS | METH_KEYWORDS,
32
-		"Return a random RPN expression"},
28
+static PyMethodDef rpnmodule_methods[] = {
29
+	PYRPN_method("get_ops", pyrpn_ops,
30
+			METH_NOARGS,
31
+			"/",
32
+			"Return a dict with valid operands"),
33
+	PYRPN_method("random_expr", pyrpn_random,
34
+			METH_VARARGS | METH_KEYWORDS,
35
+			"args_count, token_count",
36
+			"Return a random RPN expression string"),
33 37
 	{NULL} // Sentinel
34 38
 };
35 39
 

+ 1
- 3
python_pyrpn.h View File

@@ -66,9 +66,7 @@ extern PyTypeObject RPNExprType;
66 66
 /**@brief Python module initialization function
67 67
  * @ingroup python_module */
68 68
 PyMODINIT_FUNC PyInit_pyrpn(void);
69
-/**@brief pyrpn module methods list
70
- * @ingroup python_module */
71
-extern PyMethodDef rpnmodule_methods[];
69
+
72 70
 /**@brief Python module specs
73 71
  * @ingroup python_module */
74 72
 extern PyModuleDef rpnmodule;

+ 39
- 23
python_rpnexpr.c View File

@@ -18,36 +18,52 @@
18 18
  */
19 19
 #include "python_rpnexpr.h"
20 20
 
21
-PyMethodDef RPNExpr_methods[] = {
22
-	{"random", (PyCFunction)rpnexpr_random, METH_CLASS | METH_VARARGS | METH_KEYWORDS,
23
-		"Return a new random RPN expression string"},
24
-	{"default_mutation_params", (PyCFunction)rpnexpr_default_mutation_params,
25
-		METH_CLASS | METH_FASTCALL,
26
-		"Return the default mutation parameters"},
27
-	{"eval", (PyCFunction)rpnexpr_eval, METH_FASTCALL, "Evaluate an expression"},
28
-	{"mutate", (PyCFunction)rpnexpr_mutate,
29
-		METH_VARARGS | METH_KEYWORDS,
30
-		"Mutate an expression given an RPNMutationParamsTuple instance"},
31
-	{"reset_stack", (PyCFunction)rpnexpr_reset_stack, METH_NOARGS,
32
-		"Reset stack memory storage (set all items to 0)"},
33
-	{"__getstate__", (PyCFunction)rpnexpr_getstate, METH_NOARGS,
34
-		"Pickling method. Return a bytes repr of tokenized expression \
35
-and the stack state."},
36
-	{"__setstate__", (PyCFunction)rpnexpr_setstate, METH_O,
37
-		"Unpickling method"},
38
-	{"__copy__", (PyCFunction)rpnexpr_copy, METH_NOARGS,
39
-		"Clone method. Return a new cloned instance"},
40
-	{"uid", (PyCFunction)rpnexpr_getexprstate, METH_NOARGS,
41
-		"Return a base64 uid for expression"},
21
+static PyMethodDef RPNExpr_methods[] = {
22
+	PYRPN_method("random", rpnexpr_random,
23
+			METH_CLASS | METH_VARARGS | METH_KEYWORDS,
24
+			"cls, args_count, token_count=10",
25
+			"Return a new random RPN expression string"),
26
+	PYRPN_method("default_mutation_params", rpnexpr_default_mutation_params,
27
+			METH_CLASS | METH_FASTCALL,
28
+			"cls, /",
29
+			"Return the default mutation parameters"),
30
+	PYRPN_method("eval", rpnexpr_eval, METH_FASTCALL,
31
+			"self, /, *args",
32
+			"Evaluate an expression"),
33
+	PYRPN_method("mutate", rpnexpr_mutate,
34
+			METH_VARARGS | METH_KEYWORDS,
35
+			"self, params=None, n_mutations=1",
36
+			"Mutate an expression"),
37
+	PYRPN_method("reset_stack", rpnexpr_reset_stack,
38
+			METH_NOARGS,
39
+			"self, /",
40
+			"Reset the stack (set all items to 0)"),
41
+	PYRPN_method("__getstate__", rpnexpr_getstate,
42
+			METH_NOARGS,
43
+			"self, /",
44
+			"Pickling method (see pickle module).\n"
45
+			"Return a bytes representation of the expression state."),
46
+	PYRPN_method("__setstate__", rpnexpr_setstate,
47
+			METH_O,
48
+			"self, state, /",
49
+			"Unpickling method (see pickle module)."),
50
+	PYRPN_method("__copy__", rpnexpr_copy,
51
+			METH_NOARGS,
52
+			"self, /",
53
+			"Return a new equivalent instance (see copy module)"),
54
+	PYRPN_method("uid", rpnexpr_getexprstate,
55
+			METH_NOARGS,
56
+			"self, /",
57
+			"Return a base64 uid for expression"),
42 58
 	{NULL} //Sentinel
43 59
 };
44 60
 
45
-PyMemberDef RPNExpr_members[] = {
61
+static PyMemberDef RPNExpr_members[] = {
46 62
 	{NULL}
47 63
 };
48 64
 
49 65
 /**@todo Continue sequence implementation with contains, concat, repeat etc. */
50
-PySequenceMethods RPNExpr_seq_methods = {
66
+static PySequenceMethods RPNExpr_seq_methods = {
51 67
 	.sq_length = rpnexpr_len,
52 68
 	.sq_item = rpnexpr_token_item,
53 69
 	.sq_ass_item = rpnexpr_token_ass_item,

+ 0
- 6
python_rpnexpr.h View File

@@ -45,12 +45,6 @@
45 45
  *
46 46
  */
47 47
 
48
-/**@brief RPNExpr Python class methods list
49
- * @ingroup python_type */
50
-extern PyMethodDef RPNExpr_methods[];
51
-/**@brief RPNExpr Python class members list
52
- * @ingroup python_type */
53
-extern PyMemberDef RPNExpr_members[];
54 48
 /**@brief RPNExpr Python class type definition
55 49
  * @ingroup python_type */
56 50
 extern PyTypeObject RPNExprType;

+ 13
- 9
python_rpntoken.c View File

@@ -1,8 +1,9 @@
1 1
 #include "python_rpntoken.h"
2 2
 
3 3
 static PyMethodDef RPNToken_methods[] = {
4
-	{"from_str", (PyCFunction)rpntoken_from_str, METH_CLASS | METH_O,
5
-		"Return a new RPNToken subclass instance from string"},
4
+	PYRPN_method("from_str", rpntoken_from_str, METH_CLASS | METH_O,
5
+			"cls, token_str, /",
6
+			"Return a new RPNToken subclass instance from string"),
6 7
 	{NULL} //
7 8
 };
8 9
 
@@ -22,13 +23,16 @@ PyTypeObject RPNTokenType = {
22 23
 };
23 24
 
24 25
 static PyMethodDef RPNTokenOp_methods[] = {
25
-	{"opcode_max", (PyCFunction)rpntokenop_opcode_max,
26
-		 METH_STATIC | METH_NOARGS,
27
-		"Return the maximum valid value for an opcode"},
28
-	{"chr", (PyCFunction)rpntokenop_opchr, METH_NOARGS,
29
-		"Return the single char representation of the operand"},
30
-	{"str", (PyCFunction)rpntokenop_opstr, METH_NOARGS,
31
-		"Return the string (multi-char) representation of the operand"},
26
+	PYRPN_method("opcode_max", rpntokenop_opcode_max,
27
+			METH_STATIC | METH_NOARGS,
28
+			"",
29
+			"Return the maximum valid value for an opcode"),
30
+	PYRPN_method("chr", rpntokenop_opchr, METH_NOARGS,
31
+			"self, /",
32
+			"Return the single char representation of operand"),
33
+	PYRPN_method("str", rpntokenop_opstr, METH_NOARGS,
34
+			"self, /",
35
+			"Return the string (multi-char) representation of the operand"),
32 36
 	{NULL} //
33 37
 };
34 38
 

Loading…
Cancel
Save