/*=gfunc find_file * * what: locate a file in the search path * exparg: file-name, name of file with text * exparg: @suffix @ file suffix to try, too @ opt @ * * doc: * * AutoGen has a search path that it uses to locate template and definition * files. This function will search the same list for @file{file-name}, both * with and without the @file{.suffix}, if provided. =*/ SCM ag_scm_find_file(SCM file, SCM suffix) { SCM res = SCM_UNDEFINED; if (! AG_SCM_STRING_P(file)) scm_wrong_type_arg(FIND_FILE_NAME, 1, file); { char z[ AG_PATH_MAX+1 ]; char const * pz = ag_scm2zchars(file, "file-name"); /* * The suffix is optional. If provided, it will be a string. */ if (AG_SCM_STRING_P(suffix)) { char* apz[2]; apz[0] = (char *)ag_scm2zchars(suffix, "file suffix"); apz[1] = NULL; if (SUCCESSFUL(findFile(pz, z, (char const **)apz, NULL))) res = AG_SCM_STR02SCM(z); } else if (SUCCESSFUL(findFile(pz, z, NULL, NULL))) res = AG_SCM_STR02SCM(z); } return res; }
/*! \todo Finish function documentation!!! * \brief * \par Function Description * */ SCM g_funcs_filesel(SCM scm_msg, SCM scm_templ, SCM scm_flags) { int c_flags; char *r, *msg, *templ; SCM v; SCM_ASSERT (scm_is_string (scm_msg), scm_msg, SCM_ARG1, "gschem-filesel"); SCM_ASSERT (scm_is_string (scm_templ), scm_templ, SCM_ARG2, "gschem-filesel"); /*! \bug FIXME -- figure out the magic SCM_ASSERT for the flags */ /*! \bug FIXME -- how to deal with conflicting flags? * Should I throw a scheme error? Just deal in the c code? */ for (c_flags = 0; scm_is_pair (scm_flags); scm_flags = SCM_CDR (scm_flags)) { char *flag; SCM scm_flag = SCM_CAR (scm_flags); flag = scm_to_utf8_string (scm_flag); if (strcmp (flag, "may_exist") == 0) { c_flags |= FSB_MAY_EXIST; } else if (strcmp (flag, "must_exist") == 0) { c_flags |= FSB_MUST_EXIST; } else if (strcmp (flag, "must_not_exist") == 0) { c_flags |= FSB_SHOULD_NOT_EXIST; } else if (strcmp (flag, "save") == 0) { c_flags |= FSB_SAVE; } else if (strcmp (flag, "open") == 0) { c_flags |= FSB_LOAD; } else { free(flag); scm_wrong_type_arg ("gschem-filesel", SCM_ARG3, scm_flag); } free(flag); } msg = scm_to_utf8_string (scm_msg); templ = scm_to_utf8_string (scm_templ); r = generic_filesel_dialog (msg, templ, c_flags); free(msg); free(templ); v = scm_from_utf8_string (r); g_free (r); return v; }
/* Mark a pointer object destroyed */ SWIGINTERN void SWIG_Guile_MarkPointerDestroyed(SCM s) { SCM smob = SWIG_Guile_GetSmob(s); if (!SCM_NULLP(smob)) { if (SCM_SMOB_PREDICATE(swig_tag, smob) || SCM_SMOB_PREDICATE(swig_collectable_tag, smob)) { SCM_SET_CELL_TYPE(smob, swig_destroyed_tag); } else scm_wrong_type_arg(NULL, 0, s); } }
SCM gucu_set_panel_userdata (SCM pan, SCM data) { if (_scm_is_panel (pan)) { set_panel_userptr (_scm_to_panel (pan), (void *) data); } else scm_wrong_type_arg ("set-panel-userdata", SCM_ARG1, pan); return SCM_UNDEFINED; }
SWIGINTERNINLINE void * SWIG_Guile_MustGetPtr (SCM s, swig_type_info *type, int argnum, int flags, const char *func_name) { void *result; int res = SWIG_Guile_ConvertPtr(s, &result, type, flags); if (!SWIG_IsOK(res)) { /* type mismatch */ scm_wrong_type_arg((char *) func_name, argnum, s); } return result; }
SWIGINTERN swig_type_info * SWIG_Guile_PointerType(SCM object) { SCM smob = SWIG_Guile_GetSmob(object); if (SCM_NULLP(smob)) return NULL; else if (SCM_SMOB_PREDICATE(swig_tag, smob) || SCM_SMOB_PREDICATE(swig_collectable_tag, smob) || SCM_SMOB_PREDICATE(swig_destroyed_tag, smob)) { return (swig_type_info *) SCM_CELL_WORD_2(smob); } else scm_wrong_type_arg("SWIG-Guile-PointerType", 1, object); }
SWIGINTERN unsigned long SWIG_Guile_PointerAddress(SCM object) { SCM smob = SWIG_Guile_GetSmob(object); if (SCM_NULLP(smob)) return 0; else if (SCM_SMOB_PREDICATE(swig_tag, smob) || SCM_SMOB_PREDICATE(swig_collectable_tag, smob) || SCM_SMOB_PREDICATE(swig_destroyed_tag, smob)) { return (unsigned long) (void *) SCM_CELL_WORD_1(smob); } else scm_wrong_type_arg("SWIG-Guile-PointerAddress", 1, object); }
SCM gucu_panel_userdata (SCM pan) { if (_scm_is_panel (pan)) { PANEL *c_panel = _scm_to_panel (pan); return (SCM) (panel_userptr (c_panel)); } else scm_wrong_type_arg ("set-panel-userdata", SCM_ARG1, pan); /* Never reached */ return SCM_UNDEFINED; }
void scm_dynstack_push_dynamic_state (scm_t_dynstack *dynstack, SCM state, scm_t_dynamic_state *dynamic_state) { scm_t_bits *words; SCM state_box; if (SCM_UNLIKELY (scm_is_false (scm_dynamic_state_p (state)))) scm_wrong_type_arg ("with-dynamic-state", 0, state); state_box = scm_make_variable (scm_set_current_dynamic_state (state)); words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_DYNAMIC_STATE, 0, DYNAMIC_STATE_WORDS); words[0] = SCM_UNPACK (state_box); }
SCM python_import(SCM smodulename) { if (!SCM_STRINGP(smodulename)) { scm_wrong_type_arg("python-import",SCM_ARG1,smodulename); } else { char *mname = scm_to_locale_string(smodulename); PyObject *pmodule = PyImport_ImportModule(mname); PyObject *pexception = PyErr_Occurred(); if (pexception) { PyObject *prepr = PyObject_Repr(pexception); Py_XDECREF(pmodule); PyErr_Clear(); SCM smname = scm_list_1(scm_mem2string(mname,strlen(mname))); free(mname); if (NULL == prepr) { scm_misc_error("python-import", // NOT COVERED BY TESTS "Python exception during module ~A import - could not be identified", smname); } else { int strlength = PyString_Size(prepr); char *pstr = PyString_AsString(prepr); SCM slist = scm_list_2(SCM_CAR(smname),scm_mem2string(pstr,strlength)); Py_DECREF(prepr); scm_misc_error("python-import", "Python exception during module ~A import: ~A", slist); } } // OK, exception did not occur. Do we have a module? if (NULL == pmodule) { SCM slist = scm_list_1(scm_mem2string(mname,strlen(mname))); free(mname); scm_misc_error("python-eval","could not import module ~S", slist); } free(mname); SCM smodule = wrap_pyobject(pmodule); Py_DECREF(pmodule); // wrap_pyobject did Py_INCREF return(smodule); } }
static SPDPriority assign_priority(SCM priority, const char *func_name) { char *c_priority; SCM_ASSERT(SCM_SYMBOLP(priority), priority, SCM_ARG3, func_name); c_priority = SCM_SYMBOL_CHARS(priority); { const int invalid_priority = -1000; int int_priority = ((!strcmp(c_priority, "important")) ? SPD_IMPORTANT : (!strcmp(c_priority, "message")) ? SPD_MESSAGE : (!strcmp(c_priority, "text")) ? SPD_TEXT : (!strcmp(c_priority, "notification")) ? SPD_NOTIFICATION : (!strcmp(c_priority, "progress")) ? SPD_PROGRESS : invalid_priority); if (int_priority == invalid_priority) scm_wrong_type_arg(func_name, SCM_ARG3, priority); return int_priority; } }
// Get the panel below PAN SCM gucu_panel_below (SCM pan) { if (_scm_is_panel (pan)) { PANEL *c_pan = _scm_to_panel (pan); PANEL *c_pan2 = panel_below (c_pan); return _scm_from_panel (c_pan2); } else if (pan == SCM_BOOL_F) { PANEL *c_pan2 = panel_below (0); return _scm_from_panel (c_pan2); } else scm_wrong_type_arg ("panel-below", SCM_ARG1, pan); /* Never reached */ return SCM_BOOL_F; }
/* The fluid is stored on the stack, but the value has to be stored on the heap, so that all continuations that capture this dynamic scope capture the same binding. */ void scm_dynstack_push_fluid (scm_t_dynstack *dynstack, SCM fluid, SCM value, scm_t_dynamic_state *dynamic_state) { scm_t_bits *words; SCM value_box; if (SCM_UNLIKELY (!SCM_FLUID_P (fluid))) scm_wrong_type_arg ("with-fluid*", 0, fluid); value_box = scm_make_variable (value); words = push_dynstack_entry (dynstack, SCM_DYNSTACK_TYPE_WITH_FLUID, 0, WITH_FLUID_WORDS); words[0] = SCM_UNPACK (fluid); words[1] = SCM_UNPACK (value_box); /* Go ahead and swap them. */ scm_swap_fluid (fluid, value_box, dynamic_state); }
// python_apply implements the function call: // (python-apply ("module.submodule" 'obj 'func) // (arg1 arg2 arg3) // (('keyword1 . val4) ('keyword2 . val5)) // sargtemplate // skwtemplate) // which is the basic way to invoke a Python function. // // sfunc specifies the function to be invoked. The possibilities // are: // String - denotes a top level function ("func" means __main__.func). // pysmob - assumed to be a callable object. // ("module.submodule" ...) - a List of strings/symbols/keywords // in which the first item must be a string denotes: // Module "module.submodule" (which should have already been imported // using python-import) // followed by name of object in that module, followed by attribute,..., // until the final callable attribute. // (pysmob ...) - a List starting with pysmob followed by // strings/symbols/keywords - processed similarly, except that the // pysmob stands for the module. // sarg is a list of arguments (in Python it's *arg) // skw is an alist (in Python it's **kw). // sargtemplate - specifies how to convert sarg - optional argument. // skwtemplate - specifies how to convert skw - optional argument. // srestemplate - specifies how to convert the result back into // SCM - optional argument. SCM python_apply(SCM sfunc, SCM sarg, SCM skw, SCM sargtemplate, SCM skwtemplate, SCM srestemplate) { PyObject *pfunc = NULL; PyObject *parg = NULL; PyObject *pkw = NULL; PyObject *pfuncobj = NULL; PyObject *pres = NULL; SCM sres = SCM_UNDEFINED; if (SCM_UNBNDP(sargtemplate)) { //(sargtemplate == SCM_UNDEFINED) // SCM_UNSPECIFIED sargtemplate = sargtemplate_default; } if (SCM_UNBNDP(skwtemplate)) { skwtemplate = skwtemplate_default; } if (SCM_UNBNDP(srestemplate)) { srestemplate = srestemplate_default; } // Retrieve the function object. pfunc = guile2python(sfunc,SCM_UNSPECIFIED); if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) { char *preprfunc = PyString_AsString(PyObject_Repr(pfunc)); scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded pfunc ~S\n"),scm_list_1(scm_makfrom0str(preprfunc))); } if (NULL == pfunc) { scm_misc_error("python-apply","conversion failure (~S)", scm_list_1(SCM_CDR(sfunc))); } // If it is a string, prepend it with "__main__". if (PyString_CheckExact(pfunc)) { // Convert it into a List of two items, to unify // subsequent treatment. PyObject *plist = PyList_New(2); if (NULL == plist) { Py_DECREF(pfunc); // NOT COVERED BY TESTS scm_memory_error("python-apply"); // NOT COVERED BY TESTS } if (-1 == PyList_SetItem(plist,0,PyString_FromString("__main__"))) { Py_DECREF(pfunc); // NOT COVERED BY TESTS Py_DECREF(plist); // NOT COVERED BY TESTS scm_misc_error("python-apply","PyList_SetItem 0 failure (~S)", // NOT COVERED BY TESTS scm_list_1(SCM_CAR(sfunc))); } if (-1 == PyList_SetItem(plist,1,pfunc)) { Py_DECREF(pfunc); // NOT COVERED BY TESTS Py_DECREF(plist); // NOT COVERED BY TESTS scm_misc_error("python-apply","PyList_SetItem 1 failure (~S)", // NOT COVERED BY TESTS scm_list_1(SCM_CAR(sfunc))); } pfunc = plist; // plist stole previous pfunc's value's reference. } else if (IS_PYSMOBP(sfunc)) { // We check the SCM object because guile2python() destroys // the indication whether the SCM was originally a pysmob, when it // converts it into PyObject. PyObject *plist1 = PyList_New(1); if (NULL == plist1) { Py_DECREF(pfunc); // NOT COVERED BY TESTS scm_memory_error("python-apply"); // NOT COVERED BY TESTS } if (-1 == PyList_SetItem(plist1,0,pfunc)) { Py_DECREF(pfunc); // NOT COVERED BY TESTS Py_DECREF(plist1); // NOT COVERED BY TESTS scm_misc_error("python-apply","PyList_SetItem 0 failure (~S)", // NOT COVERED BY TESTS scm_list_1(SCM_CAR(sfunc))); } pfunc = plist1; // plist1 stole previous pfunc's value's reference. // Now pfunc is an 1-member list, and this member is // expected to be callable. } else if (!PyList_CheckExact(pfunc)) { // Now, the qualified function name must be a proper list. scm_wrong_type_arg("python-apply",SCM_ARG1,sfunc); } if (1 > PyList_Size(pfunc)) { // The list must consist of at least one callable module name/object. scm_misc_error("python-apply", "first argument must contain at least one callable object (~S)", scm_list_1(SCM_CAR(sfunc))); } if (PyString_CheckExact(PyList_GetItem(pfunc,0))) { // If it is a string, we assume it to be the name of a module // which has already been imported. // Due to the existence of dots, // we don't allow it to be symbol or keyword. pfuncobj = PyImport_AddModule(PyString_AsString(PyList_GetItem(pfunc,0))); if (NULL == pfuncobj) { Py_DECREF(pfunc); scm_misc_error("python-apply", "module ~S could not be accessed - probably not imported", scm_list_1(SCM_CAR(sfunc))); } Py_INCREF(pfuncobj); } else { // We assume that it is a callable or object with attributes. pfuncobj = PyList_GetItem(pfunc,0); if (NULL == pfuncobj) { Py_DECREF(pfunc); scm_misc_error("python-apply", "could not access object starting ~S", scm_list_1(sfunc)); } Py_INCREF(pfuncobj); } // Here we dereference attributes (if any). int listsize = PyList_Size(pfunc); int ind; for (ind = 1; ind < listsize; ++ind) { PyObject *pnextobj = PyObject_GetAttr(pfuncobj,PyList_GetItem(pfunc,ind)); if (NULL == pnextobj) { PyObject *pexception = PyErr_Occurred(); Py_DECREF(pfunc); Py_DECREF(pfuncobj); if (pexception) { PyErr_Clear(); // An AttributeError exception is expected here. if (!PyErr_GivenExceptionMatches(pexception,PyExc_AttributeError)) { PyObject *prepr = PyObject_Repr(pexception); if (NULL == prepr) { scm_misc_error("python-apply", "python exception - could not be identified", SCM_UNSPECIFIED); } else { int strlength = PyString_Size(prepr); char *pstr = PyString_AsString(prepr); SCM srepr = scm_list_1(scm_mem2string(pstr,strlength)); Py_DECREF(prepr); scm_misc_error("python-apply", "Python exception (~A) while dereferencing object attribute", srepr); } } // else we got the expected AttributeError exception. } // else we got NULL==pnextobj without Python exception. scm_misc_error("python-apply", "could not dereference ~Ath level attribute in ~S", scm_list_2(scm_long2num(ind),sfunc)); } Py_INCREF(pnextobj); Py_DECREF(pfuncobj); pfuncobj = pnextobj; } Py_DECREF(pfunc); // We do not need it anymore. pfuncobj points at // the function actually to be invoked. if (!PyCallable_Check(pfuncobj)) { Py_DECREF(pfuncobj); scm_misc_error("python-apply","function denoted by ~S is not callable",scm_list_1(sfunc)); } if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) { char *preprfuncobj = PyString_AsString(PyObject_Repr(pfuncobj)); scm_simple_format(scm_current_output_port(), scm_makfrom0str("# python_apply: decoded function actually to be invoked: ~S\n"), scm_list_1(scm_makfrom0str(preprfuncobj))); } // Retrieve positional arguments parg = g2p_apply(sarg,sargtemplate); if (NULL == parg) { Py_DECREF(pfuncobj); scm_misc_error("python-apply","positional arguments conversion failure (~S)", scm_list_1(sarg)); } // Validate that it is indeed a tuple. if (!PyTuple_CheckExact(parg)) { Py_DECREF(pfuncobj); Py_DECREF(parg); scm_wrong_type_arg("python-apply",SCM_ARG2,sarg); } if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) { char *pposarg = PyString_AsString(PyObject_Repr(parg)); scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded positional arguments ~S\n"),scm_list_1(scm_makfrom0str(pposarg))); } // Retrieve keyword arguments. pkw = guileassoc2pythondict(skw,skwtemplate); if (NULL == pkw) { // Seems that PyDict_CheckExact() does not handle NULL argument gracefully. Py_DECREF(pfuncobj); Py_DECREF(parg); scm_misc_error("python-apply","keyword arguments conversion failure (~S)", scm_list_1(skw)); } if (!PyDict_CheckExact(pkw)) { Py_DECREF(pfuncobj); Py_DECREF(parg); Py_DECREF(pkw); scm_misc_error("python-apply", "keyword arguments (~S) not properly converted into Python Dict", scm_list_1(skw)); } if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) { char *pkwarg = PyString_AsString(PyObject_Repr(pkw)); scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded keyword arguments ~S\n"),scm_list_1(scm_makfrom0str(pkwarg))); } // Ready to invoke the function. pres = PyEval_CallObjectWithKeywords(pfuncobj,parg,pkw); PyObject *pexception = PyErr_Occurred(); if (pexception) { PyObject *prepr = PyObject_Repr(pexception); Py_DECREF(pfuncobj); Py_DECREF(parg); Py_DECREF(pkw); Py_XDECREF(pres); PyErr_Clear(); if (NULL == prepr) { scm_misc_error("python-apply", "python exception - could not be identified", SCM_UNSPECIFIED); } else { int strlength = PyString_Size(prepr); char *pstr = PyString_AsString(prepr); SCM srepr = scm_list_1(scm_mem2string(pstr,strlength)); Py_DECREF(prepr); scm_misc_error("python-apply","Python exception: ~A", srepr); } } if (NULL != pres) { sres = p2g_apply(pres,srestemplate); if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) { char *presstr = PyString_AsString(PyObject_Repr(pres)); scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: decoded results:\n# Python: ~S\n# Scheme: ~S\n"),scm_list_2(scm_makfrom0str(presstr),sres)); } } else { // else sres remains SCM_UNDEFINED. if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYTHON_APPLY)) { scm_simple_format(scm_current_output_port(),scm_makfrom0str("# python_apply: Python code returned <NULL>\n"),SCM_EOL); } } return(sres); }
SCM python_eval(SCM sobj,SCM smode) { if (!SCM_STRINGP(sobj)) { scm_wrong_type_arg("python-eval",SCM_ARG1,sobj); } int start = (SCM_UNBNDP(smode)) || (SCM_EQ_P(SCM_BOOL_F,smode)) ? Py_file_input : Py_eval_input; char *pstr = scm_to_locale_string(sobj); if (NULL == pstr) { scm_memory_error("python-eval"); // NOT COVERED BY TESTS //return(SCM_UNSPECIFIED); } PyObject *pmaindict = PyModule_GetDict(PyImport_AddModule("__main__")); if (NULL == pmaindict) { scm_misc_error("python-eval","could not get __main__ for (~S), mode ~A", // NOT COVERED BY TESTS scm_list_2(sobj,smode)); } Py_INCREF(pmaindict); PyObject *pres = PyRun_String(pstr, start, pmaindict, pmaindict); Py_DECREF(pmaindict); free(pstr); PyObject *pexception = PyErr_Occurred(); if (pexception) { PyObject *prepr = PyObject_Repr(pexception); Py_XDECREF(pres); PyErr_Clear(); if (NULL == prepr) { scm_misc_error("python-eval", // NOT COVERED BY TESTS "python exception - could not be identified", SCM_UNSPECIFIED); } else { int strlength = PyString_Size(prepr); char *pstr = PyString_AsString(prepr); SCM slist = scm_list_1(scm_mem2string(pstr,strlength)); Py_DECREF(prepr); scm_misc_error("python-eval","Python exception: ~A", slist); } } switch(start) { case Py_eval_input: { if (NULL != pres) { SCM sres = p2g_apply(pres, SCM_EQ_P(SCM_BOOL_T,smode) ? srestemplate_default : smode); Py_DECREF(pres); return(sres); } else { scm_misc_error("python-eval","could not return result of evaluation", SCM_UNSPECIFIED); return(SCM_UNSPECIFIED); } } case Py_file_input: default: { Py_XDECREF(pres); return(SCM_UNSPECIFIED); } } }