Issue 32003: multiprocessing.Array("b", 1), multiprocessing.Array("c",1 ) wrong value returned (original) (raw)

Created on 2017-11-10 16:33 by snwokenk, last changed 2022-04-11 14:58 by admin.

Messages (5)
msg306037 - (view) Author: Samuel Nwokenkwo (snwokenk) Date: 2017-11-10 16:33
1st sequence: arr = multiprocessing.Array("b", 1) # byte type arr[0] = 's'.encode() print(arr[:]) result is [115] ---- 2nd sequence: arr = multiprocessing.Array("c", 1) # character type arr[0] = 's'.encode() print(arr[:]) result is b's' ---------- Wrong values for given types.
msg306061 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-11-10 23:33
The values are correct for the given type codes, which should be the same as the corresponding type codes for the array and struct modules. Except the array module doesn't support the "c" type. However, assigning b's' to an index of a "b" type array should fail with a TypeError, so I don't think you're showing actual code. A "b" array value is a signed integer in the range [-128, 127]. Larger magnitude integers can be assigned, but they alias (wrap around) back to this range.
msg306064 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-11-10 23:43
I don't understand why you think they are the wrong values. What values were you expecting? You have a byte array, you set the value to the byte b's', which is 115, and you get 115. You have a (byte) character array, you set the value to the byte b's', and you get b's'. What were you expecting?
msg306074 - (view) Author: Samuel Nwokenkwo (snwokenk) Date: 2017-11-11 05:16
I completely wrote the wrong code: So I'll copy and paste: -------------------- Scenario 1 import multiprocessing br = multiprocessing.Array('c', 1) br[0] = 's' print(br[:]) Scenario 1 result = "TypeError: one character bytes, bytearray or integer expected" ------------------------- Scenario 2 import multiprocessing br = multiprocessing.Array('b', 1) br[0] = 's'.encode() print(br[:]) Scenario 2 results = "TypeError: an integer is required (got type bytes)" ----------------------------------- I believe my confusion is that I am thinking more of the python data type byte, which takes b'', than C language data type byte (which takes numbers from -127 to 128. This confusion is compounded when I do something like scenario 3: ------------------------------- import multiprocessing br = multiprocessing.Array('c', 1) br[0] = 's'.encode() print(br[:]) scenario 3 results: b's' ------------------ In the first scenario passing 's' i get an error, even though by definition 's' is a c char data type.
msg306075 - (view) Author: Samuel Nwokenkwo (snwokenk) Date: 2017-11-11 05:26
To clarify my expectation was something like this: arr = multiprocessing.Array('c', 3) # type char arr = "Fun" which is similar to c implementation: char arr[3] = "Fun" AND arr = multiprocessing.Array('b', 3) # type byte arr = b'fun' Also this website list 'c' as a data type supported: https://svn.python.org/projects/python/trunk/Lib/multiprocessing/sharedctypes.py
History
Date User Action Args
2022-04-11 14:58:54 admin set github: 76184
2017-11-11 05:26:20 snwokenk set messages: +
2017-11-11 05:16:04 snwokenk set messages: +
2017-11-11 01:04:55 eryksun set type: behaviorcomponents: + Library (Lib), - asyncio
2017-11-10 23:43:54 steven.daprano set nosy: + steven.daprano, - eryksuntype: behavior -> (no value)messages: + components: + asyncio, - Library (Lib)
2017-11-10 23:33:54 eryksun set nosy: + eryksunmessages: + components: + Library (Lib), - asynciotype: behavior
2017-11-10 16:33:26 snwokenk create