codecamp

15.17 传递文件名给C扩展

问题

You need to pass filenames to C library functions, but need to make sure the filenamehas been encoded according to the system’s expected filename encoding.

解决方案

To write an extension function that receives a filename, use code such as this:

static PyObject *py_get_filename(PyObject *self, PyObject *args) {
PyObject *bytes;char *filename;Py_ssize_t len;if (!PyArg_ParseTuple(args,”O&”, PyUnicode_FSConverter, &bytes)) {

return NULL;

}PyBytes_AsStringAndSize(bytes, &filename, &len);/ Use filename [](#)/...

/ Cleanup and return [](#)/Py_DECREF(bytes)Py_RETURN_NONE;

}

If you already have a PyObject * that you want to convert as a filename, use code suchas the following:

PyObject obj; / Object with the filename */PyObject *bytes;char *filename;Py_ssize_t len;

bytes = PyUnicode_EncodeFSDefault(obj);PyBytes_AsStringAndSize(bytes, &filename, &len);/ Use filename [](#)/...

/ Cleanup [](#)/Py_DECREF(bytes);

If you need to return a filename back to Python, use the following code:

/ Turn a filename into a Python object [](#)/

char filename; / Already set _/int filenamelen; / Already set */

PyObject *obj = PyUnicode_DecodeFSDefaultAndSize(filename, filename_len);

讨论

Dealing with filenames in a portable way is a tricky problem that is best left to Python.If you use this recipe in your extension code, filenames will be handled in a manner thatis consistent with filename handling in the rest of Python. This includes encoding/decoding of bytes, dealing with bad characters, surrogate escapes, and other complica‐tions.

15.16 不确定编码格式的C字符串
15.18 传递已打开的文件给C扩展
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }