/ src / emc / pythonplugin / testpp.cc
testpp.cc
  1  /*    This is a component of LinuxCNC
  2   *    Copyright 2011, 2012 Michael Haberler <git@mah.priv.at>
  3   *
  4   *    This program is free software; you can redistribute it and/or modify
  5   *    it under the terms of the GNU General Public License as published by
  6   *    the Free Software Foundation; either version 2 of the License, or
  7   *    (at your option) any later version.
  8   *
  9   *    This program is distributed in the hope that it will be useful,
 10   *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 11   *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12   *    GNU General Public License for more details.
 13   *
 14   *    You should have received a copy of the GNU General Public License
 15   *    along with this program; if not, write to the Free Software
 16   *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 17   */
 18  // test harness for python_plugin
 19  
 20  #include "python_plugin.hh"
 21  #include <ctype.h>
 22  #include <stdio.h>
 23  #include <stdlib.h>
 24  #include <unistd.h>
 25  
 26  #define CHK(bad, fmt, ...)					       \
 27      do {							       \
 28  	if (bad) {						       \
 29  	    fprintf(stderr,fmt, ## __VA_ARGS__);		       \
 30  	    exit(1);						       \
 31  	}							       \
 32      } while(0)
 33  
 34  const char *strstore(const char *s) { return strdup(s); }
 35  extern PythonPlugin *python_plugin;
 36  
 37  void analyze(const char *what,bp::object retval)
 38  {
 39      PyObject *res_str = PyObject_Str(retval.ptr());
 40      Py_XDECREF(res_str);
 41      printf("analyze: %s returned '%s' - '%s'\n",what,
 42  	   PyString_AsString(res_str),
 43  	   retval.ptr()->ob_type->tp_name);
 44  }
 45  
 46  void exercise(PythonPlugin *pp,const char *mod, const char*func,  bp::tuple tupleargs,   bp::dict kwargs)
 47  {
 48  
 49      bp::object r;
 50  
 51      bool callable = pp->is_callable(mod,func);
 52      printf("callable(%s%s%s) = %s\n",mod ? mod : "", mod ? ".":"",
 53  	   func, callable ? "TRUE": "FALSE");
 54  
 55  
 56      int status = pp->call(mod,func, tupleargs,kwargs,r);
 57      switch (status) {
 58      case PLUGIN_EXCEPTION:
 59  	printf("call(%s%s%s): exception='%s' status = %d\n",
 60  	       mod ? mod : "", mod ? ".":"",func,pp->last_exception().c_str(), status);
 61  	break;
 62      case PLUGIN_OK:
 63  	printf("call(%s%s%s): OK\n",  mod ? mod : "", mod ? ".":"",func);
 64  	analyze( func,r);
 65  	break;
 66      default:
 67  	printf("call(%s%s%s): status = %d\n", mod ? mod : "", mod ? ".":"",func,status);
 68      }
 69  }
 70  
 71  void run(PythonPlugin *pp,const char *cmd, bool as_file)
 72  {
 73      bp::object r;
 74      int status = pp->run_string(cmd,r, as_file);
 75      printf("run_string(%s): status = %d\n", cmd,status);
 76      analyze(cmd,r);
 77  }
 78  
 79  void foo_init()
 80  {
 81      printf("foo init\n");
 82  }
 83  void bar_init()
 84  {
 85      printf("bar init\n");
 86  }
 87  
 88  const char *inifile = "test.ini";
 89  const char *section = "PYTHON";
 90  struct _inittab builtin_modules[] = {
 91      { (char *) "foo", foo_init },
 92      { (char *) "bar", bar_init },
 93      // any others...
 94      { NULL, NULL }
 95  };
 96  
 97  int builtins;
 98  bool as_file = false;
 99  int
100  main (int argc, char **argv)
101  {
102  
103      int index;
104      int c;
105      bp::object retval, pythis;
106      char *callablefunc = NULL;
107      char *callablemod = NULL;
108      char *xcallable = NULL;
109      int status;
110  
111      opterr = 0;
112  
113      while ((c = getopt (argc, argv, "fbi:C:c:x:")) != -1) {
114  	switch (c)  {
115  	case 'f':
116  	    as_file = true;
117  	    break;
118  	case 'b':
119  	    builtins++;
120  	    break;
121  	case 'i':
122  	    inifile = optarg;
123  	    break;
124  	case 'C':
125  	    callablemod = optarg;
126  	    break;
127  	case 'c':
128  	    callablefunc = optarg;
129  	    break;
130  	case 'x':
131  	    xcallable = optarg;
132  	    break;
133  	case '?':
134  	    if (optopt == 'c')
135  		fprintf (stderr, "Option -%c requires an argument.\n", optopt);
136  	    else if (isprint (optopt))
137  		fprintf (stderr, "Unknown option `-%c'.\n", optopt);
138  	    else
139  		fprintf (stderr,
140  			 "Unknown option character `\\x%x'.\n",
141  			 optopt);
142  	    return 1;
143  	default:
144  	    abort ();
145  	}
146      }
147  
148      if (!PythonPlugin::instantiate(builtins ? builtin_modules : NULL)) {
149  	fprintf (stderr,"instantiate: cant instantiate Python plugin");
150      }
151  
152      // creates the singleton instance
153      status = python_plugin->configure(inifile, section);
154  
155      if (status != PLUGIN_OK) {
156  	fprintf (stderr, "configure failed: %d\n",status);
157      }
158  
159      // PythonPlugin two = python_plugin; // this fails since copy constructor is private.
160      // PythonPlugin &second = PythonPlugin::getInstance();  // returns the singleton instance
161  
162      printf("status = %d\n", python_plugin->plugin_status());
163      bp::tuple tupleargs,nulltupleargs;
164      bp::dict kwargs,nullkwargs;
165      kwargs['x'] = 10;
166      kwargs['y'] = 20;
167      tupleargs = bp::make_tuple(3,2,1,"foo");
168  
169      exercise(python_plugin,NULL,"func",nulltupleargs, nullkwargs);
170  
171      system("/usr/bin/touch testmod.py");
172  
173      exercise(python_plugin,NULL,"badfunc",tupleargs,kwargs);
174      exercise(python_plugin,NULL,"retstring",tupleargs,kwargs);
175      exercise(python_plugin,NULL,"retdouble",tupleargs,kwargs);
176      // exercise(python_plugin,"submod","subfunc");
177  
178      for (index = optind; index < argc; index++) {
179  	run(python_plugin, argv[index], as_file);
180      }
181      return 0;
182  }