|
5 | 5 | ----------
|
6 | 6 | 问题
|
7 | 7 | ----------
|
8 |
| -You need to pass filenames to C library functions, but need to make sure the filename |
9 |
| -has been encoded according to the system’s expected filename encoding. |
| 8 | +你需要向C库函数传递文件名,但是需要确保文件名根据系统期望的文件名编码方式编码过。 |
10 | 9 |
|
11 | 10 | |
|
12 | 11 |
|
13 | 12 | ----------
|
14 | 13 | 解决方案
|
15 | 14 | ----------
|
16 |
| -To write an extension function that receives a filename, use code such as this: |
| 15 | +写一个接受一个文件名为参数的扩展函数,如下这样: |
17 | 16 |
|
18 |
| -static PyObject *py_get_filename(PyObject *self, PyObject *args) { |
19 |
| - PyObject *bytes; |
20 |
| - char *filename; |
21 |
| - Py_ssize_t len; |
22 |
| - if (!PyArg_ParseTuple(args,"O&", PyUnicode_FSConverter, &bytes)) { |
23 |
| - return NULL; |
24 |
| - } |
25 |
| - PyBytes_AsStringAndSize(bytes, &filename, &len); |
26 |
| - /* Use filename */ |
27 |
| - ... |
| 17 | +:: |
28 | 18 |
|
29 |
| - /* Cleanup and return */ |
30 |
| - Py_DECREF(bytes) |
31 |
| - Py_RETURN_NONE; |
32 |
| -} |
| 19 | + static PyObject *py_get_filename(PyObject *self, PyObject *args) { |
| 20 | + PyObject *bytes; |
| 21 | + char *filename; |
| 22 | + Py_ssize_t len; |
| 23 | + if (!PyArg_ParseTuple(args,"O&", PyUnicode_FSConverter, &bytes)) { |
| 24 | + return NULL; |
| 25 | + } |
| 26 | + PyBytes_AsStringAndSize(bytes, &filename, &len); |
| 27 | + /* Use filename */ |
| 28 | + ... |
33 | 29 |
|
34 |
| -If you already have a PyObject * that you want to convert as a filename, use code such |
35 |
| -as the following: |
| 30 | + /* Cleanup and return */ |
| 31 | + Py_DECREF(bytes) |
| 32 | + Py_RETURN_NONE; |
| 33 | + } |
36 | 34 |
|
37 |
| -PyObject *obj; /* Object with the filename */ |
38 |
| -PyObject *bytes; |
39 |
| -char *filename; |
40 |
| -Py_ssize_t len; |
| 35 | +如果你已经有了一个 ``PyObject *`` ,希望将其转换成一个文件名,可以像下面这样做: |
41 | 36 |
|
42 |
| -bytes = PyUnicode_EncodeFSDefault(obj); |
43 |
| -PyBytes_AsStringAndSize(bytes, &filename, &len); |
44 |
| -/* Use filename */ |
45 |
| -... |
| 37 | +:: |
46 | 38 |
|
47 |
| -/* Cleanup */ |
48 |
| -Py_DECREF(bytes); |
| 39 | + PyObject *obj; /* Object with the filename */ |
| 40 | + PyObject *bytes; |
| 41 | + char *filename; |
| 42 | + Py_ssize_t len; |
49 | 43 |
|
50 |
| -If you need to return a filename back to Python, use the following code: |
| 44 | + bytes = PyUnicode_EncodeFSDefault(obj); |
| 45 | + PyBytes_AsStringAndSize(bytes, &filename, &len); |
| 46 | + /* Use filename */ |
| 47 | + ... |
51 | 48 |
|
52 |
| -/* Turn a filename into a Python object */ |
| 49 | + /* Cleanup */ |
| 50 | + Py_DECREF(bytes); |
53 | 51 |
|
54 |
| -char *filename; /* Already set */ |
55 |
| -int filename_len; /* Already set */ |
| 52 | + If you need to return a filename back to Python, use the following code: |
56 | 53 |
|
57 |
| -PyObject *obj = PyUnicode_DecodeFSDefaultAndSize(filename, filename_len); |
| 54 | + /* Turn a filename into a Python object */ |
| 55 | + |
| 56 | + char *filename; /* Already set */ |
| 57 | + int filename_len; /* Already set */ |
| 58 | + |
| 59 | + PyObject *obj = PyUnicode_DecodeFSDefaultAndSize(filename, filename_len); |
58 | 60 |
|
59 | 61 | |
|
60 | 62 |
|
61 | 63 | ----------
|
62 | 64 | 讨论
|
63 | 65 | ----------
|
64 |
| -Dealing with filenames in a portable way is a tricky problem that is best left to Python. |
65 |
| -If you use this recipe in your extension code, filenames will be handled in a manner that |
66 |
| -is consistent with filename handling in the rest of Python. This includes encoding/ |
67 |
| -decoding of bytes, dealing with bad characters, surrogate escapes, and other complica‐ |
68 |
| -tions. |
| 66 | +以可移植方式来处理文件名是一个很棘手的问题,最后交由Python来处理。 |
| 67 | +如果你在扩展代码中使用本节的技术,文件名的处理方式和和Python中是一致的。 |
| 68 | +包括编码/界面字节,处理坏字符,代理转换和其他复杂情况。 |
| 69 | + |
0 commit comments