[issue29753] [Linux] ctypes packs bitfields Incorrectly

2021-07-11 Thread Sam Price

Sam Price  added the comment:

I’ve been looking forward to this fix.  The old implementation did  not
match what my LLVM compiler generated.

On Sun, Jul 11, 2021 at 2:52 PM Charles Machalow 
wrote:

>
> Charles Machalow  added the comment:
>
> Maybe we need to add a __packing__ option to specify how packing should
> work and default to legacy behavior. Then allow users to specify if they
> want similar behavior cross-os.
>
> Otherwise changing this does change packing for existing users and can lead
> to breakages.
>
> What exactly was the hit regression here?
>
> On Sun, Jul 11, 2021, 10:47 AM miss-islington 
> wrote:
>
> >
> > miss-islington  added the
> > comment:
> >
> >
> > New changeset 42da46ed522157b057d73e6b623615ef6427999e by Miss Islington
> > (bot) in branch '3.10':
> > bpo-29753: revert 0d7ad9f (GH-19850) (GH-27085)
> >
> >
> https://github.com/python/cpython/commit/42da46ed522157b057d73e6b623615ef6427999e
> >
> >
> > --
> >
> > ___
> > Python tracker 
> > <https://bugs.python.org/issue29753>
> > ___
> >
>
> ------
>
> ___
> Python tracker 
> <https://bugs.python.org/issue29753>
> ___
>
-- 
Thank you,

Sam Price
(707) 742-3726

--

___
Python tracker 
<https://bugs.python.org/issue29753>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue29753] Ctypes Packing Bitfields Incorrectly - Linux

2020-03-05 Thread Sam Price


Sam Price  added the comment:

I ran into this bug on mac and submitted a duplicate issue of 39858


If you add the check *pfield_size != *pbitofs it fixes this bug.

#ifndef MS_WIN32
} else if (bitsize /* this is a bitfield request */
&& *pfield_size /* we have a bitfield open */
&& *pfield_size != *pbitofs /* Current field has been filled, start new 
one */
&& dict->size * 8 >= *pfield_size
&& (*pbitofs + bitsize) <= dict->size * 8) {
/* expand bit field */
fieldtype = EXPAND_BITFIELD;
#endif


However this would not fix the results where you expand a bitfield around 3 
bytes.
clang produces the 3 bytes if you try and expand around.
#include 
#include 
typedef struct testA{
uint8_t a0 : 7;
uint8_t a1 : 7;
uint8_t a2 : 7;
uint8_t a3 : 3;
}  __attribute__((packed)) testA;

int main(){
printf("%d\n", sizeof(testA)); /* Prints out 3 */
return 0;
}
python prints out a size of 4.

--
nosy: +thesamprice

___
Python tracker 
<https://bugs.python.org/issue29753>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39858] bitfield layout wrong in ctypes

2020-03-05 Thread Sam Price


Sam Price  added the comment:

Does not happen on windows.

Error is in cfield.c

```
#ifndef MS_WIN32
} else if (bitsize /* this is a bitfield request */
&& *pfield_size /* we have a bitfield open */
&& dict->size * 8 >= *pfield_size
&& (*pbitofs + bitsize) <= dict->size * 8) {
/* expand bit field */
fieldtype = EXPAND_BITFIELD;
#endif
```

Specifically dict->size * 8 >= *pfield_size
if *bitofs == *pfield_size then the current field is filled, and expanding the 
bitfield should not be done.

Consider adding this *pfield_size != *bitofs
#ifndef MS_WIN32
} else if (bitsize /* this is a bitfield request */
&& *pfield_size /* we have a bitfield open */
&& *pfield_size != *pbitofs /* Current field has been filled, start new 
one */
&& dict->size * 8 >= *pfield_size
&& (*pbitofs + bitsize) <= dict->size * 8) {
/* expand bit field */
fieldtype = EXPAND_BITFIELD;
#endif

--
versions: +Python 3.7, Python 3.8, Python 3.9

___
Python tracker 
<https://bugs.python.org/issue39858>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue39858] bitfield layout wrong in ctypes

2020-03-04 Thread Sam Price


New submission from Sam Price :

if 8 1 byte fields are included in a ctype field, it allows an extra byte to be 
included in the packing when there is no room left for the next field.

If I put the bitfields in a child structure then I get expected results.
 
In [35]: run ctypeSizeTest.py
Size is  4 Expected 3
0 0x1 a0
0 0x10001 a1
0 0x10002 a2
0 0x10003 a3
0 0x10004 a4
0 0x10005 a5
0 0x10006 a6
0 0x10007 a7
0 0x40008 b0 <- Expected to be at offset 1, not 0.
2 0xc b1 <- Expected to be at offset 1, not 2
Size is  3 Expected 3
0 0x1 a
1 0x4 b0
1 0xc0004 b1

--
components: ctypes
files: ctypeSizeTest.py
messages: 363417
nosy: thesamprice
priority: normal
severity: normal
status: open
title: bitfield layout wrong in ctypes
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6
Added file: https://bugs.python.org/file48954/ctypeSizeTest.py

___
Python tracker 
<https://bugs.python.org/issue39858>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com