Skip to content

Instantly share code, notes, and snippets.

@eberle1080
Created October 22, 2013 15:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eberle1080/7103201 to your computer and use it in GitHub Desktop.
Save eberle1080/7103201 to your computer and use it in GitHub Desktop.
An example of how to use PyUnicode_AsASCIIString
// Assumption: you have a variable named "pyobj" which is
// a pointer to an instance of PyUnicodeObject.
PyObject* temp = PyUnicode_AsASCIIString(pyobj);
if (NULL == temp) {
// Means the string can't be converted to ASCII, the codec failed
printf("Oh noes\n");
return;
}
// Get the actual bytes as a C string
char* c_str = PyByteArray_AsString(temp);
// Use the string in some manner
printf("The python unicode string is: %s\n", c_str);
// Make sure the temp stuff gets cleaned up at the end
Py_XDECREF(temp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment