Skip to content

Commit 751d2e7

Browse files
authored
Eliminate from ... import * in wasmtime.* (#304)
* Eliminate nearly all "import *" instances in wasmtime package One case is left for now in wasmtime._ffi, in the interest of keeping the PR less huge. * Work around type mismatches of wasmtime._value._unintern() refs #303
1 parent 5360a87 commit 751d2e7

File tree

19 files changed

+1436
-1433
lines changed

19 files changed

+1436
-1433
lines changed

ci/cbindgen.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def __init__(self):
1717
self.ret += '# This is a procedurally generated file, DO NOT EDIT\n'
1818
self.ret += '# instead edit `./ci/cbindgen.py` at the root of the repo\n'
1919
self.ret += '\n'
20-
self.ret += 'from ctypes import *\n'
2120
self.ret += 'import ctypes\n'
2221
self.ret += 'from typing import Any\n'
2322
self.ret += 'from enum import Enum, auto\n'
@@ -39,7 +38,7 @@ def visit_Struct(self, node):
3938
self.ret += "\n"
4039
if not node.decls:
4140
self.forward_declared[node.name] = True
42-
self.ret += "class {}(Structure):\n".format(node.name)
41+
self.ret += "class {}(ctypes.Structure):\n".format(node.name)
4342
self.ret += " pass\n"
4443
return
4544

@@ -55,7 +54,7 @@ def visit_Struct(self, node):
5554
if node.name in self.forward_declared:
5655
self.ret += "{}._fields_ = [\n".format(node.name)
5756
else:
58-
self.ret += "class {}(Structure):\n".format(node.name)
57+
self.ret += "class {}(ctypes.Structure):\n".format(node.name)
5958
self.ret += " _fields_ = [\n"
6059

6160
for decl in node.decls:
@@ -72,7 +71,7 @@ def visit_Union(self, node):
7271
assert(node.decls)
7372

7473
self.ret += "\n"
75-
self.ret += "class {}(Union):\n".format(node.name)
74+
self.ret += "class {}(ctypes.Union):\n".format(node.name)
7675
self.ret += " _fields_ = [\n"
7776
for decl in node.decls:
7877
self.ret += " (\"{}\", {}),\n".format(name(decl.name), type_name(decl.type))
@@ -184,49 +183,49 @@ def type_name(ty, ptr=False, typing=False):
184183
if typing:
185184
return "ctypes._Pointer"
186185
if isinstance(ty, c_ast.IdentifierType) and ty.names[0] == "void":
187-
return "c_void_p"
186+
return "ctypes.c_void_p"
188187
elif not isinstance(ty, c_ast.FuncDecl):
189-
return "POINTER({})".format(type_name(ty, False, typing))
188+
return "ctypes.POINTER({})".format(type_name(ty, False, typing))
190189

191190
if isinstance(ty, c_ast.IdentifierType):
192191
if ty.names == ['unsigned', 'char']:
193-
return "int" if typing else "c_ubyte"
192+
return "int" if typing else "ctypes.c_ubyte"
194193
assert(len(ty.names) == 1)
195194

196195
if ty.names[0] == "void":
197196
return "None"
198197
elif ty.names[0] == "_Bool":
199-
return "bool" if typing else "c_bool"
198+
return "bool" if typing else "ctypes.c_bool"
200199
elif ty.names[0] == "byte_t":
201-
return "c_ubyte"
200+
return "ctypes.c_ubyte"
202201
elif ty.names[0] == "int8_t":
203-
return "c_int8"
202+
return "ctypes.c_int8"
204203
elif ty.names[0] == "uint8_t":
205-
return "c_uint8"
204+
return "ctypes.c_uint8"
206205
elif ty.names[0] == "int16_t":
207-
return "c_int16"
206+
return "ctypes.c_int16"
208207
elif ty.names[0] == "uint16_t":
209-
return "c_uint16"
208+
return "ctypes.c_uint16"
210209
elif ty.names[0] == "int32_t":
211-
return "int" if typing else "c_int32"
210+
return "int" if typing else "ctypes.c_int32"
212211
elif ty.names[0] == "uint32_t":
213-
return "int" if typing else "c_uint32"
212+
return "int" if typing else "ctypes.c_uint32"
214213
elif ty.names[0] == "uint64_t":
215-
return "int" if typing else "c_uint64"
214+
return "int" if typing else "ctypes.c_uint64"
216215
elif ty.names[0] == "int64_t":
217-
return "int" if typing else "c_int64"
216+
return "int" if typing else "ctypes.c_int64"
218217
elif ty.names[0] == "float32_t":
219-
return "float" if typing else "c_float"
218+
return "float" if typing else "ctypes.c_float"
220219
elif ty.names[0] == "float64_t":
221-
return "float" if typing else "c_double"
220+
return "float" if typing else "ctypes.c_double"
222221
elif ty.names[0] == "size_t":
223-
return "int" if typing else "c_size_t"
222+
return "int" if typing else "ctypes.c_size_t"
224223
elif ty.names[0] == "ptrdiff_t":
225-
return "int" if typing else "c_ssize_t"
224+
return "int" if typing else "ctypes.c_ssize_t"
226225
elif ty.names[0] == "char":
227-
return "c_char"
226+
return "ctypes.c_char"
228227
elif ty.names[0] == "int":
229-
return "int" if typing else "c_int"
228+
return "int" if typing else "ctypes.c_int"
230229
# ctypes values can't stand as typedefs, so just use the pointer type here
231230
elif typing and 'callback_t' in ty.names[0]:
232231
return "ctypes._Pointer"
@@ -244,13 +243,13 @@ def type_name(ty, ptr=False, typing=False):
244243
# TODO: apparently errors are thrown if we faithfully represent the
245244
# pointer type here, seems odd?
246245
if isinstance(ty.type, c_ast.PtrDecl):
247-
tys.append("c_size_t")
246+
tys.append("ctypes.c_size_t")
248247
else:
249248
tys.append(type_name(ty.type))
250249
if ty.args and ty.args.params:
251250
for param in ty.args.params:
252251
tys.append(type_name(param.type))
253-
return "CFUNCTYPE({})".format(', '.join(tys))
252+
return "ctypes.CFUNCTYPE({})".format(', '.join(tys))
254253
elif isinstance(ty, c_ast.PtrDecl) or isinstance(ty, c_ast.ArrayDecl):
255254
return type_name(ty.type, True, typing)
256255
else:

0 commit comments

Comments
 (0)