Skip to content

Commit 1769514

Browse files
committed
Remove all uses of "from ctypes import *"
1 parent cb7db9c commit 1769514

File tree

17 files changed

+134
-131
lines changed

17 files changed

+134
-131
lines changed

ci/cbindgen.py

Lines changed: 0 additions & 1 deletion
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'

wasmtime/_bindings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# This is a procedurally generated file, DO NOT EDIT
44
# instead edit `./ci/cbindgen.py` at the root of the repo
55

6-
from ctypes import *
76
import ctypes
87
from typing import Any
98
from enum import Enum, auto

wasmtime/_config.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from . import _ffi as ffi
2-
from ctypes import *
32
import ctypes
43
from wasmtime import WasmtimeError, Managed
54
import typing
@@ -223,7 +222,7 @@ def cache(self, enabled: typing.Union[bool, str]) -> None:
223222
error = ffi.wasmtime_config_cache_config_load(self.ptr(), None)
224223
elif isinstance(enabled, str):
225224
error = ffi.wasmtime_config_cache_config_load(self.ptr(),
226-
c_char_p(enabled.encode('utf-8')))
225+
ctypes.c_char_p(enabled.encode('utf-8')))
227226
else:
228227
raise TypeError("expected string or bool")
229228
if error:

wasmtime/_ffi.py

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from ctypes import *
21
from pathlib import Path
32
import ctypes
43
import sys
@@ -34,49 +33,49 @@
3433

3534
filename = Path(__file__).parent / (sys_platform + '-' + machine) / libname
3635

37-
dll = cdll.LoadLibrary(str(filename))
38-
39-
WASM_I32 = c_uint8(0)
40-
WASM_I64 = c_uint8(1)
41-
WASM_F32 = c_uint8(2)
42-
WASM_F64 = c_uint8(3)
43-
WASM_ANYREF = c_uint8(128)
44-
WASM_FUNCREF = c_uint8(129)
45-
# WASM_V128 = c_uint8(4)
46-
47-
WASMTIME_I32 = c_uint8(0)
48-
WASMTIME_I64 = c_uint8(1)
49-
WASMTIME_F32 = c_uint8(2)
50-
WASMTIME_F64 = c_uint8(3)
51-
WASMTIME_V128 = c_uint8(4)
52-
WASMTIME_FUNCREF = c_uint8(5)
53-
WASMTIME_EXTERNREF = c_uint8(6)
54-
55-
WASM_CONST = c_uint8(0)
56-
WASM_VAR = c_uint8(1)
57-
58-
WASMTIME_EXTERN_FUNC = c_uint8(0)
59-
WASMTIME_EXTERN_GLOBAL = c_uint8(1)
60-
WASMTIME_EXTERN_TABLE = c_uint8(2)
61-
WASMTIME_EXTERN_MEMORY = c_uint8(3)
62-
WASMTIME_EXTERN_SHAREDMEMORY = c_uint8(4)
63-
WASMTIME_EXTERN_INSTANCE = c_uint8(4)
64-
WASMTIME_EXTERN_MODULE = c_uint8(5)
36+
dll = ctypes.cdll.LoadLibrary(str(filename))
37+
38+
WASM_I32 = ctypes.c_uint8(0)
39+
WASM_I64 = ctypes.c_uint8(1)
40+
WASM_F32 = ctypes.c_uint8(2)
41+
WASM_F64 = ctypes.c_uint8(3)
42+
WASM_ANYREF = ctypes.c_uint8(128)
43+
WASM_FUNCREF = ctypes.c_uint8(129)
44+
# WASM_V128 = ctypes.c_uint8(4)
45+
46+
WASMTIME_I32 = ctypes.c_uint8(0)
47+
WASMTIME_I64 = ctypes.c_uint8(1)
48+
WASMTIME_F32 = ctypes.c_uint8(2)
49+
WASMTIME_F64 = ctypes.c_uint8(3)
50+
WASMTIME_V128 = ctypes.c_uint8(4)
51+
WASMTIME_FUNCREF = ctypes.c_uint8(5)
52+
WASMTIME_EXTERNREF = ctypes.c_uint8(6)
53+
54+
WASM_CONST = ctypes.c_uint8(0)
55+
WASM_VAR = ctypes.c_uint8(1)
56+
57+
WASMTIME_EXTERN_FUNC = ctypes.c_uint8(0)
58+
WASMTIME_EXTERN_GLOBAL = ctypes.c_uint8(1)
59+
WASMTIME_EXTERN_TABLE = ctypes.c_uint8(2)
60+
WASMTIME_EXTERN_MEMORY = ctypes.c_uint8(3)
61+
WASMTIME_EXTERN_SHAREDMEMORY = ctypes.c_uint8(4)
62+
WASMTIME_EXTERN_INSTANCE = ctypes.c_uint8(4)
63+
WASMTIME_EXTERN_MODULE = ctypes.c_uint8(5)
6564

6665
WASMTIME_FUNCREF_NULL = (1 << 64) - 1
6766

6867

69-
class wasm_ref_t(Structure):
68+
class wasm_ref_t(ctypes.Structure):
7069
pass
7170

7271

73-
class wasm_val_union(Union):
72+
class wasm_val_union(ctypes.Union):
7473
_fields_ = [
75-
("i32", c_int32),
76-
("i64", c_int64),
77-
("f32", c_float),
78-
("f64", c_double),
79-
("ref", POINTER(wasm_ref_t)),
74+
("i32", ctypes.c_int32),
75+
("i64", ctypes.c_int64),
76+
("f32", ctypes.c_float),
77+
("f64", ctypes.c_double),
78+
("ref", ctypes.POINTER(wasm_ref_t)),
8079
]
8180

8281
i32: int
@@ -86,8 +85,8 @@ class wasm_val_union(Union):
8685
ref: "typing.Union[ctypes._Pointer[wasm_ref_t], None]"
8786

8887

89-
class wasm_val_t(Structure):
90-
_fields_ = [("kind", c_uint8), ("of", wasm_val_union)]
88+
class wasm_val_t(ctypes.Structure):
89+
_fields_ = [("kind", ctypes.c_uint8), ("of", wasm_val_union)]
9190

9291
kind: int
9392
of: wasm_val_union
@@ -97,23 +96,23 @@ class wasm_val_t(Structure):
9796

9897

9998
def to_bytes(vec: wasm_byte_vec_t) -> bytearray:
100-
ty = c_uint8 * vec.size
101-
return bytearray(ty.from_address(addressof(vec.data.contents)))
99+
ty = ctypes.c_uint8 * vec.size
100+
return bytearray(ty.from_address(ctypes.addressof(vec.data.contents)))
102101

103102

104103
def to_str(vec: wasm_byte_vec_t) -> str:
105104
return to_bytes(vec).decode("utf-8")
106105

107106

108107
def to_str_raw(ptr: "ctypes._Pointer", size: int) -> str:
109-
return string_at(ptr, size).decode("utf-8")
108+
return ctypes.string_at(ptr, size).decode("utf-8")
110109

111110

112111
def str_to_name(s: str, trailing_nul: bool = False) -> wasm_byte_vec_t:
113112
if not isinstance(s, str):
114113
raise TypeError("expected a string")
115114
s_bytes = s.encode('utf8')
116-
buf = cast(create_string_buffer(s_bytes), POINTER(c_uint8))
115+
buf = ctypes.cast(ctypes.create_string_buffer(s_bytes), ctypes.POINTER(ctypes.c_uint8))
117116
if trailing_nul:
118117
extra = 1
119118
else:

wasmtime/_func.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def get(self, name: str) -> Optional[AsExtern]:
154154

155155
# First convert to a raw name so we can typecheck our argument
156156
name_bytes = name.encode('utf-8')
157-
name_buf = ffi.create_string_buffer(name_bytes)
157+
name_buf = ctypes.create_string_buffer(name_bytes)
158158

159159
# Next see if we've been invalidated
160160
if self.__ptr is None:

wasmtime/_globals.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import ctypes
2+
13
from . import _ffi as ffi
2-
from ctypes import *
34
from wasmtime import GlobalType, Val, WasmtimeError
45
from typing import Any
56
from ._store import Storelike
@@ -16,9 +17,9 @@ def __init__(self, store: Storelike, ty: GlobalType, val: Any):
1617
error = ffi.wasmtime_global_new(
1718
store._context(),
1819
ty.ptr(),
19-
byref(val),
20-
byref(global_))
21-
ffi.wasmtime_val_unroot(byref(val))
20+
ctypes.byref(val),
21+
ctypes.byref(global_))
22+
ffi.wasmtime_val_unroot(ctypes.byref(val))
2223
if error:
2324
raise WasmtimeError._from_ptr(error)
2425
self._global = global_
@@ -34,7 +35,7 @@ def type(self, store: Storelike) -> GlobalType:
3435
Gets the type of this global as a `GlobalType`
3536
"""
3637

37-
ptr = ffi.wasmtime_global_type(store._context(), byref(self._global))
38+
ptr = ffi.wasmtime_global_type(store._context(), ctypes.byref(self._global))
3839
return GlobalType._from_ptr(ptr, None)
3940

4041
def value(self, store: Storelike) -> Any:
@@ -44,7 +45,7 @@ def value(self, store: Storelike) -> Any:
4445
Returns a native python type
4546
"""
4647
raw = ffi.wasmtime_val_t()
47-
ffi.wasmtime_global_get(store._context(), byref(self._global), byref(raw))
48+
ffi.wasmtime_global_get(store._context(), ctypes.byref(self._global), ctypes.byref(raw))
4849
val = Val._from_raw(store, raw)
4950
if val.value is not None:
5051
return val.value
@@ -56,8 +57,8 @@ def set_value(self, store: Storelike, val: Any) -> None:
5657
Sets the value of this global to a new value
5758
"""
5859
val = Val._convert_to_raw(store, self.type(store).content, val)
59-
error = ffi.wasmtime_global_set(store._context(), byref(self._global), byref(val))
60-
ffi.wasmtime_val_unroot(byref(val))
60+
error = ffi.wasmtime_global_set(store._context(), ctypes.byref(self._global), ctypes.byref(val))
61+
ffi.wasmtime_val_unroot(ctypes.byref(val))
6162
if error:
6263
raise WasmtimeError._from_ptr(error)
6364

wasmtime/_instance.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ctypes
2+
13
from . import _ffi as ffi
24
from ctypes import POINTER, byref
35
from wasmtime import Module, WasmtimeError
@@ -76,8 +78,8 @@ def __init__(self, store: Storelike, instance: Instance):
7678
extern_list = []
7779
i = 0
7880
item = ffi.wasmtime_extern_t()
79-
name_ptr = POINTER(ffi.c_char)()
80-
name_len = ffi.c_size_t(0)
81+
name_ptr = POINTER(ctypes.c_char)()
82+
name_len = ctypes.c_size_t(0)
8183
while ffi.wasmtime_instance_export_nth(
8284
store._context(),
8385
byref(instance._instance),

wasmtime/_linker.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ctypes
2-
from ctypes import *
32
from typing import Any
43
from wasmtime import Instance, Engine, FuncType
54
from wasmtime import Module, WasmtimeError, Func, Managed
@@ -49,17 +48,17 @@ def define(self, store: Storelike, module: str, name: str, item: AsExtern) -> No
4948
"""
5049
raw_item = get_extern_ptr(item)
5150
module_bytes = module.encode('utf-8')
52-
module_buf = create_string_buffer(module_bytes)
51+
module_buf = ctypes.create_string_buffer(module_bytes)
5352
name_bytes = name.encode('utf-8')
54-
name_buf = create_string_buffer(name_bytes)
53+
name_buf = ctypes.create_string_buffer(name_bytes)
5554
error = ffi.wasmtime_linker_define(
5655
self.ptr(),
5756
store._context(),
5857
module_buf,
5958
len(module_bytes),
6059
name_buf,
6160
len(name_bytes),
62-
byref(raw_item))
61+
ctypes.byref(raw_item))
6362
if error:
6463
raise WasmtimeError._from_ptr(error)
6564

@@ -73,9 +72,9 @@ def define_func(self, module: str, name: str, ty: FuncType, func: Callable[...,
7372
the linker can be used to instantiate modules in multiple stores.
7473
"""
7574
module_bytes = module.encode('utf-8')
76-
module_buf = create_string_buffer(module_bytes)
75+
module_buf = ctypes.create_string_buffer(module_bytes)
7776
name_bytes = name.encode('utf-8')
78-
name_buf = create_string_buffer(name_bytes)
77+
name_buf = ctypes.create_string_buffer(name_bytes)
7978
if not isinstance(ty, FuncType):
8079
raise TypeError("expected a FuncType")
8180
idx = FUNCTIONS.allocate((func, ty.results, access_caller))
@@ -106,12 +105,12 @@ def define_instance(self, store: Storelike, name: str, instance: Instance) -> No
106105
if not isinstance(instance, Instance):
107106
raise TypeError("expected an `Instance`")
108107
name_bytes = name.encode('utf8')
109-
name_buf = create_string_buffer(name_bytes)
108+
name_buf = ctypes.create_string_buffer(name_bytes)
110109
error = ffi.wasmtime_linker_define_instance(self.ptr(),
111110
store._context(),
112111
name_buf,
113112
len(name_bytes),
114-
byref(instance._instance))
113+
ctypes.byref(instance._instance))
115114
if error:
116115
raise WasmtimeError._from_ptr(error)
117116

@@ -146,7 +145,7 @@ def define_module(self, store: Storelike, name: str, module: Module) -> None:
146145
if not isinstance(module, Module):
147146
raise TypeError("expected a `Module`")
148147
name_bytes = name.encode('utf-8')
149-
name_buf = create_string_buffer(name_bytes)
148+
name_buf = ctypes.create_string_buffer(name_bytes)
150149
error = ffi.wasmtime_linker_module(self.ptr(), store._context(), name_buf, len(name_bytes), module.ptr())
151150
if error:
152151
raise WasmtimeError._from_ptr(error)
@@ -162,11 +161,11 @@ def instantiate(self, store: Storelike, module: Module) -> Instance:
162161
Raises an error if an import of `module` hasn't been defined in this
163162
linker or if a trap happens while instantiating the instance.
164163
"""
165-
trap = POINTER(ffi.wasm_trap_t)()
164+
trap = ctypes.POINTER(ffi.wasm_trap_t)()
166165
instance = ffi.wasmtime_instance_t()
167166
with enter_wasm(store) as trap:
168167
error = ffi.wasmtime_linker_instantiate(
169-
self.ptr(), store._context(), module.ptr(), byref(instance), trap)
168+
self.ptr(), store._context(), module.ptr(), ctypes.byref(instance), trap)
170169
if error:
171170
raise WasmtimeError._from_ptr(error)
172171
return Instance._from_raw(instance)
@@ -181,10 +180,10 @@ def get_default(self, store: Storelike, name: str) -> Func:
181180
Raises an error if the default export wasn't present.
182181
"""
183182
name_bytes = name.encode('utf-8')
184-
name_buf = create_string_buffer(name_bytes)
183+
name_buf = ctypes.create_string_buffer(name_bytes)
185184
func = ffi.wasmtime_func_t()
186185
error = ffi.wasmtime_linker_get_default(self.ptr(), store._context(),
187-
name_buf, len(name_bytes), byref(func))
186+
name_buf, len(name_bytes), ctypes.byref(func))
188187
if error:
189188
raise WasmtimeError._from_ptr(error)
190189
return Func._from_raw(func)
@@ -197,14 +196,14 @@ def get(self, store: Storelike, module: str, name: str) -> AsExtern:
197196
defined twice with different types.
198197
"""
199198
module_bytes = module.encode('utf-8')
200-
module_buf = create_string_buffer(module_bytes)
199+
module_buf = ctypes.create_string_buffer(module_bytes)
201200
name_bytes = name.encode('utf-8')
202-
name_buf = create_string_buffer(name_bytes)
201+
name_buf = ctypes.create_string_buffer(name_bytes)
203202
item = ffi.wasmtime_extern_t()
204203
ok = ffi.wasmtime_linker_get(self.ptr(), store._context(),
205204
module_buf, len(module_bytes),
206205
name_buf, len(name_bytes),
207-
byref(item))
206+
ctypes.byref(item))
208207
if ok:
209208
return wrap_extern(item)
210209
raise WasmtimeError("item not defined in linker")

0 commit comments

Comments
 (0)