MCPcopy
hub / github.com/lutzroeder/netron / emitWith

Method emitWith

source/python.js:16750–16795  ·  view source on GitHub ↗
(stmt)

Source from the content-addressed store, hash-verified

16748 }
16749 }
16750 emitWith(stmt) {
16751 const targets = stmt.items;
16752 const entered = [];
16753 for (const target of targets) {
16754 const e = target.context_expr;
16755 const rhs = this.emitExpr(e);
16756 const n = this.graph.insertNode(this.graph.create('prim::Enter', [rhs]));
16757 entered.push(rhs);
16758 if (rhs.type() instanceof torch.ClassType === false) {
16759 throw new python.Error('With item expression must return an object.');
16760 }
16761 const rhsClass = rhs.type();
16762 const enterMethod = rhsClass.findMethod('__enter__');
16763 const exitMethod = rhsClass.findMethod('__exit__');
16764 if (!enterMethod || !exitMethod) {
16765 throw new python.Error('Object returned by with item expression does not define __enter__ and __exit__ methods.');
16766 }
16767 const enterSchema = enterMethod.getSchema();
16768 if (enterSchema.arguments.length !== 1) {
16769 throw new python.Error('__enter__ must have only one argument and one return value.');
16770 }
16771 const exitSchema = exitMethod.getSchema();
16772 if (exitSchema.arguments.length === 4) {
16773 for (let i = 1; i < 4; i++) {
16774 if (exitSchema.arguments[i].type !== torch.AnyType.get()) {
16775 throw new python.Error('Argument of __exit__ must have Any type.');
16776 }
16777 }
16778 } else {
16779 throw new python.Error('__exit__ must have four arguments');
16780 }
16781 n.output(0).setType(enterSchema.returns[0].type);
16782 if (target.optional_vars) {
16783 throw new python.Error('Not implemented.');
16784 // Var i = target.var().get();
16785 // this.environment_stack.setVar(i.range(), i.name().name(), n.output(0));
16786 }
16787 }
16788 this.emitStatements(stmt.body);
16789 while (entered.length > 0) {
16790 const input = entered.pop();
16791 const n = this.graph.create('prim::Exit');
16792 this.graph.insertNode(n);
16793 n.addInput(input);
16794 }
16795 }
16796 emitLoopCommon(range, emit_body, iter_val, targets, cond) {
16797 let max_trip_count_val = null;
16798 if (iter_val === null) {

Callers 1

emitStatementsMethod · 0.80

Calls 13

emitExprMethod · 0.80
insertNodeMethod · 0.80
findMethodMethod · 0.80
getSchemaMethod · 0.80
setTypeMethod · 0.80
emitStatementsMethod · 0.80
addInputMethod · 0.80
createMethod · 0.45
pushMethod · 0.45
typeMethod · 0.45
getMethod · 0.45
outputMethod · 0.45

Tested by

no test coverage detected