Does Python Bytearray Use Signed Integers In The C Representation?
Solution 1:
Does Python bytearray use signed integers in the C representation?
It uses char
s. Whether those are signed depends on the compiler. You can see this in Include/bytearrayobject.h
. Here's the 2.7 version:
/* Object layout */
typedef struct {
PyObject_VAR_HEAD
/* XXX(nnorwitz): should ob_exports be Py_ssize_t? */
int ob_exports; /* how many buffer exports */
Py_ssize_t ob_alloc; /* How many bytes allocated */
char *ob_bytes;
} PyByteArrayObject;
typedef struct {
PyObject_VAR_HEAD
Py_ssize_t ob_alloc; /* How many bytes allocated in ob_bytes */
char *ob_bytes; /* Physical backing buffer */
char *ob_start; /* Logical start inside ob_bytes */
/* XXX(nnorwitz): should ob_exports be Py_ssize_t? */
int ob_exports; /* How many buffer exports */
} PyByteArrayObject;
If true, what should be considered the "right" result of the in-place sort?
A Python bytearray represents a sequence of integers in the range 0 <= elem < 256, regardless of whether the compiler considers char
s to be signed. You should probably sort it as a sequence of integers in the range 0 <= elem < 256, rather than as a sequence of signed char
s.
To match output of sorted, will it be sufficient on the C side to cast values to unsigned long when dealing with bytearray?
I don't know enough about Cython to say what the correct code change would be.
Post a Comment for "Does Python Bytearray Use Signed Integers In The C Representation?"