Skip to content

Commit 0f9bb1b

Browse files
committed
alignment SCIENCE
1 parent 5554b67 commit 0f9bb1b

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Lib/test/support/__init__.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import os
1313
import re
1414
import stat
15+
import struct
1516
import sys
1617
import sysconfig
1718
import textwrap
@@ -899,16 +900,28 @@ def expected_failure_if_gil_disabled():
899900
_header = 'PHBBInP'
900901
else:
901902
_header = 'nP'
902-
_align = '0n'
903903
_vheader = _header + 'n'
904904

905+
def _align(fmt):
906+
"""Pad the struct the way a C compiler does.
907+
908+
C alignment pads the struct total size so that arrays keep the largest
909+
alignment element aligned in an array.
910+
"""
911+
align = '0n'
912+
if 'q' in fmt or 'Q' in fmt:
913+
align = '0q'
914+
if 'd' in fmt:
915+
align = '0d'
916+
return align
917+
905918
def calcobjsize(fmt):
906-
import struct
907-
return struct.calcsize(_header + fmt + _align)
919+
whole_fmt = _header + fmt
920+
return struct.calcsize(whole_fmt + _align(whole_fmt))
908921

909922
def calcvobjsize(fmt):
910-
import struct
911-
return struct.calcsize(_vheader + fmt + _align)
923+
whole_fmt = _vheader + fmt
924+
return struct.calcsize(whole_fmt + _align(whole_fmt))
912925

913926

914927
_TPFLAGS_STATIC_BUILTIN = 1<<1

0 commit comments

Comments
 (0)