Title: multiprocessing.Array("b", 1), multiprocessing.Array("c",1 ) wrong value returned
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: snwokenk, steven.daprano, yselivanov
Priority: normal Keywords:

Created on 2017-11-10 16:33 by snwokenk, last changed 2017-11-11 05:26 by snwokenk.

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()


result is [115]
2nd sequence:
arr = multiprocessing.Array("c", 1)  # character type

arr[0] = 's'.encode()


result is b's'

Wrong values for given types.
msg306061 - (view) Author: Eryk Sun (eryksun) * 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'


Scenario 1 result = "TypeError: one character bytes, bytearray or integer expected"


Scenario 2
import multiprocessing

br = multiprocessing.Array('b', 1)

br[0] = 's'.encode()


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()


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"


arr =  multiprocessing.Array('b', 3) # type byte
arr = b'fun'

Also this website list 'c' as a data type supported:
Date User Action Args
2017-11-11 05:26:20snwokenksetmessages: + msg306075
2017-11-11 05:16:04snwokenksetmessages: + msg306074
2017-11-11 01:04:55eryksunsettype: behavior
components: + Library (Lib), - asyncio
2017-11-10 23:43:54steven.dapranosetnosy: + steven.daprano, - eryksun
type: behavior -> (no value)
messages: + msg306064
components: + asyncio, - Library (Lib)
2017-11-10 23:33:54eryksunsetnosy: + eryksun
messages: + msg306061

components: + Library (Lib), - asyncio
type: behavior
2017-11-10 16:33:26snwokenkcreate