What do programming languages ​​call code written in another language?

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

I assume that python code just passes the list to C code, and C code passes the list back, but how does python code know where to pass it, or what C gave it the correct data type and how does C code know which data type it gave?

+4
source share
3 answers

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

This basically means that you can wrap module C 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!\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 */
};

Python, C/++.

+6

( ) (ABI). , , , ..

ABI , ():

  • ( , 32) (,% EAX R3). ..
  • , .
  • .
  • F1, F2 .., , .
  • ( ) , (, char ) , .

, ABI, , , .

+1

Python/C, , .

, , , , , , // , , .

, VAX, C Pascal ( 1980- ). C Pascal ( ), , ( , ). VMS.

+1

All Articles