I tried Python C extension

I copied the first half of the tutorial extension of Python with C and C ++. I was thinking of studying Cython, but I happened to see a blog post about the above method, so I felt like trying it. I have the impression that it will not be that difficult. I don't know the API or anything yet. .. Really squeeze spammodule.c as a tutorial. Paste the comments as they are in the tutorial text.

spammodule.c


#include <Python.h>

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
  const char *command;
  int sts;

  if (!PyArg_ParseTuple(args, "s", &command))
    /*A Python string or Unicode object that points to a character string
Convert to C pointer. "s"Meaning of
    */
      return NULL;

  sts = system(command);
  return Py_BuildValue("i", sts);
  /*A new Python object that takes a formatted string and any number of C values as arguments
return it. "i"Means an integer object*/
}

static PyMethodDef SpamMethods[] = {
  {"system", spam_system, METH_VARARGS,
   "Description Execute a shell command."},
  {NULL, NULL, 0, NULL}
};


PyMODINIT_FUNC
initspam(void)
{
  (void) Py_InitModule("spam", SpamMethods);
}

After writing safely, create setup.py as follows. I referred to this article .

setup.py



from distutils.core import setup, Extension

module1 = Extension('spam',
                    sources = ['spammodule.c'])

setup (name = 'Spam',
       version = '1.0',
       description = 'This is a spam package',
       ext_modules = [module1])

The rest is on the command line

$python setup.py build

(This time it's an experiment, so I won't install it) To execute. Then, a directory called build will be created directly under the current. I think that the contents differ depending on the environment, but if you specify the path of spam.so and import it It seems that it can be used. I copied and pasted spam.so into a suitable directory (Is it okay?) It was a suitable memo.

Recommended Posts

I tried Python C extension
Python C / C ++ Extension Pattern-Pointer
I tried Python> autopep8
I tried Python> decorator
I tried adding a Python3 module in C
I tried fp-growth with python
I tried scraping with Python
[Python] I tried using OpenPose
I tried gRPC with Python
I tried scraping with python
[Python] I immediately tried using Pylance's VS Code extension.
I tried to touch Python (installation)
I tried web scraping with python.
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I tried running prolog with python 3.8.2.
I tried Line notification in Python
I tried SMTP communication with Python
[Python] I tried using YOLO v3
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
Wrangle x Python book I tried it [2]
I tried scraping
I tried scraping Yahoo News with Python
I tried PyQ
I started python
Python3 standard input I tried to summarize
I tried sending an email with python.
I tried using Bayesian Optimization in Python
I tried AutoKeras
I tried using UnityCloudBuild API from Python
python C ++ notes
I tried to implement ADALINE in Python
I tried a functional language with Python
python, openFrameworks (c ++)
I tried recursion with Python ② (Fibonacci sequence)
I tried to implement PPO in Python
I tried papermill
Python: I tried the traveling salesman problem
Wrangle x Python book I tried it [1]
Mayungo's Python Learning Episode 8: I tried input
[Python] I tried to calculate TF-IDF steadily
I tried scraping Yahoo weather (Python edition)
I tried to touch Python (basic syntax)
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I tried the Python Tornado Testing Framework
#I tried something like Vlookup with Python # 2
[Python / DynamoDB / boto3] List of operations I tried
I tried hundreds of millions of SQLite with python
[Python] I tried substituting the function name for the function name
Python C / C ++ Extension Pattern-Pass data to Python as np.array
vprof --I tried using the profiler for Python
I tried "differentiating" the image with Python + OpenCV
I tried object detection using Python and OpenCV
I tried playing a typing game in Python
I tried the least squares method in Python
I tried L-Chika with Raspberry Pi 4 (Python edition)