C language to see and remember Part 2 Call C language from Python (argument) string

capi.py


import myModule as capi
print(capi.Message("Hello, Japan"))

Execute
capi>python capi.py
Hello, Japan
Hello, Japan

capilib.c


#include <Python.h>

static PyObject* Message(PyObject* self, PyObject* args){
    char* str;
    if (!PyArg_ParseTuple(args, "s",&str)){
        return NULL;
    }
    printf("%s\n",str);
    return Py_BuildValue("s", str);
}
// Function Definition struct
static PyMethodDef myMethods[] = {
    { "Message", Message, METH_VARARGS, "Prints Message"},
    { NULL }
};
static struct PyModuleDef myModule = {
    PyModuleDef_HEAD_INIT,"myModule","C API Module",-1,myMethods
};
PyMODINIT_FUNC PyInit_myModule(void){
    return PyModule_Create(&myModule);
}

** Attention point **

char* str;
//Capture arguments
if (!PyArg_ParseTuple(args, "s",&str)){
    return NULL;
}
//Creating a return value
return Py_BuildValue("s", str);

--PyArg_ParseTuple (args, "s", & str) reads the argument "s" string into str. --The return value "s" represents a string. -Example https://docs.python.org/2.0/ext/buildValue.html

Recommended Posts

C language to see and remember Part 2 Call C language from Python (argument) string
C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 5 Call C language from Python (argument) Array
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
C language to see and remember Part 1 Call C language from Python (hello world)
Go language to see and remember Part 8 Call GO language from Python
Go language to see and remember Part 7 C language in GO language
Call c language from python (python.h)
Call C language functions from Python to exchange multidimensional arrays
[Python] How to call a c function from python (ctypes)
Call Matlab from Python to optimize
Call C from Python with DragonFFI
Try to make a Python module in C language
Go language to see and remember Part 7 C language in GO language
Call popcount from Ruby / Python / C #
Introduction to Protobuf-c (C language ⇔ Python)
Python to switch from another language
Call C / C ++ from Python on Mac
[Python] Hit Keras from TensorFlow and TensorFlow from c ++ to speed up execution
How to get a string from a command line argument in python
Call your own C language shared library from Python using ctypes
[It's not too late to learn Python from 2020] Part 3 Python Language Basic (1)
From Python to using MeCab (and CaboCha)
Porting and modifying doublet-solver from python2 to python3.
Socket communication by C language and Python
Generate C language from S-expressions in Python
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 1)
How to generate permutations in Python and C ++
Writing logs to CSV file (Python, C language)
[Python] How to read data from CIFAR-10 and CIFAR-100
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 3)
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➀
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 2)
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
Call a Python script from Embedded Python in C ++ / C ++
List of Python code to move and remember
I want to make C ++ code from Python code!
An easy way to call Java from Python
The story of porting code from C to Go and getting hooked (and to the language spec)
Try to make a Python module in C language
python> datetime> From date string (ISO format: 2015-12-09 12:40:08) to datetime type
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Introduction to Python language
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Python regular expression basics and tips to learn from scratch
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
I tried to illustrate the time and time in C language
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Pass OpenCV data from the original C ++ library to Python
How to call Python or Julia from Ruby (experimental implementation)
Call CPLEX from Python (DO cplex)
Post from Python to Slack
Python indentation and string format
Cheating from PHP to Python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Switch from python2.7 to python3.6 (centos7)
Connect to sqlite from python
Ported from R language of "Sazae-san's rock-paper-scissors data analysis" to Python