Skip to content

Commit 3913bad

Browse files
gareth-reespitrou
authored andcommitted
bpo-19896: Add typecodes 'q' and 'Q' to multiprocessing.sharedctypes (#2741)
* bpo-19896: Add typcodes 'q' and 'Q' to multiprocessing.sharedctypes. Patch by Antony Lee. * Add NEWS entry. * Slightly tweak NEWS entry Make it clear this is more of a fix rather than a new feature.
1 parent 7c5798e commit 3913bad

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Lib/multiprocessing/sharedctypes.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
#
2424

2525
typecode_to_type = {
26-
'c': ctypes.c_char, 'u': ctypes.c_wchar,
27-
'b': ctypes.c_byte, 'B': ctypes.c_ubyte,
28-
'h': ctypes.c_short, 'H': ctypes.c_ushort,
29-
'i': ctypes.c_int, 'I': ctypes.c_uint,
30-
'l': ctypes.c_long, 'L': ctypes.c_ulong,
31-
'f': ctypes.c_float, 'd': ctypes.c_double
26+
'c': ctypes.c_char, 'u': ctypes.c_wchar,
27+
'b': ctypes.c_byte, 'B': ctypes.c_ubyte,
28+
'h': ctypes.c_short, 'H': ctypes.c_ushort,
29+
'i': ctypes.c_int, 'I': ctypes.c_uint,
30+
'l': ctypes.c_long, 'L': ctypes.c_ulong,
31+
'q': ctypes.c_longlong, 'Q': ctypes.c_ulonglong,
32+
'f': ctypes.c_float, 'd': ctypes.c_double
3233
}
3334

3435
#

Lib/test/_test_multiprocessing.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def wait_for_handle(handle, timeout):
106106
#
107107

108108
try:
109-
from ctypes import Structure, c_int, c_double
109+
from ctypes import Structure, c_int, c_double, c_longlong
110110
except ImportError:
111111
Structure = object
112112
c_int = c_double = None
@@ -1637,6 +1637,7 @@ class _TestValue(BaseTestCase):
16371637
('i', 4343, 24234),
16381638
('d', 3.625, -4.25),
16391639
('h', -232, 234),
1640+
('q', 2 ** 33, 2 ** 34),
16401641
('c', latin('x'), latin('y'))
16411642
]
16421643

@@ -3179,7 +3180,8 @@ def test_free_from_gc(self):
31793180
class _Foo(Structure):
31803181
_fields_ = [
31813182
('x', c_int),
3182-
('y', c_double)
3183+
('y', c_double),
3184+
('z', c_longlong,)
31833185
]
31843186

31853187
class _TestSharedCTypes(BaseTestCase):
@@ -3191,9 +3193,10 @@ def setUp(self):
31913193
self.skipTest("requires multiprocessing.sharedctypes")
31923194

31933195
@classmethod
3194-
def _double(cls, x, y, foo, arr, string):
3196+
def _double(cls, x, y, z, foo, arr, string):
31953197
x.value *= 2
31963198
y.value *= 2
3199+
z.value *= 2
31973200
foo.x *= 2
31983201
foo.y *= 2
31993202
string.value *= 2
@@ -3203,18 +3206,20 @@ def _double(cls, x, y, foo, arr, string):
32033206
def test_sharedctypes(self, lock=False):
32043207
x = Value('i', 7, lock=lock)
32053208
y = Value(c_double, 1.0/3.0, lock=lock)
3209+
z = Value(c_longlong, 2 ** 33, lock=lock)
32063210
foo = Value(_Foo, 3, 2, lock=lock)
32073211
arr = self.Array('d', list(range(10)), lock=lock)
32083212
string = self.Array('c', 20, lock=lock)
32093213
string.value = latin('hello')
32103214

3211-
p = self.Process(target=self._double, args=(x, y, foo, arr, string))
3215+
p = self.Process(target=self._double, args=(x, y, z, foo, arr, string))
32123216
p.daemon = True
32133217
p.start()
32143218
p.join()
32153219

32163220
self.assertEqual(x.value, 14)
32173221
self.assertAlmostEqual(y.value, 2.0/3.0)
3222+
self.assertEqual(z.value, 2 ** 34)
32183223
self.assertEqual(foo.x, 6)
32193224
self.assertAlmostEqual(foo.y, 4.0)
32203225
for i in range(10):
@@ -3225,12 +3230,14 @@ def test_synchronize(self):
32253230
self.test_sharedctypes(lock=True)
32263231

32273232
def test_copy(self):
3228-
foo = _Foo(2, 5.0)
3233+
foo = _Foo(2, 5.0, 2 ** 33)
32293234
bar = copy(foo)
32303235
foo.x = 0
32313236
foo.y = 0
3237+
foo.z = 0
32323238
self.assertEqual(bar.x, 2)
32333239
self.assertAlmostEqual(bar.y, 5.0)
3240+
self.assertEqual(bar.z, 2 ** 33)
32343241

32353242
#
32363243
#
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix multiprocessing.sharedctypes to recognize typecodes ``'q'`` and ``'Q'``.

0 commit comments

Comments
 (0)