Skip to content

Commit 356c145

Browse files
committed
15.17小节完成
1 parent 8ae252a commit 356c145

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

source/c15/p17_pass_filenames_to_c_extensions.rst

+40-39
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,65 @@
55
----------
66
问题
77
----------
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库函数传递文件名,但是需要确保文件名根据系统期望的文件名编码方式编码过。
109

1110
|
1211
1312
----------
1413
解决方案
1514
----------
16-
To write an extension function that receives a filename, use code such as this:
15+
写一个接受一个文件名为参数的扩展函数,如下这样:
1716

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+
::
2818

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+
...
3329

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+
}
3634

37-
PyObject *obj; /* Object with the filename */
38-
PyObject *bytes;
39-
char *filename;
40-
Py_ssize_t len;
35+
如果你已经有了一个 ``PyObject *`` ,希望将其转换成一个文件名,可以像下面这样做:
4136

42-
bytes = PyUnicode_EncodeFSDefault(obj);
43-
PyBytes_AsStringAndSize(bytes, &filename, &len);
44-
/* Use filename */
45-
...
37+
::
4638

47-
/* Cleanup */
48-
Py_DECREF(bytes);
39+
PyObject *obj; /* Object with the filename */
40+
PyObject *bytes;
41+
char *filename;
42+
Py_ssize_t len;
4943

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+
...
5148

52-
/* Turn a filename into a Python object */
49+
/* Cleanup */
50+
Py_DECREF(bytes);
5351

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:
5653

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);
5860

5961
|
6062
6163
----------
6264
讨论
6365
----------
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

Comments
 (0)