@@ -325,6 +325,9 @@ msgid ""
325
325
"0x1d000000\n"
326
326
">>>"
327
327
msgstr ""
328
+ ">>> print(hex(windll.kernel32.GetModuleHandleA(None)))\n"
329
+ "0x1d000000\n"
330
+ ">>>"
328
331
329
332
#: ../../library/ctypes.rst:165
330
333
msgid ""
@@ -347,6 +350,17 @@ msgid ""
347
350
"ValueError: Procedure probably called with too many arguments (4 bytes in excess)\n"
348
351
">>>"
349
352
msgstr ""
353
+ ">>> cdll.kernel32.GetModuleHandleA(None)\n"
354
+ "Traceback (most recent call last):\n"
355
+ " File \" <stdin>\" , line 1, in <module>\n"
356
+ "ValueError: Procedure probably called with not enough arguments (4 bytes missing)\n"
357
+ ">>>\n"
358
+ "\n"
359
+ ">>> windll.msvcrt.printf(b\" spam\" )\n"
360
+ "Traceback (most recent call last):\n"
361
+ " File \" <stdin>\" , line 1, in <module>\n"
362
+ "ValueError: Procedure probably called with too many arguments (4 bytes in excess)\n"
363
+ ">>>"
350
364
351
365
#: ../../library/ctypes.rst:180
352
366
msgid ""
@@ -369,6 +383,11 @@ msgid ""
369
383
"OSError: exception: access violation reading 0x00000020\n"
370
384
">>>"
371
385
msgstr ""
386
+ ">>> windll.kernel32.GetModuleHandleA(32)\n"
387
+ "Traceback (most recent call last):\n"
388
+ " File \" <stdin>\" , line 1, in <module>\n"
389
+ "OSError: exception: access violation reading 0x00000020\n"
390
+ ">>>"
372
391
373
392
#: ../../library/ctypes.rst:193
374
393
msgid ""
@@ -693,6 +712,9 @@ msgid ""
693
712
" point to, *not the contents* of the memory block (of course not, because "
694
713
"Python string objects are immutable)::"
695
714
msgstr ""
715
+ "给指针类型的实例Assigning a new value to instances of the pointer types "
716
+ ":class:`c_char_p`, :class:`c_wchar_p` 和 :class:`c_void_p` 赋新值会改变它们所指向的 "
717
+ "*内存位置*,而不是内存块的 *内容* (当然不是,因为 Python 字符串对象是不可变的)::"
696
718
697
719
#: ../../library/ctypes.rst:297
698
720
msgid ""
@@ -1059,6 +1081,8 @@ msgid ""
1059
1081
">>> print(libc.time(None))\n"
1060
1082
"1150640792"
1061
1083
msgstr ""
1084
+ ">>> print(libc.time(None))\n"
1085
+ "1150640792"
1062
1086
1063
1087
#: ../../library/ctypes.rst:497
1064
1088
msgid ""
@@ -1078,6 +1102,15 @@ msgid ""
1078
1102
"None\n"
1079
1103
">>>"
1080
1104
msgstr ""
1105
+ ">>> strchr = libc.strchr\n"
1106
+ ">>> strchr(b\" abcdef\" , ord(\" d\" ))\n"
1107
+ "8059983\n"
1108
+ ">>> strchr.restype = c_char_p # c_char_p 是一个指向字符串的指针\n"
1109
+ ">>> strchr(b\" abcdef\" , ord(\" d\" ))\n"
1110
+ "b'def'\n"
1111
+ ">>> print(strchr(b\" abcdef\" , ord(\" x\" )))\n"
1112
+ "None\n"
1113
+ ">>>"
1081
1114
1082
1115
#: ../../library/ctypes.rst:510
1083
1116
msgid ""
@@ -1147,6 +1180,22 @@ msgid ""
1147
1180
"OSError: [Errno 126] The specified module could not be found.\n"
1148
1181
">>>"
1149
1182
msgstr ""
1183
+ ">>> GetModuleHandle = windll.kernel32.GetModuleHandleA\n"
1184
+ ">>> def ValidHandle(value):\n"
1185
+ "... if value == 0:\n"
1186
+ "... raise WinError()\n"
1187
+ "... return value\n"
1188
+ "...\n"
1189
+ ">>>\n"
1190
+ ">>> GetModuleHandle.restype = ValidHandle\n"
1191
+ ">>> GetModuleHandle(None)\n"
1192
+ "486539264\n"
1193
+ ">>> GetModuleHandle(\" something silly\" )\n"
1194
+ "Traceback (most recent call last):\n"
1195
+ " File \" <stdin>\" , line 1, in <module>\n"
1196
+ " File \" <stdin>\" , line 3, in ValidHandle\n"
1197
+ "OSError: [Errno 126] The specified module could not be found.\n"
1198
+ ">>>"
1150
1199
1151
1200
#: ../../library/ctypes.rst:552
1152
1201
msgid ""
@@ -1380,6 +1429,12 @@ msgid ""
1380
1429
"specifying a :attr:`~Structure._align_` class attribute in the subclass "
1381
1430
"definition."
1382
1431
msgstr ""
1432
+ "在默认情况下,Structure 和 Union 字段使用与 C 编译器一样的方式进行对齐。 可以通过在子类定义中指定 "
1433
+ ":attr:`~Structure._pack_` 类属性来覆盖此行为。 该属性必须被设为一个正整数来指明字段的最大对齐值。 这也是 ``#pragma"
1434
+ " pack(n)`` 在 MSVC 中所做的事情。 还可以使用与. It is also possible to set a minimum "
1435
+ "alignment for how the subclass itself is packed in the same way ``#pragma "
1436
+ "align(n)`` 在 MSVC 中一样的方式来设置子类本身数据打包的最小对齐值。 这可以通过在子类定义中指定 "
1437
+ ":attr:`~Structure._align_` 类属性来实现。"
1383
1438
1384
1439
#: ../../library/ctypes.rst:677
1385
1440
msgid ""
@@ -2122,6 +2177,13 @@ msgid ""
2122
2177
"py_cmp_func 1 7\n"
2123
2178
">>>"
2124
2179
msgstr ""
2180
+ ">>> qsort(ia, len(ia), sizeof(c_int), cmp_func)\n"
2181
+ "py_cmp_func 5 1\n"
2182
+ "py_cmp_func 33 99\n"
2183
+ "py_cmp_func 7 33\n"
2184
+ "py_cmp_func 5 7\n"
2185
+ "py_cmp_func 1 7\n"
2186
+ ">>>"
2125
2187
2126
2188
#: ../../library/ctypes.rst:1051
2127
2189
msgid "Now we can actually compare the two items and return a useful result::"
@@ -2142,6 +2204,18 @@ msgid ""
2142
2204
"py_cmp_func 5 7\n"
2143
2205
">>>"
2144
2206
msgstr ""
2207
+ ">>> def py_cmp_func(a, b):\n"
2208
+ "... print(\" py_cmp_func\" , a[0], b[0])\n"
2209
+ "... return a[0] - b[0]\n"
2210
+ "...\n"
2211
+ ">>>\n"
2212
+ ">>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func))\n"
2213
+ "py_cmp_func 5 1\n"
2214
+ "py_cmp_func 33 99\n"
2215
+ "py_cmp_func 7 33\n"
2216
+ "py_cmp_func 1 7\n"
2217
+ "py_cmp_func 5 7\n"
2218
+ ">>>"
2145
2219
2146
2220
#: ../../library/ctypes.rst:1066
2147
2221
msgid "As we can easily check, our array is sorted now::"
0 commit comments