(.env) [boris@fedora33server YANDEXDBA]$ cat test.h
#pragma once
#include <cstddef>
#include <vector>
namespace abc {
std::vector<int> catchList(int q,int m,int pw1,int pw2) ;
}
(.env) [boris@fedora33server YANDEXDBA]$ cat test.cpp
#include <Python.h>
#include <iostream>
#include <vector>
#include <cmath>
#include "test.h"
#pragma GCC diagnostic ignored "-Wsign-compare"
namespace abc {
int digitSum(int n)
{
int sum=0,m;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
return sum;
}
std::vector<int> catchList(int k,int m,int pw1,int pw2) {
std::vector<int> result;
int j,z;
for( j=k; j < m; ++j)
{
z = digitSum(j);
if (pow(double(z),pw1) == pow(double(j),pw2))
result.push_back(j);
}
return result;
}
}
extern "C"{}
namespace {
static PyObject *caughtList(PyObject* self, PyObject* args)
{
int p,n;
int pwr1,pwr2;
// Now four arguments pass through PyArg_ParseTuple
if(!PyArg_ParseTuple(args,"iiii" ,&p,&n,&pwr1,&pwr2))
return NULL;
std::vector<int>numbers = abc::catchList(p,n,pwr1,pwr2);
PyObject* result = PyList_New(numbers.size());
for(int i = 0; i < numbers.size(); i++) {
PyList_SetItem(result, i, PyLong_FromLong(numbers[i]));
}
return result;
};
// Our Module's Function Definition struct
// We require this `NULL` to signal the end of our method
// definition
static PyMethodDef myMethods[] = {
{ "caughtList", caughtList, METH_VARARGS, "Calculate prime numbers" },
{ NULL, NULL, 0, NULL }
};
// Our Module Definition struct
static struct PyModuleDef myModule = {
PyModuleDef_HEAD_INIT,
"myModule",
"Test Module",
-1,
myMethods
};
// Initializes our module using our above struct
PyMODINIT_FUNC PyInit_myModule(void)
{
return PyModule_Create(&myModule);
};
} // namespace(.env)
[boris@fedora33server YANDEXDBA]$ cat setup.py
from distutils.core import setup, Extension
import sysconfig
language = 'c++'
std = 'c++20'
default_compile_args = sysconfig.get_config_var('CFLAGS').split()
extra_compile_args = [f"-std={std}", "-Wall", "-Wextra", "-Werror", "-DNDEBUG", "-O3"]
setup(name = 'myModule', version = '1.0', \
ext_modules = [Extension('myModule', ['test.cpp'])])
running install
running build
running build_ext
building 'myModule' extension
creating build
creating build/temp.linux-x86_64-3.9
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/home/boris/YANDEXDBA/.env/include -I/usr/include/python3.9 -c test.cpp -o build/temp.linux-x86_64-3.9/test.o
creating build/lib.linux-x86_64-3.9
g++ -pthread -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -g build/temp.linux-x86_64-3.9/test.o -L/usr/lib64 -o build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so
running install_lib
copying build/lib.linux-x86_64-3.9/myModule.cpython-39-x86_64-linux-gnu.so -> /home/boris/YANDEXDBA/.env/lib64/python3.9/site-packages
running install_egg_info
Removing /home/boris/YANDEXDBA/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info
Writing /home/boris/YANDEXDBA/.env/lib64/python3.9/site-packages/myModule-1.0-py3.9.egg-info
No comments:
Post a Comment