6.3. C module variable parameters

[Note]

  • Requires kernel version >= v1.10.0

  • Variable arguments must be placed after positional arguments

Example.

  1. // test.c
  2. void test_vals(PikaObj* self, int a, PikaTuple* val){
  3. printf("a: %d\n", a);
  4. for(int i =0; i< tuple_getSize(val); i++){
  5. Arg* arg_i = tuple_getArg(val, i);
  6. printf("val[%d]: %d\n", i, arg_getInt(arg_i));
  7. }
  8. }

Output the result:

  1. >>> test.vals(1, 2, 3, 4)
  2. a: 1
  3. val[0]: 2
  4. val[1]: 3
  5. >>>

6.4. C module keyword parameters

[Note]

  • Requires kernel version >= v1.10.7

  • Keyword arguments must be placed after positional and variable arguments

Example.

  1. # test.pyi
  2. def keys(a:int, **keys):...

Output result:

  1. >>> test.keys(1, b=2, c=3)
  2. a: 1
  3. keys['b']: 2
  4. keys['c']: 3
  5. >>>

6.5. C module constants

  1. class cJSON:
  2. cJSON_Invalid: int
  3. cJSON_False: int
  4. def __init__(self):...
  5. ...
  1. void pika_cjson_cJSON___init__(PikaObj* self) {
  2. obj_setInt(self, "cJSON_False", cJSON_False);
  3. ...
  4. }

These constants can be used directly without creating an object, i.e. as class properties.

Note that PikaScript class properties are read-only, and all modifications to class properties are invalid.

6.6. C module initialization

Define __init__() function directly in .pyi to perform module initialization, which will be triggered when the module is loaded, PikaScript has a delayed module loading mechanism, import will not trigger module loading directly, but only when the module is actually used for the first time.

For example:

  1. # test.pyi
  2. def __init__():...
  3. def hello():...
  1. //test.c
  2. void test___init__(PikaObj* self){
  3. printf("now loading module test...\n");
  4. }
  5. void test_hello(PikaObj* self){
  6. printf("hello!\n");
  7. };
  1. # main.py
  2. import test
  3. print('before run test.hello()')
  4. print('after run test.hello()')

Output.