Python doesn't have true character types. Python has strings. The ord()
function works by taking a 1-character string as a function argument, and just throws an error if the string's length is longer.
Under the hood, all ord()
really does is just cast the char
to int
. In C, I could write a naive function like this:
#include <string.h> // for strlen
int c_ord(const char* string)
{
// declare our variables
size_t length;
int ret;
// check the length
// note that Python actually stores the length,
// so this wouldn't be done in real code.
// This is here for example
length = strlen(string);
if (length != 1) {
// invalid length, set a dummy placeholder
ret = -1;
} else {
// just grab our value
ret = string[0];
}
return ret;
}
Notice how all ord()
is doing is getting the exact value, just getting the character, not the string representation. What Cython is doing is the true behavior: treating char like an integer and therefore printing out it's integer value. To treat a character like a string, we could create a array of characters, and let Python know it's a string. The builtin method chr
does this for us all under the hood.
%%cython
import cython
cdef int k = 65
print chr(k)
To write a trivial method in Cython to create a null-terminated C-string, and optionally convert it to a Python string, we can do the following:
Python doesn't have true character types. Python has strings. The ord()
function works by taking a 1-character string as a function argument, and just throws an error if the string's length is longer.
Under the hood, all ord()
really does is just cast the char
to int
. In C, I could write a naive function like this:
#include <string.h> // for strlen
int c_ord(const char* string)
{
// declare our variables
size_t length;
int ret;
// check the length
// note that Python actually stores the length,
// so this wouldn't be done in real code.
// This is here for example
length = strlen(string);
if (length > 1) {
// invalid length, set a dummy placeholder
ret = -1;
} else {
// just grab our value
ret = string[0];
}
return ret;
}
Notice how all ord()
is doing is getting the exact value, just getting the character, not the string representation. What Cython is doing is the true behavior: treating char like an integer and therefore printing out it's integer value. To treat a character like a string, we could create a array of characters, and let Python know it's a string. The builtin method chr
does this for us all under the hood.
%%cython
import cython
cdef int k = 65
print chr(k)
To write a trivial extension to do chr()
and create a null-terminated string (commonly referred to as a C-string), we can write the following.
%%cython
# imports
import cython
from libc.stdlib cimport malloc, free
# create null-terminated string, or a C-string
cdef char* c_string = <char*>malloc(2) # only need 2
c_string[0] = 65 # 'A'
c_string[1] = 0 # '\0', null-termination
# ... do what we want with the C-string
# convert to Python object
cdef bytes str = c_string
# remember to free the allocate memory
free(c_string)
# use Python object
print(str)