Dies ist ein einfaches Beispiel für etwas, das ich versuche, zum Laufen zu bringen, bevor ich ein wirklich nützliches Problem angehe. Der C-Code:
typedef struct {
uint32_t seconds;
uint32_t nanoseconds;
} geoTime;
int myTest(geoTime *myTime){
printf("Time: %d %d\n", myTime->seconds, myTime->nanoseconds);
myTime->seconds = myTime->nanoseconds;
geoTime T = {314, 159};
printf("MyTime: %d %d retValue: %d %d\n", myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds);
return 314;
}
Der Python-Code:
import ctypes
import time
import math
lib_astro = ctypes.CDLL("libastroC.so")
class geoTime(ctypes.Structure):
_fields_ = [("seconds", ctypes.c_uint),
("nanoseconds", ctypes.c_uint)]
now = time.time()
print "Python Now: ", now
now_geoTime = geoTime()
now_geoTime.seconds = ctypes.c_uint(int((math.floor(now))))
now_geoTime.nanoseconds = ctypes.c_uint(int(math.floor(math.modf(now)[0] * 1000000000)))
print "Python geoTime now:", now_geoTime.seconds, now_geoTime.nanoseconds
lib_astro.myTest.argtypes = [ctypes.POINTER(geoTime)]
lib_astro.myTest.restype = geoTime
print "************* ENTERING C ********************"
test = lib_astro.myTest(ctypes.byref(now_geoTime))
print "************* EXITING C **********************"
print "Modified now_geoTime: ",now_geoTime.seconds, now_geoTime.nanoseconds
print "test: ",test
Ausgabe:
Python Now: 1336401085.43
Python geoTime now: 1336401085 432585000
************* ENTERING C ********************
Time: 1336401085 432585000
MyTime: 432585000 432585000 retValue: 314 159
************* EXITING C **********************
Modified now_geoTime: 432585000 432585000
test: 314
Der obige Code funktioniert genau so, wie ich es erwarten würde, mein Zeiger geht hinein und wird geändert und ich erhalte meine Ganzzahl zurück. Das Problem tritt auf, wenn ich versuche, eine geoTime-Struktur in C zu erstellen und diese an Python zurückzugeben.
Code in C hinzugefügt/geändert:
geoTime patTest(geoTime *myTime){
printf("Time: %d %d\n", myTime->seconds, myTime->nanoseconds);
myTime->seconds = myTime->nanoseconds;
geoTime T = {314, 159};
printf("MyTime: %d %d retValue: %d %d\n", myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds);
return T;
}
Geänderter Python-Code:
lib_astro.patTest.argtypes = [ctypes.POINTER(geoTime)]
lib_astro.patTest.restype = geoTime
print "************* ENTERING C ********************"
test = lib_astro.patTest(ctypes.byref(now_geoTime))
print "************* EXITING C **********************"
print "Modified now_geoTime: ",now_geoTime.seconds, now_geoTime.nanoseconds
print "Type of test: ",test
print "Information in test: ", test.seconds, test.nanoseconds
Wenn ich meinen Code so ändere, wird der C-Code in myTime statt der Informationen aus Python und der Rückgabewert wird in now_geoTime statt in test platziert. Haben Sie eine Idee, was da schief laufen könnte? Es sieht so aus, als ob der Python-Code etwas nicht so macht, wie ich es erwarte, denn der C-Code scheint mit den Werten, die übergeben werden, korrekt zu funktionieren.
Ausgabe des letzten Beispiels:
Python Now: 1336404920.77
Python geoTime now: 1336404920 773674011
************* ENTERING C ********************
Time: 90500 -17037640
MyTime: -17037640 -17037640 retValue: 314 159
************* EXITING C **********************
Modified now_geoTime: 314 159
Type of test: <__main__.geoTime object at 0x82bedf4>
Information in test: 137096800 134497384
Jede Idee würde sehr geschätzt werden, ich habe versucht, dies zu bekommen, um für eine ganze Weile jetzt arbeiten. Vielen Dank im Voraus!