static int pyscm_PySCM_setattr(pyscm_PySCMObject *self, char *name, PyObject *v) { /* Set attribute 'name' to value 'v'. v==NULL means delete */ if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYSCM)) { scm_simple_format(scm_current_output_port(),scm_makfrom0str("# pyscm_PySCM_setattr: trying to set attribute=~S from pobj=~S to value ~S\n"),scm_list_3(scm_makfrom0str(name),verbosity_repr((PyObject *)self),verbosity_repr(v))); } SCM sobj_keyword; SCM sattr_vector = retrieve_sattr_vector(self,name,&sobj_keyword); if (SCM_UNBNDP(sattr_vector)) { // Attribute error exception was raised by retrieve_sattr_vector(). return(-1); } SCM ssetattr_func = GET_H_SETATTR_FUNC(sattr_vector); if (SCM_EQ_P(SCM_EOL,ssetattr_func)) { PyErr_SetString(PyExc_AttributeError, name); return(-1); } if (NULL != v) { SCM sval = p2g_apply(v, GET_H_P2G_SETATTR_TEMPLATE(sattr_vector)); scm_append_x(scm_list_2(sobj_keyword,sval)); } SCM sresult = scm_apply(ssetattr_func,sobj_keyword,SCM_EOL); return(SCM_EQ_P(SCM_BOOL_F,sresult) ? (-1) : 0); }
// Common code for pyscm_PySCM_getattr() and pyscm_PySCM_setattr(): // Retrieve and return the 4-element vector corresponding to desired // attribute of the pyscm_PySCMObject. // Perform also validity checking and raise Python exception if // invalid. // Since it is needed later, also the SCM object, corresponding to the // pyscm_PySCMObject, is returned to the caller, put into 2-element // list together with the #:-keyword corresponding to name. static SCM retrieve_sattr_vector(pyscm_PySCMObject *self, char *name, SCM *sobj_keyword) { SCM shandle = scm_hashv_get_handle(pyscm_registration_hash,scm_long2num(self->ob_scm_index)); if (SCM_BOOLP(shandle) && SCM_EQ_P(SCM_BOOL_F,shandle)) { Py_FatalError("PySCM object lost its associated SCM object"); } // Now: // SCM_CADR(shandle) is the SCM object itself // SCM_CDDR(shandle) is the stemplate. SCM sattrshash = GET_ATTRS_HASH(SCM_CDDR(shandle)); if (SCM_EQ_P(SCM_BOOL_F,sattrshash)) { PyErr_SetString(PyExc_AttributeError, name); return(SCM_UNDEFINED); // Error return } // The object has attributes. Build the hash key (a keyword). size_t nlength = strlen(name); char *dashstr = malloc(nlength+2); dashstr[0] = '-'; dashstr[1] = '\0'; strncat(dashstr,name,nlength); SCM skeyword = scm_make_keyword_from_dash_symbol(scm_str2symbol(dashstr)); // !!! Do we have to free dashstr? // !!! Similar code is used also in pytoguile.c - review it. SCM sattr_vector_handle = scm_hashv_get_handle(sattrshash,skeyword); if (SCM_EQ_P(SCM_BOOL_F,sattr_vector_handle)) { // Missing attribute. How to deal with it? sattr_vector_handle = scm_hashv_get_handle(sattrshash,SCM_BOOL_F); if (SCM_EQ_P(SCM_BOOL_F,sattr_vector_handle)) { // Hash value corresponding to key #f is itself another #f, which // means that the object does not wish to exhibit to Python // unknown attributes. PyErr_SetString(PyExc_AttributeError, name); return(SCM_UNDEFINED); // Error return } // Otherwise, we'll use the hash value corresponding to #f as // a catch-all for all attributes not otherwise defined. } *sobj_keyword = scm_list_2(SCM_CADR(shandle),skeyword); return(SCM_CDR(sattr_vector_handle)); }
static PyObject * pyscm_PySCM_call(pyscm_PySCMObject *self, PyObject *args, PyObject *kwargs) { /* Return the result of calling self with argument args */ SCM shandle = scm_hashv_get_handle(pyscm_registration_hash,scm_long2num(self->ob_scm_index)); if (SCM_BOOLP(shandle) && SCM_EQ_P(SCM_BOOL_F,shandle)) { Py_FatalError("PySCM object lost its associated SCM object"); // NOT COVERED BY TESTS } // Now: // SCM_CADR(shandle) is the SCM object itself // SCM_CDDR(shandle) is the stemplate. if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYSCM)) { scm_simple_format(scm_current_output_port(),scm_makfrom0str("# pyscm_PySCM_call: calling ~S with args=~S and keywords=~S; stemplate=~S\n"),scm_list_4(SCM_CADR(shandle),verbosity_repr(args),verbosity_repr(kwargs),SCM_CDDR(shandle))); } SCM sapply_func = GET_APPLY_FUNC(SCM_CDDR(shandle)); if (SCM_EQ_P(SCM_EOL,sapply_func)) { if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYSCM)) { scm_simple_format(scm_current_output_port(),scm_makfrom0str("# pyscm_PySCM_call: raising exceptions.TypeError due to \"PySCM wraps a non-callable SCM\"\n"),SCM_EOL); } PyErr_SetString(PyExc_TypeError, "PySCM wraps a non-callable SCM"); return(NULL); } // Process arguments. SCM sargs_template = GET_P2G_POSITIONAL_ARGS_TEMPLATE(SCM_CDDR(shandle)); SCM skwargs_template = GET_P2G_KEYWORD_ARGS_TEMPLATE(SCM_CDDR(shandle)); /*if (logical_xor(SCM_EQ_P(SCM_EOL,sargs_template),(NULL==args)) || logical_xor(SCM_EQ_P(SCM_EOL,skwargs_template),(NULL==kwargs)))*/ // The following allows template to exist without actual arguments. if ((SCM_EQ_P(SCM_EOL,sargs_template) && (NULL != args)) || (SCM_EQ_P(SCM_EOL,skwargs_template) && (NULL != kwargs))) { if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYSCM)) { scm_simple_format(scm_current_output_port(),scm_makfrom0str("# pyscm_PySCM_call: raising exceptions.TypeError due to \"wrapped SCM does not take some of the provided arguments\"\n"),SCM_EOL); } PyErr_SetString(PyExc_TypeError, "wrapped SCM does not take some of the provided arguments"); return(NULL); } SCM sargs = SCM_EQ_P(SCM_EOL,sargs_template) || (NULL == args) ? SCM_EOL : p2g_apply(args,sargs_template); SCM skwargs = SCM_EQ_P(SCM_EOL,skwargs_template) || (NULL == kwargs) ? SCM_EOL : p2g_apply(kwargs,skwargs_template); SCM sresult = scm_apply(sapply_func,scm_list_2(SCM_CADR(shandle),scm_list_2(sargs,skwargs)),SCM_EOL); SCM sresult_template = GET_G2P_RESULT_TEMPLATE(SCM_CDDR(shandle)); if (SCM_EQ_P(SCM_EOL,sresult_template)) { Py_RETURN_NONE; } else { return(g2p_apply(sresult,sresult_template)); } }
// Does not include the template object in the string representation. static PyObject * pyscm_PySCM_str(pyscm_PySCMObject *self) { if (0 == self->ob_scm_index) { return(PyString_FromString("<no SCM association>")); } SCM shandle = scm_hashv_get_handle(pyscm_registration_hash,scm_long2num(self->ob_scm_index)); if (SCM_BOOLP(shandle) && SCM_EQ_P(SCM_BOOL_F,shandle)) { Py_FatalError("PySCM object lost its associated SCM object"); } SCM sstr = scm_object_to_string(SCM_CADR(shandle),scm_variable_ref(scm_c_lookup("write"))); PyObject *pstr = PyString_FromStringAndSize(SCM_STRING_CHARS(sstr),SCM_STRING_LENGTH(sstr)); return(pstr); // possibly NULL. }
static PyObject * pyscm_PySCM_getattr(pyscm_PySCMObject *self, char *name) { if (pyguile_verbosity_test(PYGUILE_VERBOSE_PYSCM)) { scm_simple_format(scm_current_output_port(),scm_makfrom0str("# pyscm_PySCM_getattr: trying to get attribute=~S from pobj=~S\n"),scm_list_2(scm_makfrom0str(name),verbosity_repr((PyObject *)self))); } SCM sobj_keyword; SCM sattr_vector = retrieve_sattr_vector(self,name,&sobj_keyword); if (SCM_UNBNDP(sattr_vector)) { // Attribute error exception was raised by retrieve_sattr_vector(). return(NULL); } SCM sgetattr_func = GET_H_GETATTR_FUNC(sattr_vector); if (SCM_EQ_P(SCM_EOL,sgetattr_func)) { PyErr_SetString(PyExc_AttributeError, name); return(NULL); } SCM stemplate = GET_H_G2P_GETATTR_TEMPLATE(sattr_vector); SCM sresult = scm_apply(sgetattr_func,sobj_keyword,SCM_EOL); return(g2p_apply(sresult,stemplate)); }
int weechat_guile_command_cb (void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { char *ptr_name, *path_script; SCM value; /* make C compiler happy */ (void) data; (void) buffer; if (argc == 1) { plugin_script_display_list (weechat_guile_plugin, guile_scripts, NULL, 0); } else if (argc == 2) { if (weechat_strcasecmp (argv[1], "list") == 0) { plugin_script_display_list (weechat_guile_plugin, guile_scripts, NULL, 0); } else if (weechat_strcasecmp (argv[1], "listfull") == 0) { plugin_script_display_list (weechat_guile_plugin, guile_scripts, NULL, 1); } else if (weechat_strcasecmp (argv[1], "autoload") == 0) { plugin_script_auto_load (weechat_guile_plugin, &weechat_guile_load_cb); } else if (weechat_strcasecmp (argv[1], "reload") == 0) { weechat_guile_unload_all (); plugin_script_auto_load (weechat_guile_plugin, &weechat_guile_load_cb); } else if (weechat_strcasecmp (argv[1], "unload") == 0) { weechat_guile_unload_all (); } else return WEECHAT_RC_ERROR; } else { if (weechat_strcasecmp (argv[1], "list") == 0) { plugin_script_display_list (weechat_guile_plugin, guile_scripts, argv_eol[2], 0); } else if (weechat_strcasecmp (argv[1], "listfull") == 0) { plugin_script_display_list (weechat_guile_plugin, guile_scripts, argv_eol[2], 1); } else if ((weechat_strcasecmp (argv[1], "load") == 0) || (weechat_strcasecmp (argv[1], "reload") == 0) || (weechat_strcasecmp (argv[1], "unload") == 0)) { ptr_name = argv_eol[2]; if (strncmp (ptr_name, "-q ", 3) == 0) { guile_quiet = 1; ptr_name += 3; while (ptr_name[0] == ' ') { ptr_name++; } } if (weechat_strcasecmp (argv[1], "load") == 0) { /* load guile script */ path_script = plugin_script_search_path (weechat_guile_plugin, ptr_name); weechat_guile_load ((path_script) ? path_script : ptr_name); if (path_script) free (path_script); } else if (weechat_strcasecmp (argv[1], "reload") == 0) { /* reload one guile script */ weechat_guile_reload_name (ptr_name); } else if (weechat_strcasecmp (argv[1], "unload") == 0) { /* unload guile script */ weechat_guile_unload_name (ptr_name); } guile_quiet = 0; } else if (weechat_strcasecmp (argv[1], "eval") == 0) { /* eval guile code */ value = weechat_guile_catch (scm_c_eval_string, argv_eol[2]); if (!SCM_EQ_P (value, SCM_UNDEFINED) && !SCM_EQ_P (value, SCM_UNSPECIFIED)) { scm_display (value, guile_port); } weechat_guile_stdout_flush (); } else return WEECHAT_RC_ERROR; } return WEECHAT_RC_OK; }
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); } } }
int weechat_guile_command_cb (void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) { char *path_script; SCM value; /* make C compiler happy */ (void) data; (void) buffer; if (argc == 1) { script_display_list (weechat_guile_plugin, guile_scripts, NULL, 0); } else if (argc == 2) { if (weechat_strcasecmp (argv[1], "list") == 0) { script_display_list (weechat_guile_plugin, guile_scripts, NULL, 0); } else if (weechat_strcasecmp (argv[1], "listfull") == 0) { script_display_list (weechat_guile_plugin, guile_scripts, NULL, 1); } else if (weechat_strcasecmp (argv[1], "autoload") == 0) { script_auto_load (weechat_guile_plugin, &weechat_guile_load_cb); } else if (weechat_strcasecmp (argv[1], "reload") == 0) { weechat_guile_unload_all (); script_auto_load (weechat_guile_plugin, &weechat_guile_load_cb); } else if (weechat_strcasecmp (argv[1], "unload") == 0) { weechat_guile_unload_all (); } } else { if (weechat_strcasecmp (argv[1], "list") == 0) { script_display_list (weechat_guile_plugin, guile_scripts, argv_eol[2], 0); } else if (weechat_strcasecmp (argv[1], "listfull") == 0) { script_display_list (weechat_guile_plugin, guile_scripts, argv_eol[2], 1); } else if (weechat_strcasecmp (argv[1], "load") == 0) { /* load Guile script */ path_script = script_search_path (weechat_guile_plugin, argv_eol[2]); weechat_guile_load ((path_script) ? path_script : argv_eol[2]); if (path_script) free (path_script); } else if (weechat_strcasecmp (argv[1], "reload") == 0) { /* reload one Guile script */ weechat_guile_reload_name (argv_eol[2]); } else if (weechat_strcasecmp (argv[1], "unload") == 0) { /* unload Guile script */ weechat_guile_unload_name (argv_eol[2]); } else if (weechat_strcasecmp (argv[1], "eval") == 0) { /* eval Guile code */ value = weechat_guile_catch (scm_c_eval_string, argv_eol[2]); if (!SCM_EQ_P (value, SCM_UNDEFINED) && !SCM_EQ_P (value, SCM_UNSPECIFIED)) { scm_display (value, guile_port); } weechat_guile_stdout_flush (); } else { weechat_printf (NULL, weechat_gettext ("%s%s: unknown option for " "command \"%s\""), weechat_prefix ("error"), GUILE_PLUGIN_NAME, "guile"); } } return WEECHAT_RC_OK; }