I tried adding a Python3 module in C

I tried to make a program written in C usable from Python 3 All the references are old and I had a hard time making it compatible with Python3 ...

First of all, the source code

hello.c


int add(int x, int y)
{
	return x + y;
}

void out(const char* adrs, const char* name)
{
	printf("Hello, I%s%s.\n", adrs, name);
}

This is the C source code, which has two functions, add and out.

helloWrapper.c


#include "Python.h"
extern int add(int, int);
extern void out(const char*, const char*);

PyObject* hello_add(PyObject* self, PyObject* args)
{
	int x, y, g;

	if (!PyArg_ParseTuple(args, "ii", &x, &y))
		return NULL;
	g = add(x, y);
	return Py_BuildValue("i", g);
}

PyObject* hello_out(PyObject* self, PyObject* args, PyObject* kw)
{
	const char* adrs = NULL;
	const char* name = NULL;
	static char* argnames[] = {"adrs", "name", NULL};

	if (!PyArg_ParseTupleAndKeywords(args, kw, "|ss",
			argnames, &adrs, &name))
		return NULL;
	out(adrs, name);
	return Py_BuildValue("");
}

static PyMethodDef hellomethods[] = {
	{"add", hello_add, METH_VARARGS},
	{"out", hello_out, METH_VARARGS | METH_KEYWORDS},
	{NULL}
};

static struct PyModuleDef hellomodule =
{
    PyModuleDef_HEAD_INIT,
    "hellomodule", /* name of module */
    "",            /* module documentation, may be NULL */
    -1,            /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
    hellomethods
};

PyMODINIT_FUNC PyInit_hellomodule(void)
{
    return PyModule_Create(&hellomodule);
}

This is the main source code.

See here for the meaning of each program → Call a C program from Python

Try to run

compile

Execute three commands.

gcc -fpic -o hello.o -c hello.c
gcc -fpic -I`python3-config --prefix`/Headers -o helloWrapper.o -c helloWrapper.c
gcc -L`python3-config --prefix`/lib -lpython3.5 -shared hello.o helloWrapper.o -o hellomodule.so

Try from python3

Let's try it out, is it really done?

hnkz $ python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import hellomodule
>>> hellomodule.add(2,3)
5
>>> hellomodule.out("hamamatsu","hnkz")
Hello, I am hnkz of hamamatsu.

It's done! Wow! !!

I want to apply it and make a keylogger with Python3.

References

Call a C program from Python I tried C binding in Python Compiler can't find Py_InitModule() .. is it deprecated and if so what should I use?

Recommended Posts

I tried adding a Python3 module in C
I tried playing a typing game in Python
[Memo] I tried a pivot table in Python
I tried Python C extension
Try to make a Python module in C language
I tried to implement a pseudo pachislot in Python
ABC166 in Python A ~ C problem
Solve ABC036 A ~ C in Python
I made a module in C language to filter images loaded by Python
Solve ABC037 A ~ C in Python
I tried Line notification in Python
I tried to implement a one-dimensional cellular automaton in Python
I tried "a program that removes duplicate statements in Python"
I created a class in Python and tried duck typing
I tried to make a stopwatch using tkinter in python
I tried to implement PLSA in Python
Solve ABC175 A, B, C in Python
I tried to implement permutation in Python
I tried to implement PLSA in Python 2
I tried using Bayesian Optimization in Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I created a password tool in Python.
I tried to implement a misunderstood prisoner's dilemma game in Python
A memo that I wrote a quicksort in Python
Create a Python module
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I wrote a class in Python3 and Java
Next Python in C
I tried to implement TOPIC MODEL in Python
I tried reading a CSV file using Python
I tried to develop a Formatter that outputs Python logs in JSON
To add a module to python put in Julialang
I tried using the Datetime module by Python
I tried non-blocking I / O Eventlet behavior in Python
Call a Python script from Embedded Python in C ++ / C ++
A story that didn't work when I tried to log in with the Python requests module
C API in Python 3
I tried running alembic, a Python migration tool
I tried to implement selection sort in python
I tried Python> decorator
I tried to implement a card game of playing cards in Python
I made a Caesar cryptographic program in Python.
I tried to create a class that can easily serialize Json in Python
I tried to implement what seems to be a Windows snipping tool in Python
Draw a graph in Julia ... I tried a little analysis
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I made a prime number generation program in Python
I tried using google test and CMake in C
I tried to draw a route map with Python
I want to write in Python! (2) Let's write a test
I tried running python etc. from a bat file
Try embedding Python in a C ++ program with pybind11
I made a Python module to translate comment outs
I want to randomly sample a file in Python
I tried to implement Dragon Quest poker in Python
I want to work with a robot in python.
I tried to implement GA (genetic algorithm) in Python