0

I have an import to import a shared object library created in c. It looks like this:

import Cal

I then try to make a call in python with the shared object library like so:

status = Cal.Cal_readFile(filename, result)

However when I hit this code when running my python file I get the following error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1541, in __call__
    return self.func(*args)
  File "xcal.py", line 72, in openDialog
    status = Cal.Cal_readFile(filename, result)
AttributeError: 'module' object has no attribute 'Cal_readFile'

My makefile regarding the shared object library looks like this:

CC = gcc
CFLAGS = -Wall -std=c11 -g -DNDEBUG `pkg-config --cflags python3`
CFLAGS += -fPIC
LDFLAGS=`pkg-config --libs python3`

all: caltool Cal.so

Cal.so: calmodule.o calutil.o
    $(CC) -shared $(LDFLAGS) $^ -o $@

calutil.o: calutil.c calutil.h

calmodule.o: calmodule.c calutil.c

And my C code looks like the following:

#include <python3.4/Python.h>
#include "calutil.h"

PyObject *Cal_readFile( PyObject *self, PyObject *args );

static PyMethodDef CalMethods[] = {
  {"readFile", Cal_readFile, METH_VARARGS},
  {NULL, NULL} 
};

static struct PyModuleDef calModuleDef = {
  PyModuleDef_HEAD_INIT,
  "Cal", //enable "import Cal"
  NULL, //omit module documentation
  -1, //don't reinitialize the module
  CalMethods //link module name "Cal" to methods table 
};
PyMODINIT_FUNC PyInit_Cal(void) { 
  return PyModule_Create( &calModuleDef ); 
}

PyObject *Cal_writeFile( PyObject *self, PyObject *args ) {
  return NULL;
}

Note: The Cal_writeFile function does actually have stuff happening in it, just I didn't want to paste the big function as it likely isn't needed to solve this.

Is there something I am doing wrong here? I feel like that Python import Cal should be properly be importing the shared object library named Cal.so that is in the same directory as the Python file.

1 Answer 1

2
static PyMethodDef CalMethods[] = {
  {"readFile", Cal_readFile, METH_VARARGS},
  {NULL, NULL} 
};

The Python-visible name you gave your function is readFile. You need to access it by that name, not Cal_readFile.

Sign up to request clarification or add additional context in comments.

3 Comments

THANK YOU! That resolved the problem and I'll accept your answer as soon as it lets me.
A quick question, if I modify the result variable I pass to the C function within that C function, will it modify the Python variable as well?
@Fogest: It's not clear what you mean by that. In any case, if you want to ask another question, that's what the "ask question" button is for, not the "add comment" button. You'll get more visibility, you and any potential answerers have more room to add detail and more formatting options, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.