C language to see and remember Part 3 Call C language from Python (argument) c = a + b

Receives a + b as a return value from Python with integers a and b as arguments.

capi.py


import myModule as capi
a,b=10,20
c=capi.Cal(a,b) #c=a+b
print(c)

When you run

>python capi.py
30

capilib.c


#include <Python.h>
static PyObject* Cal(PyObject* self, PyObject* args){
    int a,b,c;
    if (!PyArg_ParseTuple(args, "ll",&a,&b)){
        return NULL;
    }
    c=a+b;
    return Py_BuildValue("l", c);
}
// Function Definition struct
static PyMethodDef myMethods[] = {
    { "Cal",Cal, METH_VARARGS, "Calc a+b"},
    { 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 is **

    int a,b,c;
    if (!PyArg_ParseTuple(args, "ll",&a,&b)){
        return NULL;
    }
    c=a+b;
    return Py_BuildValue("l", c);
}

--"ll" puts two sets of int variables into variables a and b. --c = a + b Returns as an "l" integer after execution.

setup.py


from distutils.core import setup, Extension
setup(name = 'myModule', version = '1.0.0',  \
   ext_modules = [Extension('myModule', ['capilib.c'])])

** Creating a library **

capi>python setup.py install
running install
running build
running build_ext
building 'myModule' extension
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -IC:\ProgramData\Anaconda3\include -IC:\ProgramData\Anaconda3\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /Tccapilib.c /Fobuild\temp.win-amd64-3.7\Release\capilib.obj
capilib.c
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /nodefaultlib:libucrt.lib ucrt.lib /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\ProgramData\Anaconda3\libs /LIBPATH:C:\ProgramData\Anaconda3\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64" /EXPORT:PyInit_myModule build\temp.win-amd64-3.7\Release\capilib.obj /OUT:build\lib.win-amd64-3.7\myModule.cp37-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.7\Release\myModule.cp37-win_amd64.lib
Library build\temp.win-amd64-3.7\Release\myModule.cp37-win_amd64.lib and object build\temp.win-amd64-3.7\Release\myModule.cp37-win_amd64.Creating exp
I'm generating code.
Code generation is complete.
running install_lib
copying build\lib.win-amd64-3.7\myModule.cp37-win_amd64.pyd -> C:\ProgramData\Anaconda3\Lib\site-packages
running install_egg_info
Removing C:\ProgramData\Anaconda3\Lib\site-packages\myModule-1.0.0-py3.7.egg-info
Writing C:\ProgramData\Anaconda3\Lib\site-packages\myModule-1.0.0-py3.7.egg-info

Recommended Posts

C language to see and remember Part 3 Call C language from Python (argument) c = a + b
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 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
[Python] How to call a c function from python (ctypes)
Call c language from python (python.h)
Call C language functions from Python to exchange multidimensional arrays
Call a Python script from Embedded Python in C ++ / C ++
Try to make a Python module in C language
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➀
Call Matlab from Python to optimize
Python a + = b and a = a + b are different
Call a Python function from p5.js.
Call C from Python with DragonFFI
ABC127 A, B, C Explanation (python)
Call popcount from Ruby / Python / C #
Introduction to Protobuf-c (C language ⇔ Python)
ABC128 A, B, C commentary (python)
[Python] return A [or / and] B
ABC126 A, B, C Explanation (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
Simple code to call a python program from Javascript on EC2
[C language] How to create, avoid, and make a zombie process
Call dlm from python to run a time-varying coefficient regression model
Pass a list by reference from Python to C ++ with pybind11
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)
Solve ABC175 A, B, C in Python
From Python to using MeCab (and CaboCha)
Send a message from Python to Slack
python> keyword arguments> hoge (** {'a': 1,'b': 2,'c': 3})
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
Connect to postgreSQL from Python and use stored procedures in a loop.
Porting and modifying doublet-solver from python2 to python3.
Socket communication by C language and Python
Generate C language from S-expressions in Python
Call a command from Python (Windows version)
I want to pass an argument to a python function and execute it from PHP on a web server
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 1)
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)
I made a module in C language to filter images loaded by Python
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
Send a message from Slack to a Python server
Run a Python script from a C # GUI application
Edit Excel from Python to create a PivotTable
How to generate permutations in Python and C ++
Create a C array from a Python> Excel sheet
Ported a naive homebrew language compiler to Python
[Python] How to read data from CIFAR-10 and CIFAR-100
How to generate a Python object from JSON
python memo-"if not A and B" was "if (not A) and B"
List of Python code to move and remember