Add python modules to cython for use in c - c

I am currently trying to write a plugin backend in c by using .so files. Doing this in c works as I expect it to. However I thought about writing python plugins for my backend. Here is when i stumbled upon cython which seems to be very promising.
My backend is calling a function within the .so files and expects a value in return.
This function currently looks like this:
cdef public size_t transform_data(char *plugin_arguments, char **buffer):
printf("Entered function\n")
print("test\n")
printf("Test passed\n")
return 5
The interesting part is, that the printf works just fine. However the print doesn't. I suspect this is because there is some sort of linking error to a python module that I am missing? Also later on I would like to be able to add any python module to that file, for example the influxdb module. A call to influxdb.InfluxDBClient doesn't work either right now, I guess for the same reason that the print is not working.
I am compiling the file using
cythonize -3b some_plugin.pyx
and I have also tried to compile using a setup file that looks like this:
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("some_plugin.pyx"))
both resulting to a segfault as soon as I hit the print call.
Here is the code that I am using to call the .so file:
#include "execute_plugin.h"
#include <Python.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
size_t execute_plugin(char file_name[FILE_NAME_SIZE], char *plugin_arguments,
char **output_buffer) {
if (!Py_IsInitialized()) {
Py_SetPythonHome(L"/home/flo/.local/lib/python3.8");
Py_SetPath(L"/usr/lib/python3.8");
Py_Initialize();
}
if (!Py_IsInitialized())
return 0;
void *plugin;
size_t (*func_transform_data)(char *plugin_arguments, char **output_buffer);
char path[PATH_SIZE];
if (!get_path_to_file(path, PATH_SIZE)) {
printf("Could not receive the correct path to the plugin %s\n", file_name);
return 0;
}
plugin = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
if (!plugin) {
fprintf(stderr, "Error: %s\n", dlerror());
fprintf(stderr, "Cannot load %s\n", file_name);
return 0;
}
func_transform_data =
(size_t(*)(char *plugin_arguments, char **output_buffer))dlsym(
plugin, "transform_data");
if (!func_transform_data) {
fprintf(stderr, "Error: %s\n", dlerror());
dlclose(plugin);
return 0;
}
size_t length = func_transform_data(plugin_arguments, output_buffer);
printf("Size of answer is %ld\n", length);
dlclose(plugin);
Py_Finalize();
return length;
}
I have tried using the documentation and just copied the example: https://cython.readthedocs.io/en/latest/src/tutorial/embedding.html
In this example I didn't use an .so file but the .c and .h file that is also getting generated by the cythonize command. Interestingly enough the print function is working but as soon as I try to add another module like the influxdb module and try to call a function from it I also get errors.
Since I have not found a lot about using cython code in c I am wondering if what I am trying to do is even possible or if there is a better approach.

It's difficult to be certain what your issue is because you don't actually show how you call your Cython function. So what follows is a guess.
Cython does not produce standalone C functions. It produces functions that are part of a Python module and require that module to be initialized. What is probably happening is that Cython caches the lookup to the global print function as part of the module initialization. Since you skip the initialization the cached print function is not set up (hence the crash).
When you read the documentation, you'd have seen that all the examples ("Using Cython declarations from C" and "Embedding Cython modules...") import the module before using it. You must do this. The preferred way to do this is with PyImport_AppendInittab().
A second possibility is that you haven't initialized the Python interpreter. Again, this is not optional.

Related

Application crashes using Py_DECREF on string PyObject in .dll built with Python C API

I am building a DLL to embed Python 2.7 (2.7.18 to be specific) in a larger application largely following python.org's C API documentation. When I believe I am applying Py_DECREF correctly to a PyString PyObject, any linking application crashes without error. The code is largely copied from a similar application built with python 3.8, 3.9 and 3.10 that in fact works.
// python_functions.h
__declspec(dllexport) int init_python();
// python_functions.c
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <stdio.h> // printf
#include <string.h>
//#include <wchar.h> // wchar_t for python3 only
#include "python_functions.h"
__declspec(dllexport) int init_python() {
int rv = -1;
printf("attempting to initialize Python interpreter...\n");
// I have other python versions, Python27 not on path. set PYTHONHOME
Py_SetPythonHome("C:\\Python27\\"); // python27
//Py_SetPythonHome(L"C:\\Python38\\"); // python3X
Py_Initialize();
PyObject * pModule_Name = PyString_FromString("python_functions"); // python27 only
//PyObject * pModule_Name = PyUnicode_FromString("python_functions"); // python3X
if (!pModule_Name) {
printf("failed to convert module name to python string object\n");
return rv;
}
// can confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
/*
load python module named "python_functions"
*/
// can also confirm pModule_Name != NULL && Py_REFCNT(pModule_Name) == 1 here
//Py_DECREF(pModule_Name); // If I uncomment this line, application linking .dll crashes
printf("pModule_Name successfully used\n");
rv = 0;
return rv;
}
// main.c
#include <stdio.h>
#include "python_functions.h"
int main(int argc, char **argv) {
int rv = 0;
// crashes without error if Py_DECREF(pModule_Name) in python_functions.c uncommented
printf("%d\n", init_python());
return rv;
}
Compiling the .dll with gcc 8.1.0 (MinGW-64) linking against C:\Python27\python27.dll:
gcc -IC:\Python27\include -LC:\Python27\libs -IC:\Python27 python_functions.c -shared -o python_functions.dll -lpython27
Compiling main with python27.dll and python_functions.dll in the same directory:
gcc main.c -o main.exe -L./ -lpython_functions
If I uncomment the line Py_DECREF(pModule_Name), which I thought I must do since PyString_FromString returns a new reference, then running main.exe crashes without return.
Should I be using something else or not doing reference counting on result from PyString_FromString or is this a larger problem with linking to python27?
If I make the commented/corresponding compiler changes to link against python 3.8+, no problems...
The only differences I know of are the significant changes to the Python C API between 2.7 and e.g. 3.8+ as well as the differences in python 2.X vs 3.X string representations. I believe PyString is appropriate here because when I actually implement and load a module name python_functions.py, it works...so long as I leave Py_DECREF(pModule_Name) commented out and the pointer leaked.
I also thought PyString_FromString might have a problem passing a string literal, but it accepts a const char *, documentation says it creates a copy, and even passing a copy of the string literal to another variable does not work.

Is there a way to compile and execute dynamically generated code in C?

Is it possible to execute C code in a C program? For instance when reading input from the user.
There's nothing built in to do this.
This simplest thing to do is save the given code to a separate file, invoke GCC as a separate process to compile the code, then run the compiled code in a new process.
Relatively easy: write C code to temporary file, invoke cc on temporary file to create shared library, use dlopen to load in and call functions in shared library
Harder: write C code to temporary file, invoke cc on temporary file to create conventional .o file, write your own dynamic linker to to load in and call functions in .o file
Harder: write a C interpreter to interpret C code directly
If you're targetting x86/ARM and Unix/Linux, you might find libtcc of use:
#include <libtcc.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
TCCState *s = tcc_new();
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
if (tcc_compile_string(s,
"#include <stdio.h>\n"
"void hello(void) {\n"
" printf(\"Hello world\\n\");\n"
"}\n"
) != 0)
{
fprintf(stderr, "Failed to compile the code\n");
exit(2);
}
tcc_relocate(s, TCC_RELOCATE_AUTO);
void (*hello)(void) = tcc_get_symbol(s, "hello");
hello();
}

Get program's directory on windows [duplicate]

Is there a platform-agnostic and filesystem-agnostic method to obtain the full path of the directory from where a program is running using C/C++? Not to be confused with the current working directory. (Please don't suggest libraries unless they're standard ones like clib or STL.)
(If there's no platform/filesystem-agnostic method, suggestions that work in Windows and Linux for specific filesystems are welcome too.)
Here's code to get the full path to the executing app:
Variable declarations:
char pBuf[256];
size_t len = sizeof(pBuf);
Windows:
int bytes = GetModuleFileName(NULL, pBuf, len);
return bytes ? bytes : -1;
Linux:
int bytes = MIN(readlink("/proc/self/exe", pBuf, len), len - 1);
if(bytes >= 0)
pBuf[bytes] = '\0';
return bytes;
If you fetch the current directory when your program first starts, then you effectively have the directory your program was started from. Store the value in a variable and refer to it later in your program. This is distinct from the directory that holds the current executable program file. It isn't necessarily the same directory; if someone runs the program from a command prompt, then the program is being run from the command prompt's current working directory even though the program file lives elsewhere.
getcwd is a POSIX function and supported out of the box by all POSIX compliant platforms. You would not have to do anything special (apart from incliding the right headers unistd.h on Unix and direct.h on windows).
Since you are creating a C program it will link with the default c run time library which is linked to by ALL processes in the system (specially crafted exceptions avoided) and it will include this function by default. The CRT is never considered an external library because that provides the basic standard compliant interface to the OS.
On windows getcwd function has been deprecated in favour of _getcwd. I think you could use it in this fashion.
#include <stdio.h> /* defines FILENAME_MAX */
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
char cCurrentPath[FILENAME_MAX];
if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
{
return errno;
}
cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
printf ("The current working directory is %s", cCurrentPath);
This is from the cplusplus forum
On windows:
#include <string>
#include <windows.h>
std::string getexepath()
{
char result[ MAX_PATH ];
return std::string( result, GetModuleFileName( NULL, result, MAX_PATH ) );
}
On Linux:
#include <string>
#include <limits.h>
#include <unistd.h>
std::string getexepath()
{
char result[ PATH_MAX ];
ssize_t count = readlink( "/proc/self/exe", result, PATH_MAX );
return std::string( result, (count > 0) ? count : 0 );
}
On HP-UX:
#include <string>
#include <limits.h>
#define _PSTAT64
#include <sys/pstat.h>
#include <sys/types.h>
#include <unistd.h>
std::string getexepath()
{
char result[ PATH_MAX ];
struct pst_status ps;
if (pstat_getproc( &ps, sizeof( ps ), 0, getpid() ) < 0)
return std::string();
if (pstat_getpathname( result, PATH_MAX, &ps.pst_fid_text ) < 0)
return std::string();
return std::string( result );
}
If you want a standard way without libraries: No. The whole concept of a directory is not included in the standard.
If you agree that some (portable) dependency on a near-standard lib is okay: Use Boost's filesystem library and ask for the initial_path().
IMHO that's as close as you can get, with good karma (Boost is a well-established high quality set of libraries)
I know it is very late at the day to throw an answer at this one but I found that none of the answers were as useful to me as my own solution. A very simple way to get the path from your CWD to your bin folder is like this:
int main(int argc, char* argv[])
{
std::string argv_str(argv[0]);
std::string base = argv_str.substr(0, argv_str.find_last_of("/"));
}
You can now just use this as a base for your relative path. So for example I have this directory structure:
main
----> test
----> src
----> bin
and I want to compile my source code to bin and write a log to test I can just add this line to my code.
std::string pathToWrite = base + "/../test/test.log";
I have tried this approach on Linux using full path, alias etc. and it works just fine.
NOTE:
If you are on windows you should use a '\' as the file separator not '/'. You will have to escape this too for example:
std::string base = argv[0].substr(0, argv[0].find_last_of("\\"));
I think this should work but haven't tested, so comment would be appreciated if it works or a fix if not.
Filesystem TS is now a standard ( and supported by gcc 5.3+ and clang 3.9+ ), so you can use current_path() function from it:
std::string path = std::experimental::filesystem::current_path();
In gcc (5.3+) to include Filesystem you need to use:
#include <experimental/filesystem>
and link your code with -lstdc++fs flag.
If you want to use Filesystem with Microsoft Visual Studio, then read this.
No, there's no standard way. I believe that the C/C++ standards don't even consider the existence of directories (or other file system organizations).
On Windows the GetModuleFileName() will return the full path to the executable file of the current process when the hModule parameter is set to NULL. I can't help with Linux.
Also you should clarify whether you want the current directory or the directory that the program image/executable resides. As it stands your question is a little ambiguous on this point.
On Windows the simplest way is to use the _get_pgmptr function in stdlib.h to get a pointer to a string which represents the absolute path to the executable, including the executables name.
char* path;
_get_pgmptr(&path);
printf(path); // Example output: C:/Projects/Hello/World.exe
Maybe concatenate the current working directory with argv[0]? I'm not sure if that would work in Windows but it works in linux.
For example:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv) {
char the_path[256];
getcwd(the_path, 255);
strcat(the_path, "/");
strcat(the_path, argv[0]);
printf("%s\n", the_path);
return 0;
}
When run, it outputs:
jeremy#jeremy-desktop:~/Desktop$ ./test
/home/jeremy/Desktop/./test
For Win32 GetCurrentDirectory should do the trick.
You can not use argv[0] for that purpose, usually it does contain full path to the executable, but not nessesarily - process could be created with arbitrary value in the field.
Also mind you, the current directory and the directory with the executable are two different things, so getcwd() won't help you either.
On Windows use GetModuleFileName(), on Linux read /dev/proc/procID/.. files.
Just my two cents, but doesn't the following code portably work in C++17?
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main(int argc, char* argv[])
{
std::cout << "Path is " << fs::path(argv[0]).parent_path() << '\n';
}
Seems to work for me on Linux at least.
Based on the previous idea, I now have:
std::filesystem::path prepend_exe_path(const std::string& filename, const std::string& exe_path = "");
With implementation:
fs::path prepend_exe_path(const std::string& filename, const std::string& exe_path)
{
static auto exe_parent_path = fs::path(exe_path).parent_path();
return exe_parent_path / filename;
}
And initialization trick in main():
(void) prepend_exe_path("", argv[0]);
Thanks #Sam Redway for the argv[0] idea. And of course, I understand that C++17 was not around for many years when the OP asked the question.
Just to belatedly pile on here,...
there is no standard solution, because the languages are agnostic of underlying file systems, so as others have said, the concept of a directory based file system is outside the scope of the c / c++ languages.
on top of that, you want not the current working directory, but the directory the program is running in, which must take into account how the program got to where it is - ie was it spawned as a new process via a fork, etc. To get the directory a program is running in, as the solutions have demonstrated, requires that you get that information from the process control structures of the operating system in question, which is the only authority on this question. Thus, by definition, its an OS specific solution.
#include <windows.h>
using namespace std;
// The directory path returned by native GetCurrentDirectory() no end backslash
string getCurrentDirectoryOnWindows()
{
const unsigned long maxDir = 260;
char currentDir[maxDir];
GetCurrentDirectory(maxDir, currentDir);
return string(currentDir);
}
For Windows system at console you can use system(dir) command. And console gives you information about directory and etc. Read about the dir command at cmd. But for Unix-like systems, I don't know... If this command is run, read bash command. ls does not display directory...
Example:
int main()
{
system("dir");
system("pause"); //this wait for Enter-key-press;
return 0;
}
Works with starting from C++11, using experimental filesystem, and C++14-C++17 as well using official filesystem.
application.h:
#pragma once
//
// https://en.cppreference.com/w/User:D41D8CD98F/feature_testing_macros
//
#ifdef __cpp_lib_filesystem
#include <filesystem>
#else
#include <experimental/filesystem>
namespace std {
namespace filesystem = experimental::filesystem;
}
#endif
std::filesystem::path getexepath();
application.cpp:
#include "application.h"
#ifdef _WIN32
#include <windows.h> //GetModuleFileNameW
#else
#include <limits.h>
#include <unistd.h> //readlink
#endif
std::filesystem::path getexepath()
{
#ifdef _WIN32
wchar_t path[MAX_PATH] = { 0 };
GetModuleFileNameW(NULL, path, MAX_PATH);
return path;
#else
char result[PATH_MAX];
ssize_t count = readlink("/proc/self/exe", result, PATH_MAX);
return std::string(result, (count > 0) ? count : 0);
#endif
}
For relative paths, here's what I did. I am aware of the age of this question, I simply want to contribute a simpler answer that works in the majority of cases:
Say you have a path like this:
"path/to/file/folder"
For some reason, Linux-built executables made in eclipse work fine with this. However, windows gets very confused if given a path like this to work with!
As stated above there are several ways to get the current path to the executable, but the easiest way I find works a charm in the majority of cases is appending this to the FRONT of your path:
"./path/to/file/folder"
Just adding "./" should get you sorted! :) Then you can start loading from whatever directory you wish, so long as it is with the executable itself.
EDIT: This won't work if you try to launch the executable from code::blocks if that's the development environment being used, as for some reason, code::blocks doesn't load stuff right... :D
EDIT2: Some new things I have found is that if you specify a static path like this one in your code (Assuming Example.data is something you need to load):
"resources/Example.data"
If you then launch your app from the actual directory (or in Windows, you make a shortcut, and set the working dir to your app dir) then it will work like that.
Keep this in mind when debugging issues related to missing resource/file paths. (Especially in IDEs that set the wrong working dir when launching a build exe from the IDE)
A library solution (although I know this was not asked for).
If you happen to use Qt:
QCoreApplication::applicationDirPath()
Path to the current .exe
#include <Windows.h>
std::wstring getexepathW()
{
wchar_t result[MAX_PATH];
return std::wstring(result, GetModuleFileNameW(NULL, result, MAX_PATH));
}
std::wcout << getexepathW() << std::endl;
// -------- OR --------
std::string getexepathA()
{
char result[MAX_PATH];
return std::string(result, GetModuleFileNameA(NULL, result, MAX_PATH));
}
std::cout << getexepathA() << std::endl;
On POSIX platforms, you can use getcwd().
On Windows, you may use _getcwd(), as use of getcwd() has been deprecated.
For standard libraries, if Boost were standard enough for you, I would have suggested Boost::filesystem, but they seem to have removed path normalization from the proposal. You may have to wait until TR2 becomes readily available for a fully standard solution.
Boost Filesystem's initial_path() behaves like POSIX's getcwd(), and neither does what you want by itself, but appending argv[0] to either of them should do it.
You may note that the result is not always pretty--you may get things like /foo/bar/../../baz/a.out or /foo/bar//baz/a.out, but I believe that it always results in a valid path which names the executable (note that consecutive slashes in a path are collapsed to one).
I previously wrote a solution using envp (the third argument to main() which worked on Linux but didn't seem workable on Windows, so I'm essentially recommending the same solution as someone else did previously, but with the additional explanation of why it is actually correct even if the results are not pretty.
As Minok mentioned, there is no such functionality specified ini C standard or C++ standard. This is considered to be purely OS-specific feature and it is specified in POSIX standard, for example.
Thorsten79 has given good suggestion, it is Boost.Filesystem library. However, it may be inconvenient in case you don't want to have any link-time dependencies in binary form for your program.
A good alternative I would recommend is collection of 100% headers-only STLSoft C++ Libraries Matthew Wilson (author of must-read books about C++). There is portable facade PlatformSTL gives access to system-specific API: WinSTL for Windows and UnixSTL on Unix, so it is portable solution. All the system-specific elements are specified with use of traits and policies, so it is extensible framework. There is filesystem library provided, of course.
The linux bash command
which progname will report a path to program.
Even if one could issue the which command from within your program and direct the output to a tmp file and the program
subsequently reads that tmp file, it will not tell you if that program is the one executing. It only tells you where a program having that name is located.
What is required is to obtain your process id number, and to parse out the path to the name
In my program I want to know if the program was
executed from the user's bin directory or from another in the path
or from /usr/bin. /usr/bin would contain the supported version.
My feeling is that in Linux there is the one solution that is portable.
Use realpath() in stdlib.h like this:
char *working_dir_path = realpath(".", NULL);
The following worked well for me on macOS 10.15.7
brew install boost
main.cpp
#include <iostream>
#include <boost/filesystem.hpp>
int main(int argc, char* argv[]){
boost::filesystem::path p{argv[0]};
p = absolute(p).parent_path();
std::cout << p << std::endl;
return 0;
}
Compiling
g++ -Wall -std=c++11 -l boost_filesystem main.cpp
This question was asked 15 years ago, so the existing answers are now incorrect. If you're using C++17 or greater, the solution is very straightforward today:
#include <filesystem>
std::cout << std::filesystem::current_path();
See cppreference.com for more information.

Is there a way to obtain, by name, a function at current process in Windows?

Is there an equivalent of the following on Windows?
#include <dlfcn.h>
#include <stdio.h>
void main_greeting(void)
{
printf("%s\n", "hello world");
}
void lib_func(void)
{
void (*greeting)(void) = dlsym(RTLD_MAIN_ONLY, "main_greeting");
greeting ? greeting() : printf("%s\n", dlerror());
}
int main(void)
{
lib_func();
return 0;
}
This is a short snippet, the real purpose is to call a function know to exist at a main process (main_greeting), from inside a function (lib_func) from a dynamic loaded library. The main process is not modifiable, and so cannot be rewritten to pass callbacks.
On Windows, executables and DLLs are of the same format (PE nowadays), so an executable can export functions too. GetProcAddress(GetModuleHandle(NULL),TEXT("main_greeting")) will do what you want if the function is exported from the executable. It's done by -Wl,-export-all-symbols for mingw GCC.
I believe there is no equivalent option for Microsoft's linker, so if you use their toolchain, you have to:
export every function with __declspec(dllexport) in source files,
or write a module definition file listing every exported function, passing it to linker,
or generate module definition file automatically.

How to import a DLL function in C?

I was given a DLL that I'm trying to use. The DLL contains the function "send".
this is what I did:
#include <stdio.h>
#include <Windows.h>
int main(int argc, char * argv[])
{
HMODULE libHandle;
if ((libHandle = LoadLibrary(TEXT("SendSMS.dll"))) == NULL)
{
printf("load failed\n");
return 1;
}
if (GetProcAddress(libHandle, "send") == NULL)
{
printf("GetProcAddress failed\n");
printf("%d\n", GetLastError());
return 1;
}
return 0;
}
GetProcAddress returns NULL, and the last error value is 127. (procedure was not found)
What am I doing wrong?
Code look more or less good, so probably something is wrong with *.dll. Please download Dependency Walker application and check what kind of functions are exported by this library.
If you running 64bit environment and "sendsms.dll" is compiled as 32bit loadlibrary does not work. You need to compile your project as 32bit to load dlls.
Probably the DLL doesn't export such a function.
This is usually caused by the "decorations" the compiler adds to the function name. For instance "send" may actually be seen as:
_send
_send#4
?send##ABRACADABRA
To resolve this that's what you should do:
Use the "depends" utility (depends32.exe, shipped with MSVC) to view what your DLL actually exports.
If you're the author of the DLL - you may force the export name to be what you want, by using "def" file (for linker)
I noticed that you're using TEXT on LoadLibrary, but not on GetProcAddress. If GetProcAddress is misinterpreting your string, it could be looking for the wrong function.

Resources