| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*************************************** | ||
| 2 | Auteur : Pierre Aubert | ||
| 3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
| 4 | Licence : CeCILL-C | ||
| 5 | ****************************************/ | ||
| 6 | |||
| 7 | |||
| 8 | #include "wrapper_utils.h" | ||
| 9 | |||
| 10 | ///Get the string description of the Py_BuildValue call for the given type | ||
| 11 | /** @param type : type of the used | ||
| 12 | * @return corresponding string description or UnknownType if the type is not yet supported by this function | ||
| 13 | */ | ||
| 14 | ✗ | PString wrapper_getBuildValueStr(const PString & type){ | |
| 15 | ✗ | if(type == "int"){return "i";} | |
| 16 | ✗ | else if(type == "float"){return "f";} | |
| 17 | ✗ | else if(type == "double"){return "d";} | |
| 18 | ✗ | else if(type == "char"){return "c";} | |
| 19 | ✗ | else if(type == "std::string"){return "s";} | |
| 20 | else{ | ||
| 21 | ✗ | return "UnknownType"; | |
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | ///Get the function to convert the given object to a value | ||
| 26 | /** @param type : tpye to be used | ||
| 27 | * @return corresponding function or UnknownConvertFunctionToValue if the type is not yet supported by this function | ||
| 28 | */ | ||
| 29 | ✗ | PString wrapper_getObjectToValue(const PString & type){ | |
| 30 | ✗ | if(type == "float" || type == "double"){return "PyFloat_AsDouble";} | |
| 31 | ✗ | else if(type == "long" || type == "short" || type == "char"){return "PyLong_AsLong";} | |
| 32 | ✗ | else if(type == "int"){return "PyLong_AsInt";} | |
| 33 | ✗ | else if(type == "size_t"){return "PyLong_ToSize_t";} | |
| 34 | else{ | ||
| 35 | ✗ | return "UnknownConvertFunctionToValue"; | |
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | ///Get the function to convert the given value to a PyObject | ||
| 40 | /** @param type : tpye to be used | ||
| 41 | * @return corresponding function or UnknownConvertFunctionFromValue if the type is not yet supported by this function | ||
| 42 | */ | ||
| 43 | ✗ | PString wrapper_getValueToObject(const PString & type){ | |
| 44 | ✗ | if(type == "float" || type == "double"){return "PyFloat_FromDouble";} | |
| 45 | ✗ | else if(type == "long" || type == "short" || type == "char" || type == "int"){return "PyLong_FromLong";} | |
| 46 | ✗ | else if(type == "unsigned long" || type == "unsigned short" || type == "unsigned char" || type == "unsigned int"){return "PyLong_FromLong";} | |
| 47 | ✗ | else if(type == "size_t"){return "PyLong_FromSize_t";} | |
| 48 | else{ | ||
| 49 | ✗ | return "UnknownConvertFunctionFromValue"; | |
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | ///Get the expected type of a given python type | ||
| 54 | /** @param type : type ot be used | ||
| 55 | * @return corresponding Python type or NoPythonTypeYet if the type is not yet supported by this function | ||
| 56 | */ | ||
| 57 | ✗ | PString wrapper_getExpectedType(const PString & type){ | |
| 58 | ✗ | if(type == "long" || type == "int" || type == "short" || type == "char"){return "int";} | |
| 59 | ✗ | else if(type == "float" || type == "double"){return "float";} | |
| 60 | ✗ | else if(type == "std::string"){return "std";} | |
| 61 | else{ | ||
| 62 | ✗ | return "NoPythonTypeYet"; | |
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | ///Get the corresponding wrapper class name | ||
| 67 | /** @param classConfig : PClassConfig to be used | ||
| 68 | * @return corresponding wrapper class name | ||
| 69 | */ | ||
| 70 | 44 | PString wrapper_getClassName(const PClassConfig & classConfig){ | |
| 71 |
3/3✓ Branch 0 (2→3) taken 44 times.
✓ Branch 2 (3→4) taken 44 times.
✓ Branch 4 (4→5) taken 44 times.
|
44 | return "WP"+classConfig.getName(); |
| 72 | } | ||
| 73 | |||
| 74 | ///Get the proper code for PyArg_ParseTuple with one object | ||
| 75 | /** @param varName : name of the variable to be checked | ||
| 76 | * @return corresponding C++ | ||
| 77 | */ | ||
| 78 | 3 | PString wrapper_getObjectParseTuple(const PString & varName){ | |
| 79 | 3 | PString body; | |
| 80 |
3/3✓ Branch 0 (3→4) taken 3 times.
✓ Branch 2 (4→5) taken 3 times.
✓ Branch 4 (5→6) taken 3 times.
|
3 | body += "\tif(!PyArg_ParseTuple(args, \"O\", &"+varName+")){\n"; |
| 81 |
1/1✓ Branch 0 (8→9) taken 3 times.
|
3 | body += "\t\tPyErr_SetString(PyExc_RuntimeError, \"missing filename or keyword not supported\\n\");\n"; |
| 82 |
1/1✓ Branch 0 (9→10) taken 3 times.
|
3 | body += "\t\treturn NULL;\n"; |
| 83 |
1/1✓ Branch 0 (10→11) taken 3 times.
|
3 | body += "\t}\n"; |
| 84 | 3 | return body; | |
| 85 | ✗ | } | |
| 86 | |||
| 87 | ///Get the C++ definition of an attribute | ||
| 88 | /** @param attr : attribute of a class | ||
| 89 | * @return corresponding C++ | ||
| 90 | */ | ||
| 91 | 5 | PString project_wrapper_attributeDef(const PClassAttribute & attr){ | |
| 92 | 5 | PString body; | |
| 93 |
4/4✓ Branch 0 (3→4) taken 5 times.
✓ Branch 2 (4→5) taken 5 times.
✓ Branch 4 (5→6) taken 5 times.
✓ Branch 6 (6→7) taken 5 times.
|
5 | body += "\t" + attr.getDocumentation() + "\n"; |
| 94 |
4/4✓ Branch 0 (9→10) taken 5 times.
✓ Branch 2 (10→11) taken 5 times.
✓ Branch 4 (11→12) taken 3 times.
✓ Branch 5 (11→17) taken 2 times.
|
5 | if(getIsSimpleType(attr.getType())){ |
| 95 |
3/3✓ Branch 0 (12→13) taken 3 times.
✓ Branch 2 (13→14) taken 3 times.
✓ Branch 4 (14→15) taken 3 times.
|
3 | body += "\t" + attr.getType(); |
| 96 | }else{ | ||
| 97 |
1/1✓ Branch 0 (17→18) taken 2 times.
|
2 | body += "\tPyObject *"; |
| 98 | } | ||
| 99 |
4/4✓ Branch 0 (18→19) taken 5 times.
✓ Branch 2 (19→20) taken 5 times.
✓ Branch 4 (20→21) taken 5 times.
✓ Branch 6 (21→22) taken 5 times.
|
5 | body += " "+attr.getName()+";\n"; |
| 100 | 5 | return body; | |
| 101 | ✗ | } | |
| 102 | |||
| 103 | ///Create the Python type of the given class | ||
| 104 | /** @param classConfig : class to be used | ||
| 105 | * @param moduleName : name of the module where the class should be defined | ||
| 106 | * @param fromOtherType : name of an other PyTypeObject to inheritate | ||
| 107 | * @return corresponding C++ | ||
| 108 | */ | ||
| 109 | 3 | PString project_wrapper_classImplPythonType(const PClassConfig & classConfig, const PString & moduleName, const PString & fromOtherType){ | |
| 110 |
2/2✓ Branch 0 (2→3) taken 3 times.
✓ Branch 2 (3→4) taken 3 times.
|
3 | PString body, className(wrapper_getClassName(classConfig)); |
| 111 | |||
| 112 |
3/3✓ Branch 0 (4→5) taken 3 times.
✓ Branch 2 (5→6) taken 3 times.
✓ Branch 4 (6→7) taken 3 times.
|
3 | body += "///Define the type of the "+className+"\n"; |
| 113 |
3/3✓ Branch 0 (9→10) taken 3 times.
✓ Branch 2 (10→11) taken 3 times.
✓ Branch 4 (11→12) taken 3 times.
|
3 | body += "PyTypeObject "+className+"Type = {\n"; |
| 114 |
3/3✓ Branch 0 (14→15) taken 3 times.
✓ Branch 2 (15→16) taken 3 times.
✓ Branch 4 (16→17) taken 3 times.
|
3 | body += "\tPyVarObject_HEAD_INIT("+fromOtherType+", 0)\n"; |
| 115 |
6/6✓ Branch 0 (19→20) taken 3 times.
✓ Branch 2 (20→21) taken 3 times.
✓ Branch 4 (21→22) taken 3 times.
✓ Branch 6 (22→23) taken 3 times.
✓ Branch 8 (23→24) taken 3 times.
✓ Branch 10 (24→25) taken 3 times.
|
3 | body += "\t\""+moduleName+"."+classConfig.getName()+"\", /* tp_name */\n"; |
| 116 |
3/3✓ Branch 0 (29→30) taken 3 times.
✓ Branch 2 (30→31) taken 3 times.
✓ Branch 4 (31→32) taken 3 times.
|
3 | body += "\tsizeof("+className+"), /* tp_basicsize */\n"; |
| 117 |
1/1✓ Branch 0 (34→35) taken 3 times.
|
3 | body += "\t0, /* tp_itemsize */\n"; |
| 118 |
3/3✓ Branch 0 (35→36) taken 3 times.
✓ Branch 2 (36→37) taken 3 times.
✓ Branch 4 (37→38) taken 3 times.
|
3 | body += "\t(destructor)"+className+"_dealloc, /* tp_dealloc */\n"; |
| 119 |
1/1✓ Branch 0 (40→41) taken 3 times.
|
3 | body += "\t0, /* tp_print */\n"; |
| 120 |
1/1✓ Branch 0 (41→42) taken 3 times.
|
3 | body += "\t0, /* tp_getattr */\n"; |
| 121 |
1/1✓ Branch 0 (42→43) taken 3 times.
|
3 | body += "\t0, /* tp_setattr */\n"; |
| 122 |
1/1✓ Branch 0 (43→44) taken 3 times.
|
3 | body += "\t0, /* tp_reserved */\n"; |
| 123 |
1/1✓ Branch 0 (44→45) taken 3 times.
|
3 | body += "\t0, /* tp_repr */\n"; |
| 124 |
1/1✓ Branch 0 (45→46) taken 3 times.
|
3 | body += "\t0, /* tp_as_number */\n"; |
| 125 |
1/1✓ Branch 0 (46→47) taken 3 times.
|
3 | body += "\t0, /* tp_as_sequence */\n"; |
| 126 |
1/1✓ Branch 0 (47→48) taken 3 times.
|
3 | body += "\t0, /* tp_as_mapping */\n"; |
| 127 |
1/1✓ Branch 0 (48→49) taken 3 times.
|
3 | body += "\t0, /* tp_hash */\n"; |
| 128 |
1/1✓ Branch 0 (49→50) taken 3 times.
|
3 | body += "\t0, /* tp_call */\n"; |
| 129 |
1/1✓ Branch 0 (50→51) taken 3 times.
|
3 | body += "\t0, /* tp_str */\n"; |
| 130 |
1/1✓ Branch 0 (51→52) taken 3 times.
|
3 | body += "\t0, /* tp_getattro */\n"; |
| 131 |
1/1✓ Branch 0 (52→53) taken 3 times.
|
3 | body += "\t0, /* tp_setattro */\n"; |
| 132 |
1/1✓ Branch 0 (53→54) taken 3 times.
|
3 | body += "\t0, /* tp_as_buffer */\n"; |
| 133 |
1/1✓ Branch 0 (54→55) taken 3 times.
|
3 | body += "\tPy_TPFLAGS_DEFAULT |\n"; |
| 134 |
1/1✓ Branch 0 (55→56) taken 3 times.
|
3 | body += "\tPy_TPFLAGS_BASETYPE, /* tp_flags */\n"; |
| 135 |
9/9✓ Branch 0 (56→57) taken 3 times.
✓ Branch 2 (57→58) taken 3 times.
✓ Branch 4 (58→59) taken 3 times.
✓ Branch 6 (59→60) taken 3 times.
✓ Branch 8 (60→61) taken 3 times.
✓ Branch 10 (61→62) taken 3 times.
✓ Branch 12 (62→63) taken 3 times.
✓ Branch 14 (63→64) taken 3 times.
✓ Branch 16 (64→65) taken 3 times.
|
3 | body += "\t\""+className+" doc : "+classConfig.getClassDocumentation().replace("///", "")+"\", /* tp_doc */\n"; |
| 136 |
1/1✓ Branch 0 (72→73) taken 3 times.
|
3 | body += "\t0, /* tp_traverse */\n"; |
| 137 |
1/1✓ Branch 0 (73→74) taken 3 times.
|
3 | body += "\t0, /* tp_clear */\n"; |
| 138 |
1/1✓ Branch 0 (74→75) taken 3 times.
|
3 | body += "\t0, /* tp_richcompare */\n"; |
| 139 |
1/1✓ Branch 0 (75→76) taken 3 times.
|
3 | body += "\t0, /* tp_weaklistoffset */\n"; |
| 140 |
1/1✓ Branch 0 (76→77) taken 3 times.
|
3 | body += "\t0, /* tp_iter */\n"; |
| 141 |
1/1✓ Branch 0 (77→78) taken 3 times.
|
3 | body += "\t0, /* tp_iternext */\n"; |
| 142 |
3/3✓ Branch 0 (78→79) taken 3 times.
✓ Branch 2 (79→80) taken 3 times.
✓ Branch 4 (80→81) taken 3 times.
|
3 | body += "\t"+className+"_methods, /* tp_methods */\n"; |
| 143 |
3/3✓ Branch 0 (83→84) taken 3 times.
✓ Branch 2 (84→85) taken 3 times.
✓ Branch 4 (85→86) taken 3 times.
|
3 | body += "\t"+className+"_members, /* tp_members */\n"; |
| 144 |
3/3✓ Branch 0 (88→89) taken 3 times.
✓ Branch 2 (89→90) taken 3 times.
✓ Branch 4 (90→91) taken 3 times.
|
3 | body += "\t"+className+"_getseters,/* tp_getset */\n"; |
| 145 |
1/1✓ Branch 0 (93→94) taken 3 times.
|
3 | body += "\t0, /* tp_base */\n"; |
| 146 |
1/1✓ Branch 0 (94→95) taken 3 times.
|
3 | body += "\t0, /* tp_dict */\n"; |
| 147 |
1/1✓ Branch 0 (95→96) taken 3 times.
|
3 | body += "\t0, /* tp_descr_get */\n"; |
| 148 |
1/1✓ Branch 0 (96→97) taken 3 times.
|
3 | body += "\t0, /* tp_descr_set */\n"; |
| 149 |
1/1✓ Branch 0 (97→98) taken 3 times.
|
3 | body += "\t0, /* tp_dictoffset */\n"; |
| 150 |
1/1✓ Branch 0 (98→99) taken 3 times.
|
3 | body += "\t0, /* tp_init */\n"; |
| 151 |
1/1✓ Branch 0 (99→100) taken 3 times.
|
3 | body += "\t0, /* tp_alloc */\n"; |
| 152 |
3/3✓ Branch 0 (100→101) taken 3 times.
✓ Branch 2 (101→102) taken 3 times.
✓ Branch 4 (102→103) taken 3 times.
|
3 | body += "\t"+className+"_newC, /* tp_new */\n"; |
| 153 |
1/1✓ Branch 0 (105→106) taken 3 times.
|
3 | body += "};\n\n"; |
| 154 | 3 | return body; | |
| 155 | 3 | } | |
| 156 | |||
| 157 |