Skip to content

Commit 666b3c3

Browse files
committed
compile async_funcdef to coroutine
1 parent 8d2014f commit 666b3c3

9 files changed

Lines changed: 82 additions & 41 deletions

File tree

CPythonLib.includes

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ smtpd.py
153153
smtplib.py
154154
sndhdr.py
155155
socketserver.py
156-
sre*.py
156+
sre_compile.py
157+
sre_constants.py
158+
sre_parse.py
157159
stat.py
158160
string.py
161+
stringprep.py
159162
StringIO.py
160163
symbol.py
161164
sysconfig.py
@@ -171,6 +174,7 @@ traceback.py
171174
tty.py
172175
types.py
173176
tzparse.py
177+
unicodedata.py
174178
urllib/*.py
175179
user.py
176180
uu.py

Lib/_jyio.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
all this in Java, remove this module and revert io.py to its CPython original.
1919
"""
2020

21-
from __future__ import (print_function, unicode_literals)
22-
2321
import _io # Java implementations to replace this module
2422

2523
import os

Lib/encodings/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import codecs
3232
from . import aliases, _java
33-
import collections
3433

3534
_cache = {}
3635
_unknown = '--unknown--'
@@ -111,6 +110,16 @@ def search_function(encoding):
111110
mod = None
112111

113112
if mod is None:
113+
# First, see if we can load the encoding using java.nio.Charset;
114+
# FIXME this could include encodings not known to Python, so we should test that out as well
115+
entry, codecaliases = _java._java_factory(encoding)
116+
if entry is not None:
117+
_cache[encoding] = entry
118+
for alias in codecaliases:
119+
if alias not in _aliases:
120+
_aliases[alias] = modname
121+
return entry
122+
114123
# Cache misses
115124
_cache[encoding] = None
116125
return None
@@ -121,11 +130,11 @@ def search_function(encoding):
121130
if not 4 <= len(entry) <= 7:
122131
raise CodecRegistryError('module "%s" (%s) failed to register'
123132
% (mod.__name__, mod.__file__))
124-
if not isinstance(entry[0], collections.Callable) or not isinstance(entry[1], collections.Callable) or \
125-
(entry[2] is not None and not isinstance(entry[2], collections.Callable)) or \
126-
(entry[3] is not None and not isinstance(entry[3], collections.Callable)) or \
127-
(len(entry) > 4 and entry[4] is not None and not isinstance(entry[4], collections.Callable)) or \
128-
(len(entry) > 5 and entry[5] is not None and not isinstance(entry[5], collections.Callable)):
133+
if not callable(entry[0]) or not callable(entry[1]) or \
134+
(entry[2] is not None and not callable(entry[2])) or \
135+
(entry[3] is not None and not callable(entry[3])) or \
136+
(len(entry) > 4 and entry[4] is not None and not callable(entry[4])) or \
137+
(len(entry) > 5 and entry[5] is not None and not callable(entry[5])):
129138
raise CodecRegistryError('incompatible codecs in module "%s" (%s)'
130139
% (mod.__name__, mod.__file__))
131140
if len(entry)<7 or entry[6] is None:

src/org/python/compiler/CodeCompiler.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,24 @@ public boolean makeClosure(ScopeInfo scope) throws Exception {
441441
return true;
442442
}
443443

444+
@Override
445+
public Object visitAsyncFunctionDef(AsyncFunctionDef node) throws Exception {
446+
String name = node.getInternalName();
447+
java.util.List<expr> decs = node.getInternalDecorator_list();
448+
java.util.List<stmt> body = node.getInternalBody();
449+
return compileFunction(name, decs, body, node);
450+
}
451+
444452
@Override
445453
public Object visitFunctionDef(FunctionDef node) throws Exception {
446-
String name = getName(node.getInternalName());
454+
String name = node.getInternalName();
455+
java.util.List<expr> decs = node.getInternalDecorator_list();
456+
java.util.List<stmt> body = node.getInternalBody();
457+
return compileFunction(name, decs, body, node);
458+
}
447459

460+
private Object compileFunction(String internalName, java.util.List<expr> decs, java.util.List<stmt> body, stmt node) throws Exception {
461+
String name = getName(internalName);
448462
setline(node);
449463

450464
ScopeInfo scope = module.getScopeInfo(node);
@@ -473,10 +487,10 @@ public Object visitFunctionDef(FunctionDef node) throws Exception {
473487

474488
scope.setup_closure();
475489
scope.dump();
476-
module.codeConstant(new Suite(node, node.getInternalBody()), name, true, className, false,
490+
module.codeConstant(new Suite(node, body), name, true, className, false,
477491
false, node.getLine(), scope, cflags).get(code);
478492

479-
Str docStr = getDocStr(node.getInternalBody());
493+
Str docStr = getDocStr(body);
480494
if (docStr != null) {
481495
visit(docStr);
482496
} else {
@@ -494,9 +508,9 @@ public Object visitFunctionDef(FunctionDef node) throws Exception {
494508
PyObject[].class));
495509
}
496510

497-
applyDecorators(node.getInternalDecorator_list());
511+
applyDecorators(decs);
498512

499-
set(new Name(node, node.getInternalName(), expr_contextType.Store));
513+
set(new Name(node, internalName, expr_contextType.Store));
500514
return null;
501515
}
502516

@@ -2223,22 +2237,9 @@ public Object visitList(List node) throws Exception {
22232237
@Override
22242238
public Object visitListComp(ListComp node) throws Exception {
22252239
code.new_(p(PyList.class));
2226-
2227-
code.dup();
2228-
code.invokespecial(p(PyList.class), "<init>", sig(Void.TYPE));
2229-
22302240
code.dup();
2231-
2232-
code.ldc("append");
2233-
2234-
code.invokevirtual(p(PyObject.class), "__getattr__", sig(PyObject.class, String.class));
2235-
String tmp_append = "_[" + node.getLine() + "_" + node.getCharPositionInLine() + "]";
2236-
2237-
java.util.List<expr> args = new ArrayList<expr>();
2238-
args.add(node.getInternalElt());
2239-
2240-
finishComp(node, args, node.getInternalGenerators(), tmp_append);
2241-
2241+
visitInternalGenerators(node, node.getInternalElt(), node.getInternalGenerators());
2242+
code.invokespecial(p(PyList.class), "<init>", sig(Void.TYPE, PyObject.class));
22422243
return null;
22432244
}
22442245

src/org/python/compiler/Module.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ class PyCodeConstant extends Constant implements ClassConstants, Opcodes {
319319
if (scope.generator) {
320320
_moreflags |= org.python.core.CodeFlag.CO_GENERATOR.flag;
321321
}
322+
if (scope.async) {
323+
_moreflags |= CodeFlag.CO_COROUTINE.flag;
324+
}
322325
if (cflags != null) {
323326
if (cflags.isFlagSet(CodeFlag.CO_GENERATOR_ALLOWED)) {
324327
_moreflags |= org.python.core.CodeFlag.CO_GENERATOR_ALLOWED.flag;

src/org/python/compiler/ScopeInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class ScopeInfo extends Object implements ScopeConstants {
2020
public int level;
2121
public int func_level;
2222
public boolean needs_class_closure;
23+
public boolean async;
2324

2425
public void dump() { // for debugging
2526
if (org.python.core.Options.verbose < org.python.core.Py.DEBUG)

src/org/python/compiler/ScopesCompiler.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,40 @@ private void def(String name) {
104104
cur.addBound(name);
105105
}
106106

107+
@Override
108+
public Object visitAsyncFunctionDef(AsyncFunctionDef node) throws Exception {
109+
String name = node.getInternalName();
110+
arguments args = node.getInternalArgs();
111+
List<expr> decs = node.getInternalDecorator_list();
112+
List<stmt> body = node.getInternalBody();
113+
return compileFunction(name, args, decs, body, node);
114+
}
115+
107116
@Override
108117
public Object visitFunctionDef(FunctionDef node) throws Exception {
109-
def(node.getInternalName());
118+
String name = node.getInternalName();
119+
arguments args = node.getInternalArgs();
120+
List<expr> decs = node.getInternalDecorator_list();
121+
List<stmt> body = node.getInternalBody();
122+
return compileFunction(name, args, decs, body, node);
123+
}
124+
125+
private Object compileFunction(String name, arguments args, List<expr> decs, List<stmt> body, stmt node) throws Exception {
126+
def(name);
110127
ArgListCompiler ac = new ArgListCompiler();
111-
ac.visitArgs(node.getInternalArgs());
128+
ac.visitArgs(args);
112129

113130
List<expr> defaults = ac.getDefaults();
114131
for (int i = 0; i < defaults.size(); i++) {
115132
visit(defaults.get(i));
116133
}
117134

118-
List<expr> decs = node.getInternalDecorator_list();
119135
for (int i = decs.size() - 1; i >= 0; i--) {
120136
visit(decs.get(i));
121137
}
122138

123-
beginScope(node.getInternalName(), FUNCSCOPE, node, ac);
139+
beginScope(name, FUNCSCOPE, node, ac);
140+
cur.async = node instanceof AsyncFunctionDef;
124141
int n = ac.names.size();
125142
for (int i = 0; i < n; i++) {
126143
cur.addParam(ac.names.get(i));
@@ -129,7 +146,7 @@ public Object visitFunctionDef(FunctionDef node) throws Exception {
129146
visit(ac.init_code.get(i));
130147
}
131148
cur.markFromParam();
132-
suite(node.getInternalBody());
149+
suite(body);
133150
endScope();
134151
return null;
135152
}
@@ -303,11 +320,7 @@ public Object visitName(Name node) throws Exception {
303320

304321
@Override
305322
public Object visitListComp(ListComp node) throws Exception {
306-
String tmp = "_[" + node.getLine() + "_" + node.getCharPositionInLine()
307-
+ "]";
308-
cur.addBound(tmp);
309-
traverse(node);
310-
return null;
323+
return visitInternalGenerators(node, node.getInternalElt(), node.getInternalGenerators());
311324
}
312325

313326
@Override
@@ -432,7 +445,7 @@ public Object visitWith(With node) throws Exception {
432445
public Object visitExceptHandler(ExceptHandler node) throws Exception {
433446
traverse(node);
434447
if (node.getInternalName() != null) {
435-
cur.addBound(node.getInternalName());
448+
def(node.getInternalName());
436449
}
437450
return null;
438451
}

src/org/python/core/BaseSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected void _update(PyObject [] args) {
3636
* Update the underlying set with the contents of the iterable.
3737
*/
3838
protected static Set<PyObject> _update(Set<PyObject> set, PyObject data) {
39-
if (data == null) {
39+
if (data == null || data == Py.None) {
4040
return set;
4141
}
4242
if (data instanceof BaseSet) {

src/org/python/core/PyBaseCode.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ public PyObject call(ThreadState state, PyObject globals, PyObject[] defaults,
122122
PyFrame frame = new PyFrame(this, globals);
123123
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
124124
return new PyGenerator(frame, closure);
125+
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
126+
return new PyCoroutine(frame, closure);
125127
}
126128
return call(state, frame, closure);
127129
}
@@ -137,6 +139,8 @@ public PyObject call(ThreadState state, PyObject arg1, PyObject globals, PyObjec
137139
frame.f_fastlocals[0] = arg1;
138140
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
139141
return new PyGenerator(frame, closure);
142+
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
143+
return new PyCoroutine(frame, closure);
140144
}
141145
return call(state, frame, closure);
142146
}
@@ -153,6 +157,8 @@ public PyObject call(ThreadState state, PyObject arg1, PyObject arg2, PyObject g
153157
frame.f_fastlocals[1] = arg2;
154158
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
155159
return new PyGenerator(frame, closure);
160+
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
161+
return new PyCoroutine(frame, closure);
156162
}
157163
return call(state, frame, closure);
158164
}
@@ -171,6 +177,8 @@ public PyObject call(ThreadState state, PyObject arg1, PyObject arg2, PyObject a
171177
frame.f_fastlocals[2] = arg3;
172178
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
173179
return new PyGenerator(frame, closure);
180+
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
181+
return new PyCoroutine(frame, closure);
174182
}
175183
return call(state, frame, closure);
176184
}
@@ -189,6 +197,8 @@ public PyObject call(ThreadState state, PyObject arg1, PyObject arg2,
189197
frame.f_fastlocals[3] = arg4;
190198
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
191199
return new PyGenerator(frame, closure);
200+
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
201+
return new PyCoroutine(frame, closure);
192202
}
193203
return call(state, frame, closure);
194204
}
@@ -334,6 +344,8 @@ public PyObject call(ThreadState state, PyObject args[], String kws[], PyObject
334344

335345
if (co_flags.isFlagSet(CodeFlag.CO_GENERATOR)) {
336346
return new PyGenerator(frame, closure);
347+
} else if (co_flags.isFlagSet(CodeFlag.CO_COROUTINE)) {
348+
return new PyCoroutine(frame, closure);
337349
}
338350
return call(state, frame, closure);
339351
}

0 commit comments

Comments
 (0)