J'ai créé une fonction très simple en C++ qui utilise aio_write. Dans les arguments je reçois le chemin pour créer un fichier et sa taille. Pour créer un nouveau fichier, j'utilise int open(const char *pathname, int flags, mode_t mode)
.L'argument const char * ne donne que le premier caractère (sur python3)
Ensuite, je le compile à l'objet partagé en utilisant: g++ -Wall -g -Werror aio_calls.cpp -shared -o aio_calls.so -fPIC -lrt
. Sur Python 2.7.5, tout fonctionne parfaitement, mais sur Python 3.4, je n'ai que le premier caractère du chemin. Toute idée de comment le faire fonctionner, alors il prend tout le chemin?
est le code de fonction ici:
#include <sys/types.h>
#include <aio.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <fstream>
#include "aio_calls.h"
#define DLLEXPORT extern "C"
using namespace std;
DLLEXPORT int awrite(const char *path, int size)
{
// create the file
cout << path << endl;
int file = open(path, O_WRONLY | O_CREAT, 0644);
if (file == -1)
return errno;
// create the buffer
char* buffer = new char[size];
// create the control block structure
aiocb cb;
memset(buffer, 'a', size);
memset(&cb, 0, sizeof(aiocb));
cb.aio_nbytes = size;
cb.aio_fildes = file;
cb.aio_offset = 0;
cb.aio_buf = buffer;
// write!
if (aio_write(&cb) == -1)
{
close(file);
return errno;
}
// wait until the request has finished
while(aio_error(&cb) == EINPROGRESS);
// return final status for aio request
int ret = aio_return(&cb);
if (ret == -1)
return errno;
// now clean up
delete[] buffer;
close(file);
return 0;
}
Comme vous pouvez le voir je l'ai écrit au début Cout de ma fonction. C'est ce qui se passe sur python 2:
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> m=cdll.LoadLibrary('/home/administrator/Documents/aio_calls.so')
>>> m.awrite('aa.txt', 40)
aa.txt
0
Et voici ce qui se passe sur Python 3:
Python 3.4.5 (default, May 29 2017, 15:17:55)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> m=cdll.LoadLibrary('/home/administrator/Documents/aio_calls.so')
>>> m.awrite('aa.txt', 40)
a
0
Je crois que Python 3 a pris le plongeon Unicode, donc les deux premiers 'char' sont le premier caractère, et le second' char' est zéro. – molbdnilo