python / cpython

The Python programming language
https://www.python.org
Other
63.46k stars 30.39k forks source link

multiprocessing.Array("b", 1), multiprocessing.Array("c",1 ) wrong value returned #76184

Open fcac57da-30e0-4c1b-9b49-a1b67e49f60c opened 7 years ago

fcac57da-30e0-4c1b-9b49-a1b67e49f60c commented 7 years ago
BPO 32003
Nosy @stevendaprano, @1st1, @snwokenk

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-bug', 'library'] title = 'multiprocessing.Array("b", 1), multiprocessing.Array("c",1 ) wrong value returned' updated_at = user = 'https://github.com/snwokenk' ``` bugs.python.org fields: ```python activity = actor = 'snwokenk' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = creator = 'snwokenk' dependencies = [] files = [] hgrepos = [] issue_num = 32003 keywords = [] message_count = 5.0 messages = ['306037', '306061', '306064', '306074', '306075'] nosy_count = 3.0 nosy_names = ['steven.daprano', 'yselivanov', 'snwokenk'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue32003' versions = ['Python 3.6'] ```

fcac57da-30e0-4c1b-9b49-a1b67e49f60c commented 7 years ago

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.

eryksun commented 7 years ago

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.

stevendaprano commented 7 years ago

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?

fcac57da-30e0-4c1b-9b49-a1b67e49f60c commented 7 years ago

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.

fcac57da-30e0-4c1b-9b49-a1b67e49f60c commented 7 years ago

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