|
44 | 44 | def __gr__(self, name, mode='r', closefd=True, opener=None):
|
45 | 45 | pass
|
46 | 46 |
|
47 |
| -def __bootstrap_import__(filename, module_name): |
48 |
| - import sys, _imp, posix |
49 |
| - module = sys.modules[module_name] |
50 |
| - if filename.startswith("%s"): |
51 |
| - full_filename = filename % __graalpython__.core_home |
52 |
| - filename = filename[len("%s"):] |
53 |
| - elif filename.startswith(__graalpython__.stdlib_home): |
54 |
| - full_filename = filename |
55 |
| - filename = filename[len(__graalpython__.stdlib_home):] |
56 |
| - else: |
57 |
| - raise RuntimeError("There was an import during bootstrap outside the core or stdlib home.") |
58 |
| - |
59 |
| - # If we can, avoid opening the file and use our cached code |
60 |
| - if not __graalpython__.has_cached_code(filename): |
61 |
| - content = __graalpython__.read_file(full_filename) |
62 |
| - code = compile(content, filename, "exec") |
63 |
| - else: |
64 |
| - # n.b.: for these builtin modules, there's never a full path and none of |
65 |
| - # them can be packages |
66 |
| - code = __graalpython__.get_cached_code(filename) |
67 |
| - |
68 |
| - exec(code, module.__dict__) |
69 |
| - return module |
70 |
| - |
71 |
| - |
72 |
| -# # TODO(fa): This was formerly located in 'property.py' which has been intrinsified but seemingly other modules rely |
73 |
| -# # on 'descriptor'. We should revisit that. |
| 47 | + |
| 48 | +# TODO(fa): This was formerly located in 'property.py' which has been intrinsified but seemingly other modules rely |
| 49 | +# on 'descriptor'. We should revisit that. |
74 | 50 | def _f(): pass
|
75 | 51 | FunctionType = type(_f)
|
76 | 52 | descriptor = type(FunctionType.__code__)
|
77 | 53 |
|
78 |
| -__bootstrap_import__("%s/functions.py", "builtins") |
| 54 | + |
| 55 | +from sys import _getframe as __getframe__ |
| 56 | + |
| 57 | + |
| 58 | +@__graalpython__.builtin |
| 59 | +def vars(*obj): |
| 60 | + """Return a dictionary of all the attributes currently bound in obj. If |
| 61 | + called with no argument, return the variables bound in local scope.""" |
| 62 | + if len(obj) == 0: |
| 63 | + # TODO inlining _caller_locals().items() in the dict comprehension does not work for now, investigate! |
| 64 | + return __getframe__(0).f_locals |
| 65 | + elif len(obj) != 1: |
| 66 | + raise TypeError("vars() takes at most 1 argument.") |
| 67 | + try: |
| 68 | + return obj[0].__dict__ |
| 69 | + except AttributeError: |
| 70 | + raise TypeError("vars() argument must have __dict__ attribute") |
| 71 | + |
| 72 | + |
| 73 | +@__graalpython__.builtin |
| 74 | +def input(prompt=None): |
| 75 | + import sys |
| 76 | + if(not hasattr(sys, "stdin")): |
| 77 | + raise RuntimeError('input(): lost sys.stdin') |
| 78 | + if(not hasattr(sys, "stdout")): |
| 79 | + raise RuntimeError('input(): lost sys.stdout') |
| 80 | + if(not hasattr(sys, "stderr")): |
| 81 | + raise RuntimeError('input(): lost sys.stderr') |
| 82 | + |
| 83 | + if prompt is not None: |
| 84 | + print(prompt, end="", flush=hasattr(sys.stdout, "flush")) |
| 85 | + |
| 86 | + result = [] |
| 87 | + while True: |
| 88 | + ch = sys.stdin.read(1) |
| 89 | + if ch: |
| 90 | + if ch == "\n": |
| 91 | + break |
| 92 | + result.append(ch) |
| 93 | + else: |
| 94 | + if(len(result) == 0): |
| 95 | + raise EOFError('EOF when reading a line') |
| 96 | + break |
| 97 | + return "".join(result) |
| 98 | + |
| 99 | + |
| 100 | +class filter(object): |
| 101 | + def __init__(self, predicateOrNone, iterable): |
| 102 | + self.predicateOrNone = predicateOrNone |
| 103 | + self.iterable = iter(iterable) |
| 104 | + |
| 105 | + def __iter__(self): |
| 106 | + return self |
| 107 | + |
| 108 | + def __next__(self): |
| 109 | + while True: |
| 110 | + item = next(self.iterable) |
| 111 | + if self.predicateOrNone is None: |
| 112 | + if item: |
| 113 | + return item |
| 114 | + else: |
| 115 | + if self.predicateOrNone(item): |
| 116 | + return item |
0 commit comments