Skip to content

Commit 57540ad

Browse files
committed
Allow zipfile to be imported on wasm
1 parent d11cfd5 commit 57540ad

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

Lib/_dummy_os.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
A shim of the os module containing only simple path-related utilities
3+
"""
4+
5+
try:
6+
from os import *
7+
except ImportError:
8+
import abc
9+
10+
# this will throw an appropriate error if os isn't available and a module
11+
# tries to use an os function. If os has become available, then this will
12+
# just work.
13+
def __getattr__(name):
14+
import os
15+
return getattr(os, name)
16+
17+
18+
sep = '/'
19+
20+
21+
def fspath(path):
22+
"""Return the path representation of a path-like object.
23+
24+
If str or bytes is passed in, it is returned unchanged. Otherwise the
25+
os.PathLike interface is used to get the path representation. If the
26+
path representation is not str or bytes, TypeError is raised. If the
27+
provided path is not str, bytes, or os.PathLike, TypeError is raised.
28+
"""
29+
if isinstance(path, (str, bytes)):
30+
return path
31+
32+
# Work from the object's type to match method resolution of other magic
33+
# methods.
34+
path_type = type(path)
35+
try:
36+
path_repr = path_type.__fspath__(path)
37+
except AttributeError:
38+
if hasattr(path_type, '__fspath__'):
39+
raise
40+
else:
41+
raise TypeError("expected str, bytes or os.PathLike object, "
42+
"not " + path_type.__name__)
43+
if isinstance(path_repr, (str, bytes)):
44+
return path_repr
45+
else:
46+
raise TypeError("expected {}.__fspath__() to return str or bytes, "
47+
"not {}".format(path_type.__name__,
48+
type(path_repr).__name__))
49+
50+
class PathLike(abc.ABC):
51+
52+
"""Abstract base class for implementing the file system path protocol."""
53+
54+
@abc.abstractmethod
55+
def __fspath__(self):
56+
"""Return the file system path representation of the object."""
57+
raise NotImplementedError
58+
59+
@classmethod
60+
def __subclasshook__(cls, subclass):
61+
return hasattr(subclass, '__fspath__')

Lib/genericpath.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
Do not use directly. The OS specific modules import the appropriate
44
functions from this module themselves.
55
"""
6-
import os
6+
try:
7+
import os
8+
except ImportError:
9+
import _dummy_os as os
710
import stat
811

912
__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',

Lib/io.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,17 @@
5252
import abc
5353

5454
from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation,
55-
open, open_code, FileIO, BytesIO, StringIO, BufferedReader,
55+
open, open_code, BytesIO, StringIO, BufferedReader,
5656
BufferedWriter, BufferedRWPair, BufferedRandom,
5757
# XXX RUSTPYTHON TODO: IncrementalNewlineDecoder
5858
# IncrementalNewlineDecoder, TextIOWrapper)
5959
TextIOWrapper)
6060

61+
try:
62+
from _io import FileIO
63+
except ImportError:
64+
pass
65+
6166
OpenWrapper = _io.open # for compatibility with _pyio
6267

6368
# Pretend this exception was created here.

Lib/posixpath.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
altsep = None
2323
devnull = '/dev/null'
2424

25-
import os
25+
try:
26+
import os
27+
except ImportError:
28+
import _dummy_os as os
2629
import sys
2730
import stat
2831
import genericpath

Lib/zipfile.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@
88
import importlib.util
99
import io
1010
import itertools
11-
import os
11+
try:
12+
import os
13+
except ImportError:
14+
import _dummy_os as os
1215
import posixpath
13-
import shutil
16+
try:
17+
import shutil
18+
except ImportError:
19+
pass
1420
import stat
1521
import struct
1622
import sys
17-
import threading
23+
try:
24+
import threading
25+
except ImportError:
26+
import _dummy_thread as threading
1827
import time
1928
import contextlib
2029
from collections import OrderedDict

0 commit comments

Comments
 (0)