FW: passing a C struct to python

Lichtenstein, Mark (Contractor) mlichten at harris.com
Tue Apr 22 14:31:30 EDT 2003


-----Original Message-----
From: Lichtenstein, Mark (Contractor) 
Sent: Tuesday, April 22, 2003 2:13 PM
To: 'Rajarshi Guha'
Subject: RE: passing a C struct to python


I suggest you place the object in the interpreter class. I will attach some java
code to show how this is done. See interp.set statements:

/* %filename%.java
 * Copyright Harris Corporation, 2002. All rights reserved.
 */
package com.harris.netboss.shared.model.rules.commands;

import com.harris.netboss.shared.model.rules.ICommand;
import com.harris.netboss.shared.model.rules.IContext;
import com.harris.netboss.shared.model.rules.PythonFactory;
import com.harris.netboss.shared.model.rules.commands.SetTimer;
import javax.management.Notification;
import org.python.util.PythonInterpreter;
import org.python.core.PyDictionary;
import org.python.core.PyObject;
import java.io.InputStream;
import java.util.Map;
import org.python.core.PyList;
import org.python.core.PyTuple;
import java.util.HashMap;

/**
 * PythonAdaptor ilements the Adaptor pattern.  
 * It implements an ICommand interface, repackages the arguments 
 * and invokes a jython script.
 *
 * @author Mark Lichtenstein
 * @version %I%, %G%
 * @since NextGen
 * @see com.netboss.harris.shared.model.rules
 */
public class PythonAdaptor implements ICommand {

/** execute: executes the jython interpreter.
 * @param arg[] The input argument to this command. (i.e. Jython Script, URL...)
 * @param arg[0] - an InputStream or String of jython
 * @param arg[1...] - any argument that is a java class.
 * @param event The Indication that triggered this command to be invoked.
 */
    public void execute(Notification event, Object[] arg)
    {
        // Get a new or existing PythonInterpreter from PythonFactory
        IContext context = (IContext) arg[0]; // set context from object array
        PythonInterpreter interp = PythonFactory.getInstance(context);
        PyObject localsObject;
        SetTimer setTimer = new SetTimer(); // Create object for Jython
        // Get the local variables in a PyObject if there is a context map
        if (context != null) {
           localsObject = (PyObject) context.get("LOCALS");
           if (localsObject != null)
              interp.setLocals(localsObject);
        }  // end if
	// Pass variables to interpreter's local namespace so that 
	// they may be accessed in a Jython (.py) script
	interp.set("event",(Object) event); 
	interp.set("context",(Object) context); 
        interp.set("setTimer",(Object) setTimer);
	interp.set("args",(Object) arg); 
        if (arg[1] instanceof String)  { // Interpret a string
            interp.exec((String)arg[1]); // run the string
        }  // end if
        if (arg[1] instanceof InputStream) { // execute a file
           InputStream is = (InputStream) arg[1];
           interp.execfile(is);
        }  // end if
        // After executing the jython command(s) we need to restore the
        // context map using any local variables found in the local namespace.
        if (context != null) {
            localsObject = interp.getLocals(); // Get local namespace variables
            context.put("LOCALS",(PyObject)localsObject);
        }
    }  // end of execute method

    /** toMap: Extract a Python dictionary into a Java Map.
    @param interp The Python interpreter object
    @param pyName The id of the python dictionary
    */
    public static Map toMap(PythonInterpreter interp, String pyName){
      PyList pa = ((PyDictionary)interp.get(pyName)).items();
      Map map = new HashMap();
      while(pa.__len__() != 0) {
        PyTuple po = (PyTuple)pa.pop();
        Object first = po.__finditem__(0).__tojava__(Object.class);
        Object second = po.__finditem__(1).__tojava__(Object.class);
        map.put(first, second);
      } // end of while loop
      return map;
   }

}

-----Original Message-----
From: Rajarshi Guha [mailto:rajarshi at presidency.com]
Sent: Tuesday, April 22, 2003 1:57 PM
To: python-list at python.org
Subject: passing a C struct to python


Hi,
  I've recently looked into calling some Python functions from my C code.
I've worked out the ways of calling a Python function
(PyObject_CallMethodObjArgs etc). However these are OK if I want to pass
a list, tuple, string etc.

However in some cases I might want to pass  an 'object' (which would be a
C struct) to the Python function. Alternatively, the Python function
might require me to pass an instance of some class. Am I correct in
thinking that these two problems are equivilant?

And my question is: what do I need to do to pass structs/classes etc? I
tried looking for pointers in the Embedding section of the Python manual,
but I could'nt really find anything :(

Any pointers would be appreciated

Thanks,
-- 
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list