forum.vmspython.org Forum Index forum.vmspython.org
Forum system
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Python and C modules

 
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS
View previous topic :: View next topic  
Author Message
GerardH



Joined: 04 Apr 2012
Posts: 1

PostPosted: Wed Apr 04, 2012 3:11 pm    Post subject: Python and C modules Reply with quote

Hi,

I am trying to bind python with c-modules.

I tried an example but it doesn't work. I tried the spammodule example.

Can anyone explain what i have to do to get this working?

I used the following code for spammodule.c:

Code:
#include "Python.h"

static PyObject *SpamError;

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    //sts = system(command);
    sts = 0;
    if (sts < 0) {
        PyErr_SetString(SpamError, "System command failed");
        return NULL;
    }
    return PyLong_FromLong(sts);
}

static PyMethodDef SpamMethods[] = {
    {"system",  spam_system, METH_VARARGS,
     "Execute a shell command."},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

PyMODINIT_FUNC
initspam(void)
{
    PyObject *m;

    m = Py_InitModule("spam", SpamMethods);
    if (m == NULL)
        return;

    SpamError = PyErr_NewException("spam.error", NULL, NULL);
    Py_INCREF(SpamError);
    PyModule_AddObject(m, "error", SpamError);
}

I found out that i have to compile this with:

cc/name=as_is/include=python_include/accept=novaxc_keywords spammodule.c

And now comes the difficult part.

The function 'system' is not found (undefined symbol vms_system), so i have disabled that call.

I suppose i have to link it as a shareable image? It becomes very difficult to find what i have to do to use it in python.

The steps i have made is:
create a directory [.spam]
in this directory i compiled the spammodule.c
i then used the following command:

link /exe=spam.exe spammodule.obj, spam.opt/opt/share

the file spam.opt:
CASE_SENSITIVE=YES
pythonshr/share

I made a file __init__.py with:
Code:
from spam import *

__version__ = '0.1'

in the parent directory i started python and typed:
import spam

I now got the message:
ImportError: dynamic module does not define init function (initspam)

So i think i got almost there, but not yet.
Back to top
View user's profile Send private message
jfp



Joined: 12 Jul 2004
Posts: 618

PostPosted: Fri Apr 06, 2012 2:52 pm    Post subject: Reply with quote

Hello,

A correct linker option file would be:
Code:
GSMATCH=lequal,1,1000
case_sensitive=YES
SYMBOL_VECTOR = (initspam=PROCEDURE)
SYMBOL_VECTOR = (INITSPAM/initspam=PROCEDURE)
spammodule.obj
pythonshr/share
case_sensitive=NO

Be sure to use the very latest image LD (see http://www.vmspython.org/History), I have fixed a problem when importing .exe module

JF
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group