Skip to content

Commit b98aea6

Browse files
committed
Fix some issues raised by pylance
1 parent 7aa3cd8 commit b98aea6

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

pywasm/wasi.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ def fd_read(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.List
832832
elem_len = mems.get_u32(iovs_ptr + 4)
833833
data = [
834834
lambda: os.read(file.host_fd, elem_len),
835-
lambda: file.pipe.read(elem_len),
835+
lambda: file.pipe.read(elem_len) if file.pipe else b'',
836836
][self.help_pipe(args[0])]()
837837
if len(data) == 0:
838838
break
@@ -965,7 +965,7 @@ def fd_write(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Lis
965965
data.extend(elem)
966966
size = [
967967
lambda: os.write(file.host_fd, data),
968-
lambda: file.pipe.write(data),
968+
lambda: file.pipe.write(data) if file.pipe else 0,
969969
][int(self.help_pipe(args[0]))]()
970970
mems.put_u32(args[3], size)
971971
return [self.ERRNO_SUCCESS]
@@ -1072,8 +1072,9 @@ def path_filestat_get(self, m: pywasm.core.Machine, args: typing.List[int]) -> t
10721072
name = mems.get(args[2], args[3]).decode()
10731073
if self.help_escp(file.wasm_name, name):
10741074
return [self.ERRNO_PERM]
1075+
foll = flag & self.LOOKUPFLAGS_SYMLINK_FOLLOW != 0
10751076
try:
1076-
info = os.stat(name, dir_fd=file.host_fd, follow_symlinks=flag & self.LOOKUPFLAGS_SYMLINK_FOLLOW)
1077+
info = os.stat(name, dir_fd=file.host_fd, follow_symlinks=foll)
10771078
except FileNotFoundError:
10781079
return [self.ERRNO_NOENT]
10791080
mems.put_u64(args[4], 1)
@@ -1113,8 +1114,9 @@ def path_filestat_set_times(self, m: pywasm.core.Machine, args: typing.List[int]
11131114
mtim = args[5]
11141115
if args[6] & self.FSTFLAGS_MTIM_NOW:
11151116
mtim = time.time_ns()
1117+
foll = flag & self.LOOKUPFLAGS_SYMLINK_FOLLOW != 0
11161118
try:
1117-
os.utime(name, ns=(atim, mtim), dir_fd=file.host_fd, follow_symlinks=flag & self.LOOKUPFLAGS_SYMLINK_FOLLOW)
1119+
os.utime(name, ns=(atim, mtim), dir_fd=file.host_fd, follow_symlinks=foll)
11181120
except FileNotFoundError:
11191121
return [self.ERRNO_NOENT]
11201122
return [self.ERRNO_SUCCESS]
@@ -1136,7 +1138,7 @@ def path_link(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.Li
11361138
return [self.ERRNO_PERM]
11371139
if self.help_escp(dest.wasm_name, dest_name):
11381140
return [self.ERRNO_PERM]
1139-
foll = args[1] & self.LOOKUPFLAGS_SYMLINK_FOLLOW
1141+
foll = args[1] & self.LOOKUPFLAGS_SYMLINK_FOLLOW != 0
11401142
try:
11411143
os.link(stem_name, dest_name, src_dir_fd=stem.host_fd, dst_dir_fd=dest.host_fd, follow_symlinks=foll)
11421144
except FileExistsError:
@@ -1489,6 +1491,7 @@ def sock_accept(self, m: pywasm.core.Machine, args: typing.List[int]) -> typing.
14891491
wasm_fd = len(self.fd)
14901492
self.fd.append(self.File(
14911493
host_fd=host_fd,
1494+
host_flag=0,
14921495
host_name=file.host_name,
14931496
host_status=self.FILE_STATUS_OPENED,
14941497
pipe=None,

test/spec.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def valj(j: typing.Dict) -> pywasm.ValInst:
4848
data = [valj({'type': 'f64', 'value': e}).into_u64() for e in j['value']]
4949
return pywasm.ValInst.from_v128_u64(data)
5050
case _:
51-
assert 0
51+
raise Exception('unreachable')
5252
case 'funcref':
5353
match j['value']:
5454
case 'null':
@@ -62,7 +62,7 @@ def valj(j: typing.Dict) -> pywasm.ValInst:
6262
case _:
6363
return pywasm.ValInst.from_ref(pywasm.core.ValType.ref_extern(), int(j['value']))
6464
case _:
65-
assert 0
65+
raise Exception('unreachable')
6666

6767

6868
def vale(a: pywasm.ValInst, b: pywasm.ValInst) -> bool:
@@ -96,10 +96,10 @@ def vale(a: pywasm.ValInst, b: pywasm.ValInst) -> bool:
9696
return a.into_i64() == b.into_i64()
9797
if a.type == pywasm.core.ValType.ref_extern():
9898
return a.into_i64() == b.into_i64()
99-
assert 0
99+
raise Exception('unreachable')
100100

101101

102-
def host(runtime: pywasm.core.Runtime) -> typing.Dict[str, typing.Dict[str, pywasm.core.Extern]]:
102+
def host(runtime: pywasm.core.Runtime):
103103
# See https://github.com/WebAssembly/spec/blob/w3c-1.0/interpreter/host/spectest.ml
104104
runtime.imports['spectest'] = {
105105
'global_i32': runtime.allocate_global(
@@ -151,8 +151,8 @@ def host(runtime: pywasm.core.Runtime) -> typing.Dict[str, typing.Dict[str, pywa
151151
for desc in all_test:
152152
suit = json.loads(pathlib.Path(desc).read_text())['commands']
153153
runtime = pywasm.Runtime()
154-
cmodule: pywasm.ModuleInst
155-
lmodule: pywasm.ModuleInst
154+
cmodule = pywasm.ModuleInst()
155+
lmodule = pywasm.ModuleInst()
156156
mmodule = {}
157157
host(runtime)
158158
for elem in suit:
@@ -238,7 +238,7 @@ def host(runtime: pywasm.core.Runtime) -> typing.Dict[str, typing.Dict[str, pywa
238238
case _:
239239
assert 0
240240
case 'assert_uninstantiable':
241-
name = pathlib.Path(desc).parent.joinpath(elem['filename'])
241+
name = str(pathlib.Path(desc).parent.joinpath(elem['filename']))
242242
try:
243243
lmodule = runtime.instance_from_file(name)
244244
except:
@@ -248,7 +248,7 @@ def host(runtime: pywasm.core.Runtime) -> typing.Dict[str, typing.Dict[str, pywa
248248
else:
249249
assert 0
250250
case 'assert_unlinkable':
251-
name = pathlib.Path(desc).parent.joinpath(elem['filename'])
251+
name = str(pathlib.Path(desc).parent.joinpath(elem['filename']))
252252
try:
253253
lmodule = runtime.instance_from_file(name)
254254
except:
@@ -258,7 +258,7 @@ def host(runtime: pywasm.core.Runtime) -> typing.Dict[str, typing.Dict[str, pywa
258258
else:
259259
assert 0
260260
case 'module':
261-
name = pathlib.Path(desc).parent.joinpath(elem['filename'])
261+
name = str(pathlib.Path(desc).parent.joinpath(elem['filename']))
262262
lmodule = runtime.instance_from_file(name)
263263
if 'name' in elem:
264264
mmodule[elem['name']] = lmodule

0 commit comments

Comments
 (0)