#include #include #include #include #include #include #include #include #include #include #include #include "../sg.h" #include "python_main.h" extern PyObject *main_o, *main_dict, *sg_o, *sg_dict; extern gint stdout_pipe[2]; gint py_status=0; /* 0=normal, 1=old line waiting */ extern grammar _PyParser_Grammar; /* From Python/graminit.c */ extern FILE *stdout_fp; GString *com_buffer; #ifndef Py_eval_input #include #define Py_eval_input eval_input #endif /* Py_eval_input */ PyObject *sg_eval_func(gchar *func_def, gdouble x_value, gdouble *y_value) { PyObject *object=NULL; gint len; object=Py_BuildValue("d",x_value); if (!object) return NULL; PyDict_SetItemString (sg_dict, "x", object); object=PyRun_String (func_def, Py_eval_input, sg_dict, sg_dict); if (!object) { if (PyErr_Occurred()) python_error_report(object); return NULL; } *y_value=PyFloat_AsDouble (object); return object; } PyObject * sg_eval_func_xy(gchar *func_def, gdouble x_value, gdouble y_value, gdouble *z_value) { PyObject *object=NULL; gint len; object=Py_BuildValue("d",x_value); if (python_error_report(object)) return NULL; PyDict_SetItemString (sg_dict, "x", object); object=Py_BuildValue("d",y_value); if (python_error_report(object)) return NULL; PyDict_SetItemString (sg_dict, "y", object); object=PyRun_String (func_def, Py_eval_input, sg_dict, sg_dict); if ( python_error_report(object)) return NULL; if (PyFloat_Check(object)) *z_value=PyFloat_AsDouble (object); else if (PyInt_Check(object)) *z_value=(double)PyInt_AsLong (object); else *z_value=0; Py_INCREF(object); return object; } PyObject *construct_func(gchar *func_name, gchar **func_lines, gint line_num) { gint i,len=0,maxlen; gchar *func_def,*pos; PyObject *callable,*temp_obj; maxlen=strlen(func_name); for (i=0;imaxlen) maxlen=temp; } func_def=g_new0(gchar,len+maxlen+100); pos=g_new(gchar,maxlen+20); sprintf(pos,"def %s(x):\n",func_name); strcat(func_def,pos); for (i=0;ind==1) { parray=(PyArrayObject *)object; for (i=0;idimensions[0];i++) { apt=parray->data+i*parray->strides[0]; switch (parray->descr->type_num) { case PyArray_CHAR: case PyArray_UBYTE: case PyArray_SBYTE: val=(double)(*(char *)(apt)); g_array_append_val( array,val); break; case PyArray_SHORT: val=(double)(*(short int *)(apt)); g_array_append_val( array,val); break; case PyArray_INT: val=(double)(*(int *)(apt)); g_array_append_val( array,val); break; case PyArray_LONG: val=(double)(*(long *)(apt)); g_array_append_val( array,val); break; case PyArray_FLOAT: val=(double)(*(float *)(apt)); g_array_append_val( array,val); break; case PyArray_DOUBLE: val=*(double *)(apt); g_array_append_val( array,val); break; case PyArray_CFLOAT: case PyArray_CDOUBLE: val=(double)(*(long double*)(apt)); g_array_append_val( array,val); break; case PyArray_OBJECT: break; } } } else #endif if (PySequence_Check(object) && !PyString_Check(object)) { len=PySequence_Length(object); for (i=0;ilen; if (expect>0 && array->lendata); *num=array->len; g_array_free(array,FALSE); /* Delete structure, but NOT the data */ return retarray; } /* Evalutes an expression and sends back an array of strings */ gchar **sg_eval_expr_string(gchar *expr, gint *num) { PyObject *object=NULL,*seq_obj; GArray *array; gchar **retarray,*val; gint i,j,len; *num=0; object=PyRun_String (expr, Py_eval_input, main_dict, sg_dict); if (python_error_report(object)) return NULL; array=g_array_new (FALSE, FALSE, sizeof (gchar *)); if (PySequence_Check(object) && !PyString_Check(object)) { len=PySequence_Length(object); for (i=0;idata); *num=array->len; g_array_free(array,FALSE); /* Delete structure, but NOT the data */ return retarray; } int python_simple(gchar *command,gint count) { int retval,len; static PyObject *object=NULL; gchar *print_string,fn[]=PACKAGE,*s_str; gchar eol='\n'; node *parse_node; perrdetail perr; if (0==count) return(0); if (!command) return(0); /* User entered a blank line at the top level prompt, skip */ if ((0==py_status)&&(eol==command[0])) { return (1); } /* Make sure the command ends with a newline and the string ends * with a NULL */ if (0!=command[count-1]) { if (eol!=command[count-1]) { command[count++]=eol; } command[count++]=0; } if (py_status==0) { /* Top level command */ com_buffer=g_string_new(command); } else { /* Continued command block (until blank line) */ if ((1==py_status)&&(eol!=command[0])) { g_string_append(com_buffer,command); return(2); } } parse_node = PyParser_ParseString(com_buffer->str, &_PyParser_Grammar, Py_file_input,&perr); switch (perr.error) { case E_EOF: /* User has started a command block, * probably with a def or class command */ py_status=1; return(2); case E_DONE: /* A complete command was found when parsing */ /*FALL THROUGH*/ default: py_status=0; if (parse_node) PyNode_Free(parse_node); } object=PyRun_String (com_buffer->str, Py_single_input, main_dict, sg_dict); if (python_error_report(object)){ g_string_free(com_buffer,TRUE); return(0); } Py_DECREF(object); g_string_free(com_buffer,TRUE); return(1); } int sg_eval_script(gchar *fname) { PyObject *object; FILE *fp; fp=fopen(fname,"r"); if (!fp) { sg_message_dialog("File not found", 0); return 0; } object=PyRun_File (fp,fname, Py_file_input, main_dict, main_dict); fclose(fp); if (python_error_report(object)) return 0; return 1; }