Title: python2.7.3 struct misaligned when returned
Type: Stage: resolved
Components: ctypes Versions: Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, iroli, mark.dickinson, meador.inge, zach.ware
Priority: normal Keywords:

Created on 2012-10-03 09:30 by iroli, last changed 2020-04-27 02:22 by zach.ware. This issue is now closed.

Messages (3)
msg171876 - (view) Author: Roland Lezuo (iroli) Date: 2012-10-03 09:30
class Int(ctypes.Structure):
           _fields_ = [ ("_i", ctypes.c_uint64),
                                   ("undef", ctypes.c_bool)]

class Int {
                Int(uint64_t i);

                uint64_t _i;
                bool undef;

extern "C" Int foo(const Int& a , const Int& b , const Int& c)
    ret = ...
    return Int(ret);

(gdb) p ret
$3 = 16
(gdb) fin
Run till exit from #0  BVSignExtend (a=..., b=..., c=...) at foo.hpp:130
0x00007ffff784eea4 in ffi_call_unix64 () from /usr/lib/python2.7/lib-dynload/
Value returned is $4 = {_i = 18577824, undef = 16}

My guess: The value 18577824 was not expected to be on the stack.

The following actions "solve" the problem:

1) add another int the class Int (after bool) and adopt _fields_ accordingly.
2) in foo C++ function:
   Int reti = Int(ret);
   return reti;

Because of the "above" solutions I strongly suspect a bug in ctypes.
msg222153 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-03 07:58
@Roland please accept our apologies for the delay in getting back to you.

Can someone else take a look please as I know nothing about ctypes, thanks.
msg367354 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2020-04-27 02:22
As Python 2.7 has now reached end-of-life, I'm closing the issue.  If you can still reproduce this issue in a modern version of Python (3.6-3.8), please reopen it and hopefully we can get someone who understands what's going on to actually look at it :)
Date User Action Args
2020-04-27 02:22:18zach.waresetstatus: open -> closed

nosy: + zach.ware
messages: + msg367354

resolution: out of date
stage: resolved
2019-03-16 00:11:22BreamoreBoysetnosy: - BreamoreBoy
2014-07-03 07:58:21BreamoreBoysetnosy: + belopolsky, amaury.forgeotdarc, BreamoreBoy
messages: + msg222153
2012-10-03 11:53:58mark.dickinsonsetnosy: + mark.dickinson, meador.inge
2012-10-03 09:30:39irolicreate