Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do programming languages call code written in another language?

Sorry if this is too vague. I was recently reading about python's list.sort() method and read that it was written in C for performance reasons.

I'm assuming that the python code just passes a list to the C code and the C code passes a list back, but how does the python code know where to pass it or that C gave it the correct data type, and how does the C code know what data type it was given?

like image 306
Igglyboo Avatar asked Jan 21 '14 14:01

Igglyboo


People also ask

How do I translate a code from one language to another?

Compilers convert one programming language into another. Usually, compilers are used to convert code so the machine can understand it. If we want it to be human-readable, we need a subset of compilers called transpilers. Transpilers also convert code however the output is generally understandable by a human.

Can code be written in other languages?

Over a third of programming language were developed in English speaking countries. But some of the well-known, highly-used coding languages were developed in non-English speaking countries e.g. Switzerland (PASCAL), Denmark (PHP), Japan (Ruby), Brazil (Lua), and The Netherlands (Python).

Can Python invoke codes written in other languages?

You can run almost any language on the shell.

How do programs in different languages communicate?

Programming languages work together by being compiled or interpreted. Essentially this means using some sort of element to change one language into another or allowing it to be understood by a different language.


2 Answers

Python can be extended in C/C++ (more info here)

It basically means that you can wrap a C module like this

#include "Python.h"

// Static function returning a PyObject pointer
static PyObject *
keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds) 
// takes self, args and kwargs.
{ 
    int voltage;
    // No such thing as strings here. Its a tough life.
    char *state = "a stiff";
    char *action = "voom";
    char *type = "Norwegian Blue";
    // Possible keywords
    static char *kwlist[] = {"voltage", "state", "action", "type", NULL};

    // unpack arguments
    if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
                                     &voltage, &state, &action, &type))
        return NULL;
    // print to stdout
    printf("-- This parrot wouldn't %s if you put %i Volts through it.\n",
           action, voltage);
    printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);

    // Reference count some None.
    Py_INCREF(Py_None);
    // return some none.
    return Py_None;
}
// Static PyMethodDef
static PyMethodDef keywdarg_methods[] = {
    /* The cast of the function is necessary since PyCFunction values
     * only take two PyObject* parameters, and keywdarg_parrot() takes
     * three.
     */
    // Declare the parrot function, say what it takes and give it a doc string.
    {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS | METH_KEYWORDS,
     "Print a lovely skit to standard output."},
    {NULL, NULL, 0, NULL}   /* sentinel */
};

And using the Python header files it will define and understand entry points and return locations in the C/C++ code.

like image 51
Jakob Bowyer Avatar answered Oct 21 '22 17:10

Jakob Bowyer


I can't speak to Python/C interaction directly, but I can give some background to how these sorts of things work in general.

On a particular platform or implementation, there is a calling convention that specifies how parameters are passed to subroutines and how values are returned to the caller. Compilers and interpreters that target that platform or implementation generate code to conform to that convention, so that subroutines/modules/whatever written in different languages can communicate with each other.

In my assembly class, we had an assignment where we had to write a program using VAX assembler, C, and Pascal (this was in the mid-Cretaceous1980s). The driver was in one of C or Pascal (can't remember which anymore), which called the assembly routine, which called the other routine (which was written in whichever language the driver wasn't). Our assembly code had to pop and push parameters from the stack based on the VMS calling convention.

like image 24
John Bode Avatar answered Oct 21 '22 17:10

John Bode