osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Embedding Python in C


Am 18.07.19 um 16:18 schrieb Jesse Ibarra:
> On Wednesday, July 17, 2019 at 2:20:51 PM UTC-6, Christian Gollwitzer wrote:
>> What level of integration do you want to achieve? Do you want
>>
>> a) to call Python functions from Smalltalk
>> b) call Smalltalk functions from Python
>> c) pass callbacks around, e.g. use a Smalltalk function within a Python
>> list comprehension, and if so, which way
>> d) integrate the class systems - derive a Python class from a Smalltalk
>> base or the other way round
>>

> 
> For right now, I need to call a .so file from Smalltalk. I can't explicitly use Python libraries since Smalltalk does not support Python. I need to use the C/Python API (in Smalltalk) to create a bridge where I can call a .so and a function in that .so file with a PyObject (This will be called back to Smalltalk from the .so file). I can't figure out a way to do that since the C/API can't call C or .so files.
> 
> sorry for the confusion on my part
> 

I still don't get it, sorry. To me it is unclear which part of the 
integration you manage to do so far, and which part is the problem.

Which Smalltalk interpreter are you using? The answer to the following 
will heavily depend on that.


Suppose I'd give you a C file with the following simple function:


double add(double a, double b) {
	return a+b;
}

Do you know how to compile this code and make it usable from Smalltalk?

One level up, consider a C function working on an array:

double arrsum(int n, double *arr) {
	double sum=0.0;
	for (int i=0; i<n; i++) sum+=arr[i];
	return sum;
}


How would you compile and link this with your Smalltalk implementation, 
such that you can pass it a Smalltalk array?

Once you can do this, you can proceed to call a Python function, which 
in C means that you invoke the function PyObject_CallObject(). A basic 
example is shown here:

https://docs.python.org/2/extending/embedding.html#pure-embedding

	Christian