00001
00006
00007
00008
00009
00010 #include <Python.h>
00011 #include <string.h>
00012 #include <errcode.h>
00013 #include "cfc.h"
00014 #include "sic_comm.h"
00015
00016 #define pyend CFC_EXPORT_NAME( pyend )
00017 #define pyexecfile CFC_EXPORT_NAME( pyexecfile )
00018 #define pyexec CFC_EXPORT_NAME( pyexec )
00019 #define pyinteract CFC_EXPORT_NAME( pyinteract )
00020 #define pyfindfunc CFC_EXPORT_NAME( pyfindfunc )
00021 #define pycallfuncd CFC_EXPORT_NAME( pycallfuncd )
00022 #define pycallfuncs CFC_EXPORT_NAME( pycallfuncs )
00023 #define pygetvar CFC_EXPORT_NAME( pygetvar )
00024 #define pydelvar CFC_EXPORT_NAME( pydelvar )
00025
00026
00027
00028
00029
00030
00037 static void lowercase(char string[]) {
00038 int i = 0;
00039
00040 while ( string[i] ) {
00041 string[i] = tolower(string[i]);
00042 i++;
00043 }
00044
00045 return;
00046 }
00047
00053 static void pystart() {
00054
00055 if (! Py_IsInitialized()) {
00056
00057 Py_Initialize();
00058 printf("Python %s on %s\n",Py_GetVersion(),Py_GetPlatform());
00059 if (! PyImport_ImportModule("readline")) {
00060 fprintf(stderr,"W-PYTHON, Failed to load Python module 'readline': ");
00061 PyErr_Print();
00062 }
00063
00064
00065 PyRun_SimpleString("from pginit import *\n");
00066 }
00067
00068 return;
00069 }
00070
00076 static int pyisslave() {
00077 int ret=-1;
00078 PyObject *module=NULL, *isslave=NULL;
00079
00080 if (! (module = PyImport_ImportModule("pygildas"))) {
00081 fprintf(stderr,"E-PYTHON, Could not import 'pygildas' module into Python.\n");
00082 PyErr_Print();
00083 } else if (! PyObject_HasAttrString(module, "is_slave")) {
00084 fprintf(stderr,"E-PYTHON, Did not found 'is_slave' attribute of 'pygildas' module.\n");
00085 } else if (! (isslave = PyObject_GetAttrString(module, "is_slave"))) {
00086 fprintf(stderr,"E-PYTHON, Failed to load 'is_slave' attribute of 'pygildas' module.\n");
00087 PyErr_Print();
00088 } else if (PyObject_IsTrue(isslave)) {
00089 ret = 1;
00090 } else {
00091 ret = 0;
00092 }
00093
00094 Py_XDECREF(module);
00095 Py_XDECREF(isslave);
00096 return ret;
00097 }
00098
00105 static int switchflag(int value) {
00106 int ret=1;
00107 PyObject *module;
00108
00109 if (! (module = PyImport_ImportModule("pygildas"))) {
00110 fprintf(stderr,"E-PYTHON, Could not import 'pygildas' module into Python.\n");
00111 PyErr_Print();
00112 } else if (! PyObject_HasAttrString(module, "loop")) {
00113 fprintf(stderr,"E-PYTHON, Did not found 'loop' attribute of 'pygildas' module.\n");
00114 } else {
00115 Py_XDECREF(PyObject_GetAttrString(module,"loop"));
00116 PyObject_SetAttrString(module,"loop",PyInt_FromLong((long) value));
00117 ret = 0;
00118 }
00119 Py_XDECREF(module);
00120
00121 return ret;
00122 }
00123
00140 static int PyRun_MyInteractiveLoop(FILE *fp, const char *filename) {
00141 int signal;
00142 PyObject *v, *module;
00143 PyCompilerFlags *flags, local_flags;
00144
00145
00146 flags = &local_flags;
00147 local_flags.cf_flags = 0;
00148
00149 v = PySys_GetObject("ps1");
00150 if (v == NULL) {
00151 PySys_SetObject("ps1", v = PyString_FromString(">>> "));
00152 Py_XDECREF(v);
00153 }
00154 v = PySys_GetObject("ps2");
00155 if (v == NULL) {
00156 PySys_SetObject("ps2", v = PyString_FromString("... "));
00157 Py_XDECREF(v);
00158 }
00159
00160
00161 if (! (module = PyImport_ImportModule("pygildas"))) {
00162 fprintf(stderr,"E-PYTHON, Could not import 'pygildas' module into Python.\n");
00163 PyErr_Print();
00164 return -1;
00165 }
00166
00167
00168 for (;;) {
00169 signal = PyRun_InteractiveOneFlags(fp, filename, flags);
00170
00171
00172 if (signal == E_EOF) {
00173 switchflag(-1);
00174 return 1;
00175 }
00176
00177 if (PyInt_AsLong(PyObject_GetAttrString(module,"loop")) == -1)
00178 return 1;
00179
00180 if (PyInt_AsLong(PyObject_GetAttrString(module,"loop")) == 0)
00181 return 0;
00182 }
00183
00184 }
00185
00192 static PyObject* sic2pyconv(char *varname) {
00193 PyObject *module, *Sic=NULL, *pyname=NULL;
00194
00195 if (! (module = PyImport_AddModule("__main__"))) {
00196 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00197 PyErr_Print();
00198 } else if (! PyObject_HasAttrString(module, "Sic")) {
00199 fprintf(stderr,"E-PYTHON, Did not found 'Sic' instance in Python __main__.\n");
00200 } else if (! (Sic = PyObject_GetAttrString(module, "Sic"))) {
00201 fprintf(stderr,"E-PYTHON, Failed to load 'Sic' from Python __main__.\n");
00202 PyErr_Print();
00203 } else if (! PyObject_HasAttrString(Sic, "sic2py")) {
00204 fprintf(stderr,"E-PYTHON, Did not found 'sic2py' method of 'Sic' instance.\n");
00205 } else {
00206 pyname = PyObject_CallMethod(Sic,"sic2py","(s)",varname);
00207 }
00208 Py_XDECREF(Sic);
00209
00210 return pyname;
00211 }
00212
00222 static int pyunsavevar(int level, PyObject *pyname) {
00223 int ret=1;
00224 PyObject *module, *Sic=NULL, *tuple=NULL, *instance, *object=NULL;
00225
00226 if (! (module = PyImport_AddModule("__main__"))) {
00227 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00228 PyErr_Print();
00229 } else if (! PyObject_HasAttrString(module, "Sic")) {
00230 fprintf(stderr,"E-PYTHON, Did not found 'Sic' instance in Python __main__.\n");
00231 } else if (! (Sic = PyObject_GetAttrString(module, "Sic"))) {
00232 fprintf(stderr,"E-PYTHON, Failed to load 'Sic' instance from Python __main__.\n");
00233 PyErr_Print();
00234 } else if (! PyObject_HasAttrString(Sic, "localspaces")) {
00235 fprintf(stderr,"E-PYTHON, Did not found 'localspaces' array in 'Sic' instance. ");
00236 } else if (! (tuple = PyObject_GetAttrString(Sic, "localspaces"))) {
00237 fprintf(stderr,"E-PYTHON, Failed to load 'localspaces' array from 'Sic' instance.\n");
00238 PyErr_Print();
00239 } else if (! (instance = PyTuple_GetItem(tuple,level))) {
00240 fprintf(stderr,"E-PYTHON, Failed to load %d-th element of 'localspaces'.\n",level);
00241 PyErr_Print();
00242 } else if (! (PyObject_HasAttr(instance, pyname))) {
00243
00244 ;
00245 } else if (! (object = PyObject_GetAttr(instance, pyname))) {
00246 fprintf(stderr,"E-PYTHON, Failed to load '%s' variable from %d-th element of 'localspaces'.\n",PyString_AsString(pyname),level);
00247 PyErr_Print();
00248 } else if (PyObject_SetAttr(module,pyname,object) == -1) {
00249 fprintf(stderr,"E-PYTHON, Failed to add attribute '%s' to Python __main__.\n",PyString_AsString(pyname));
00250 PyErr_Print();
00251 } else if (PyObject_DelAttr(instance, pyname) == -1 ) {
00252 fprintf(stderr,"E-PYTHON, Failed to delete '%s' attribute from %d-th element of 'localspaces'.\n",PyString_AsString(pyname),level);
00253 PyErr_Print();
00254 } else {
00255 ret = 0;
00256 }
00257
00258 Py_XDECREF(Sic);
00259 Py_XDECREF(tuple);
00260 Py_XDECREF(object);
00261
00262 return ret;
00263 }
00264
00276 static void Py_XDECREF_parent(PyObject *object) {
00277
00278 if (object == NULL) {
00279 fprintf(stderr,"E-PYTHON, Failed to decrement reference count (object is NULL).\n");
00280 } else if (! (PyObject_HasAttrString(object,"__name__")) ) {
00281 fprintf(stderr,"E-PYTHON, Failed to decrement reference count (object has no __name__ attribute).\n");
00282 } else if ( PyObject_RichCompareBool(PyObject_GetAttrString(object,"__name__"),PyString_FromString("__main__"),Py_EQ) != 1 ) {
00283 Py_DECREF(object);
00284 }
00285
00286 return;
00287 }
00288
00299 static PyObject *getparent(PyObject *name) {
00300 PyObject *abovename, *childname, *parent=NULL, *parentname, *grandparent;
00301 int pos1, pos2;
00302
00303 pos1 = (int) PyInt_AsLong(PyObject_CallMethod(name,"rfind","s","."));
00304
00305 if (pos1 != -1) {
00306 parentname = PyObject_CallMethod(name,"__getslice__","ii",0,pos1);
00307 childname = PyObject_CallMethod(name,"__getslice__","ii",pos1+1,PyString_Size(name));
00308
00309 pos2 = (int) PyInt_AsLong(PyObject_CallMethod(parentname,"rfind","s","."));
00310 if (pos2 == -1) { pos2 = 0; } else { pos2 += 1; }
00311 abovename = PyObject_CallMethod(parentname,"__getslice__","ii",pos2,pos1);
00312
00313 if (! (grandparent = getparent(parentname))) {
00314 fprintf(stderr,"E-PYTHON, Failed to get parent of '%s' object.\n",PyString_AsString(PyObject_Repr(parentname)));
00315 } else if (! (parent = PyObject_GetAttr(grandparent,abovename))) {
00316 fprintf(stderr,"E-PYTHON, Failed to load '%s' attribute of '%s' object.\n",PyString_AsString(abovename),PyString_AsString(PyObject_Repr(grandparent)));
00317 PyErr_Print();
00318 }
00319 Py_XDECREF_parent(grandparent);
00320
00321
00322
00323 return parent;
00324 }
00325
00326
00327 if (! (parent = PyImport_AddModule("__main__"))) {
00328 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00329 PyErr_Print();
00330 }
00331
00332
00333
00334 return parent;
00335 }
00336
00337
00338
00339
00340
00341
00350 int CFC_API pyend() {
00351
00352 if (! Py_IsInitialized()) return 0;
00353
00354 if (pyisslave() == 1) {
00355 Py_Finalize();
00356 printf(" I-PYTHON, Python interpreter has been finalized.\n");
00357 } else {
00358 printf(" I-PYTHON, Python exit.\n");
00359 Py_Exit(0);
00360 }
00361
00362 return 0;
00363 }
00364
00378 int CFC_API pyexecfile(CFC_FString name, int *ln, CFC_FString args, int *la) {
00379 int ret = 1;
00380 PyObject *sysmod, *tuple=NULL, *mainmod, *sic=NULL, *verify=NULL, *Sic=NULL;
00381 char *cargs = malloc(*la + 1);
00382 char *cname = malloc(*ln + 1);
00383
00384 CFC_f2c_strcpy(cname,name,*ln);
00385 CFC_f2c_strcpy(cargs,args,*la);
00386
00387 pystart();
00388
00389
00390 if (! (sysmod = PyImport_AddModule("sys"))) {
00391 fprintf(stderr,"E-PYTHON, Failed to load Python module 'sys'\n");
00392 PyErr_Print();
00393 } else if (! (tuple = PyObject_CallMethod(PyString_FromString(cargs),"split",NULL)) ) {
00394 fprintf(stderr,"E-PYTHON, Failed to split arguments into Python 'sys.argv'.\n");
00395 PyErr_Print();
00396 } else if ( PyObject_SetAttrString(sysmod,"argv",tuple) == -1 ) {
00397 fprintf(stderr,"E-PYTHON, Failed to store arguments into Python 'sys.argv'.\n");
00398 PyErr_Print();
00399 }
00400 Py_XDECREF(tuple);
00401 free(cargs);
00402
00403
00404 if (! (mainmod = PyImport_AddModule("__main__"))) {
00405 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00406 PyErr_Print();
00407 } else if (! PyObject_HasAttrString(mainmod, "sic")) {
00408 fprintf(stderr,"E-PYTHON, Did not found 'sic' structure in Python __main__.\n");
00409 } else if (! (sic = PyObject_GetAttrString(mainmod, "sic"))) {
00410 fprintf(stderr,"E-PYTHON, Failed to load 'sic' structure from Python __main__.\n");
00411 PyErr_Print();
00412 } else if (! PyObject_HasAttrString(sic, "verify")) {
00413 fprintf(stderr,"E-PYTHON, 'Sic' instance has no attribute 'verify'.\n");
00414 } else if (! (verify = PyObject_GetAttrString(sic, "verify"))) {
00415 fprintf(stderr,"E-PYTHON, Failed to load 'verify' attribute of 'Sic' instance.\n");
00416 PyErr_Print();
00417 } else if ( PyObject_RichCompareBool(verify,PyInt_FromLong(0),Py_NE) == 0 ) {
00418
00419 FILE *fp = fopen (cname, "r+");
00420 if (fp == NULL) {
00421 fprintf(stderr,"E-PYTHON, Python file '%s' was not found.\n",cname);
00422 } else {
00423 ret = - PyRun_SimpleFileEx(fp,cname,1);
00424 }
00425
00426 } else if (! PyObject_HasAttrString(mainmod, "Sic")) {
00427 fprintf(stderr,"E-PYTHON, Did not found 'Sic' object in Python __main__.\n");
00428 } else if (! (Sic = PyObject_GetAttrString(mainmod, "Sic"))) {
00429 fprintf(stderr,"E-PYTHON, Failed to load 'Sic' object from Python __main__.\n");
00430 PyErr_Print();
00431 } else if (! PyObject_HasAttrString(Sic, "pexecfile")) {
00432 fprintf(stderr,"E-PYTHON, 'Sic' object has no attribute 'pexecfile'.\n");
00433 } else {
00434 PyObject_CallMethod(Sic,"pexecfile","(s)",cname);
00435 if (PyErr_Occurred()) {
00436 fprintf(stderr,"E-PYTHON, An error occurred while executing '%s' file:\n",cname);
00437 PyErr_Print();
00438 } else {
00439 ret = 0;
00440 }
00441 }
00442 Py_XDECREF(sic);
00443 Py_XDECREF(verify);
00444 Py_XDECREF(Sic);
00445 free(cname);
00446
00447
00448 if (! (PyObject_HasAttrString(sysmod,"argv"))) {
00449
00450 ;
00451 } else if (PyObject_DelAttrString(sysmod,"argv") == -1) {
00452 fprintf(stderr,"W-PYTHON, Could not delete 'argv' attribute of 'sys' module.");
00453 PyErr_Print();
00454 }
00455
00456 return ret;
00457 }
00458
00471 int CFC_API pyexec(CFC_FString command, int *l) {
00472 int ret = 1;
00473 PyObject *mainmod, *sic=NULL, *verify=NULL, *module;
00474 char *formatted = malloc(*l + 2);
00475
00476 CFC_f2c_strcpy(formatted,command,*l);
00477 formatted[*l] = '\n';
00478 formatted[*l+1] = '\0';
00479
00480 pystart();
00481
00482
00483 if (! (mainmod = PyImport_AddModule("__main__"))) {
00484 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00485 PyErr_Print();
00486 } else if (! PyObject_HasAttrString(mainmod, "sic")) {
00487 fprintf(stderr,"E-PYTHON, Did not found 'sic' structure in Python __main__.\n");
00488 } else if (! (sic = PyObject_GetAttrString(mainmod, "sic"))) {
00489 fprintf(stderr,"E-PYTHON, Failed to load 'sic' structure from Python __main__.\n");
00490 PyErr_Print();
00491 } else if (! PyObject_HasAttrString(sic, "verify")) {
00492 fprintf(stderr,"E-PYTHON, 'Sic' instance has no attribute 'verify'.\n");
00493 } else if (! (verify = PyObject_GetAttrString(sic, "verify"))) {
00494 fprintf(stderr,"E-PYTHON, Failed to load 'verify' attribute of 'Sic' instance.\n");
00495 PyErr_Print();
00496 } else if ( PyObject_RichCompareBool(verify,PyInt_FromLong(0),Py_NE) == 1 ) {
00497 printf(">>> %s",formatted);
00498 }
00499
00500 Py_XDECREF(sic);
00501 Py_XDECREF(verify);
00502
00503
00504 ret = - PyRun_SimpleString(formatted);
00505
00506
00507 if (! (module = PyImport_ImportModule("readline"))) {
00508
00509 ;
00510 } else {
00511 formatted[*l] = '\0';
00512 PyObject_CallMethod(module,"add_history","s",formatted);
00513 }
00514 Py_XDECREF(module);
00515 free(formatted);
00516
00517 return ret;
00518 }
00519
00531 int CFC_API pyinteract(int *sic_code_end, int *sic_ocode) {
00532 int isslave, py_ocode;
00533
00534 pystart();
00535 isslave = pyisslave();
00536
00537
00538 if (isslave==-1) {
00539 fprintf(stderr,"E-PYTHON, An error occured while loading 'pygildas.is_slave' flag.\n");
00540 return 1;
00541 } else if ( switchflag(1) == 1 ) {
00542 fprintf(stderr,"E-PYTHON, An error occured while setting 'pygildas.loop' to 1.\n");
00543 return 1;
00544 }
00545
00546 if (isslave==1) {
00547 printf("Entering interactive session. Type 'Sic()' to go back to SIC.\n");
00548 py_ocode = PyRun_MyInteractiveLoop(stdin, "<STDIN>");
00549 if (py_ocode==1) {
00550
00551 sic_push_command_text("exit");
00552 }
00553 } else {
00554 *sic_ocode = *sic_code_end;
00555 }
00556
00557 return 0;
00558 }
00559
00569 int CFC_API pyfindfunc(CFC_FString fname, int *l) {
00570 int ret = 1;
00571 char *formatted;
00572 PyObject *module, *func=NULL, *Sic=NULL;
00573
00574 if (! Py_IsInitialized()) return 1;
00575
00576 formatted = malloc(*l + 1);
00577 CFC_f2c_strcpy(formatted,fname,*l);
00578 lowercase(formatted);
00579
00580 PyRun_SimpleString("import sys\n");
00581 PyRun_SimpleString("sys.path.append('')\n");
00582
00583 if (! (module = PyImport_AddModule("__main__"))) {
00584 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00585 PyErr_Print();
00586 } else if (! PyObject_HasAttrString(module, formatted)) {
00587
00588 ;
00589 } else if (! (func = PyObject_GetAttrString(module, formatted))) {
00590 fprintf(stderr,"E-PYTHON, Failed to load '%s' from Python __main__.\n",formatted);
00591 PyErr_Print();
00592 } else if (! PyCallable_Check(func)) {
00593
00594 } else if (! PyObject_HasAttrString(module, "Sic")) {
00595 fprintf(stderr,"E-PYTHON, Did not found 'Sic' instance in Python __main__.\n");
00596 } else if (! (Sic = PyObject_GetAttrString(module, "Sic"))) {
00597 fprintf(stderr,"E-PYTHON, Failed to load 'Sic' instance from Python __main__.\n");
00598 PyErr_Print();
00599 } else if (PyObject_SetAttrString( Sic, "pyfunc", func)) {
00600 fprintf(stderr,"E-PYTHON, Failed to add 'pyfunc' reference to 'Sic' object.\n");
00601 PyErr_Print();
00602 } else {
00603 ret = 0;
00604 }
00605
00606 Py_XDECREF(func);
00607 Py_XDECREF(Sic);
00608 free(formatted);
00609
00610 return ret;
00611 }
00612
00629 int CFC_API pycallfuncd(int *nargs, int *index, int strides[], int nwords[], double mem[], double *output) {
00630 int i, shift, ret=1;
00631 double dble;
00632 PyObject *module, *Sic=NULL, *func=NULL, *args=NULL, *value=NULL;
00633
00634 if (! (module = PyImport_AddModule("__main__"))) {
00635 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00636 PyErr_Print();
00637 } else if (! (Sic = PyObject_GetAttrString(module, "Sic"))) {
00638 fprintf(stderr,"E-PYTHON, Failed to load 'Sic' instance from Python __main__.\n");
00639 PyErr_Print();
00640 } else if (! (func = PyObject_GetAttrString(Sic, "pyfunc"))) {
00641 fprintf(stderr,"E-PYTHON, Failed to load Python function (aliased as 'pyfunc') from Sic object.\n");
00642 PyErr_Print();
00643 } else if (! (args = PyTuple_New(*nargs))) {
00644 for (i=0; i<*nargs; i++) {
00645 shift = nwords[i]-1;
00646 dble = mem[shift/2+(*index-1)*strides[i]];
00647
00648 PyTuple_SetItem(args, i, Py_BuildValue("d",dble));
00649 }
00650 } else if (! (value = PyObject_CallObject(func,args))) {
00651 fprintf(stderr,"E-PYTHON, Failed to call Python function (aliased as 'pyfunc').\n");
00652 PyErr_Print();
00653 } else {
00654 *output = PyFloat_AsDouble(value);
00655 ret = 0;
00656 }
00657
00658 Py_XDECREF(Sic);
00659 Py_XDECREF(func);
00660 Py_XDECREF(args);
00661 Py_XDECREF(value);
00662
00663 return ret;
00664 }
00665
00682 int CFC_API pycallfuncs(int *nargs, int *index, int strides[], int nwords[], float mem[], float *output) {
00683 int i, shift, ret=1;
00684 float real;
00685 PyObject *module, *Sic=NULL, *func=NULL, *args=NULL, *value=NULL;
00686
00687 if (! (module = PyImport_AddModule("__main__"))) {
00688 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00689 PyErr_Print();
00690 } else if (! (Sic = PyObject_GetAttrString(module, "Sic"))) {
00691 fprintf(stderr,"E-PYTHON, Failed to load 'Sic' instance from Python __main__.\n");
00692 PyErr_Print();
00693 } else if (! (func = PyObject_GetAttrString(Sic, "pyfunc"))) {
00694 fprintf(stderr,"E-PYTHON, Failed to load Python function (aliased as 'pyfunc') from Sic object.\n");
00695 PyErr_Print();
00696 } else if (! (args = PyTuple_New(*nargs))) {
00697 for (i=0; i<*nargs; i++) {
00698 shift = nwords[i]-1;
00699 real = mem[shift+(*index-1)*strides[i]];
00700 PyTuple_SetItem(args, i, Py_BuildValue("f",real));
00701 }
00702 } else if (! (value = PyObject_CallObject(func,args))) {
00703 fprintf(stderr,"E-PYTHON, Failed to call Python function (aliased as 'pyfunc').\n");
00704 PyErr_Print();
00705 } else {
00706 *output = (float) PyFloat_AsDouble(value);
00707 ret = 0;
00708 }
00709
00710 Py_XDECREF(Sic);
00711 Py_XDECREF(func);
00712 Py_XDECREF(args);
00713 Py_XDECREF(value);
00714
00715 return ret;
00716 }
00717
00725 int CFC_API pygetvar(CFC_FString varname, int *l) {
00726 int siclevel, ret=1;
00727 char *formatted;
00728 PyObject *module, *Sic=NULL;
00729
00730 if (! Py_IsInitialized()) return 1;
00731
00732
00733 formatted = malloc(*l);
00734 CFC_f2c_strcpy(formatted,varname,*l-1);
00735
00736 siclevel = CFC_f2c_string(varname)[*l-1] - '0';
00737
00738 if (! (module = PyImport_AddModule("__main__"))) {
00739 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00740 PyErr_Print();
00741 } else if (! PyObject_HasAttrString(module, "Sic")) {
00742 fprintf(stderr,"E-PYTHON, Did not found 'Sic' instance in Python __main__.\n");
00743 } else if (! (Sic = PyObject_GetAttrString(module, "Sic"))) {
00744 fprintf(stderr,"E-PYTHON, Failed to load 'Sic'.\n");
00745 PyErr_Print();
00746 } else if (! PyObject_CallMethod(Sic,"get","(si)",formatted,siclevel)) {
00747 fprintf(stderr,"E-PYTHON, Failed to call 'get' method of 'Sic' instance.\n");
00748 PyErr_Print();
00749 } else {
00750 ret = 0;
00751 }
00752
00753 Py_XDECREF(Sic);
00754 free(formatted);
00755
00756 return ret;
00757 }
00758
00770 int CFC_API pydelvar(CFC_FString varname, int *l) {
00771 int level, pos, ret=1;
00772 PyObject *module, *pyname=NULL, *parent=NULL, *shortname=NULL;
00773 PyObject *var=NULL, *sicname=NULL;
00774 char *formatted;
00775
00776 if (! Py_IsInitialized()) return 1;
00777
00778
00779 formatted = malloc(*l);
00780 CFC_f2c_strcpy(formatted,varname,*l-1);
00781
00782 level = CFC_f2c_string(varname)[*l-1] - '0';
00783
00784 if (! (module = PyImport_AddModule("__main__"))) {
00785 fprintf(stderr,"E-PYTHON, Failed to load Python __main__\n");
00786 PyErr_Print();
00787 } else if (! (pyname = sic2pyconv(formatted))) {
00788 fprintf(stderr,"E-PYTHON, Failed to convert SIC name '%s' to Python name calling 'sic2py'.\n",formatted);
00789 } else if (! (parent = getparent(pyname))) {
00790 fprintf(stderr,"E-PYTHON, Failed to load '%s' parent object.\n",PyString_AsString(pyname));
00791 } else {
00792
00793 pos = (int) PyInt_AsLong(PyObject_CallMethod(pyname,"rfind","s","."));
00794 if (pos == -1) { pos = 0; } else { pos += 1; }
00795 shortname = PyObject_CallMethod(pyname,"__getslice__","ii",pos,PyString_Size(pyname));
00796 }
00797
00798
00799
00800
00801 if (! PyObject_HasAttr(parent,shortname)) {
00802
00803
00804 ret = 0;
00805
00806 } else if (! (var = PyObject_GetAttr(parent,shortname))) {
00807 fprintf(stderr,"E-PYTHON, Failed to load '%s' attribute of object '%s'.\n",PyString_AsString(pyname),PyString_AsString(PyObject_Repr(parent)));
00808 PyErr_Print();
00809 } else if (! (PyObject_HasAttrString(var,"__sicname__")) ) {
00810 fprintf(stderr,"W-PYTHON, '%s' variable is neither a SicVar nor a SicStructure instance. ",PyString_AsString(pyname));
00811 fprintf(stderr,"W-PYTHON, It won't be deleted.\n");
00812 } else if (! (sicname = PyObject_GetAttrString(var,"__sicname__")) ) {
00813 fprintf(stderr,"E-PYTHON, Failed to load '__sicname__' attribute of '%s' variable in Python __main__.\n",PyString_AsString(pyname));
00814 PyErr_Print();
00815 } else if ( PyObject_RichCompareBool(PyString_FromString(formatted),sicname,Py_EQ) != 1 ) {
00816 fprintf(stderr,"E-PYTHON, %s.__sicname__ value ('%s') and original SIC name ('%s') do not match (deletion failure).\n",PyString_AsString(pyname),PyString_AsString(sicname),formatted);
00817 } else if (PyObject_DelAttr(parent,shortname) == -1) {
00818
00819 fprintf(stderr,"E-PYTHON, Failed to delete '%s' attribute of object '%s'.\n",PyString_AsString(shortname),PyString_AsString(PyObject_Repr(parent)));
00820 } else if (PyInt_AsLong(PyObject_CallMethod(pyname,"find","s",".")) == -1) {
00821
00822
00823 if (level==0) { pyunsavevar(0,pyname); }
00824
00825 while (level>0) {
00826 if (pyunsavevar(level-1,pyname) == 0) {
00827 level = 0;
00828 } else {
00829 level--;
00830 }
00831 }
00832 ret = 0;
00833 } else {
00834
00835
00836
00837 ret = 0;
00838 }
00839
00840 Py_XDECREF(pyname);
00841 Py_XDECREF_parent(parent);
00842 Py_XDECREF(shortname);
00843 Py_XDECREF(var);
00844 Py_XDECREF(sicname);
00845 free(formatted);
00846
00847 return ret;
00848 }