Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`with` statement #594

Merged
merged 1 commit into from Aug 7, 2017

Conversation

Projects
None yet
3 participants
@abonie
Copy link
Contributor

commented Jul 31, 2017

Implement SETUP_WITH and WITH_CLEANUP opcodes in Batavia's VM

@abonie abonie force-pushed the abonie:with_statement branch from 48ee732 to d4cef80 Jul 31, 2017

this.push_block(block.type, block.handler, block.level-1)
} else {
throw new builtins.BataviaError.$pyclass('Confused WITH_CLEANUP')
}

This comment has been minimized.

throw new builtins.BataviaError.$pyclass('Confused WITH_CLEANUP')
}
var ret = callables.call_function(exit_func, [exc, val, tb]) // XXX this arg list is incorrect for first 2 cases
//TODO: silence error if necessary

This comment has been minimized.

this.push('silenced')
}
}

This comment has been minimized.

this.push_block(block.type, block.handler, block.level - 1)
} else {
throw new builtins.BataviaError.$pyclass('Confused WITH_CLEANUP')
}

This comment has been minimized.

this.push_block(block.type, block.handler, block.level - 1)
} else {
throw new builtins.BataviaError.$pyclass('Confused WITH_CLEANUP')
}

This comment has been minimized.

This comment has been minimized.

Copy link
@freakboy3742

freakboy3742 Aug 1, 2017

Member

Not sure what's going on here - the lint problem is genuine; but it's on line 1747.

This comment has been minimized.

Copy link
@abonie

abonie Aug 1, 2017

Author Contributor

I fixed it. The XXX was only there to remind me to double check that this line works as intended. But now I am wondering if I should leave it as is, since it breaks "stack API" by using this.frame.stack.slice instead of combination of this.pop and this.push. The other way feels clunky in this case.

@abonie abonie force-pushed the abonie:with_statement branch from bb203b5 to 58b0f53 Aug 1, 2017

// this.push(ctxmgr_obj)
// }
VirtualMachine.prototype.byte_SETUP_WITH = function(dest) {
var mgr = this.top()

This comment has been minimized.

Copy link
@abonie

abonie Aug 2, 2017

Author Contributor

CPython would pop context manager from the stack here and push only __exit__ method of the context manager to call it as a function in WITH_CLEANUP. However I couldn't make it work this way, so I am leaving context manager on the stack and in WITH_CLEANUP I am calling mgr.__exit__ (line 1753). I don't see how this would break anything. What do you think @freakboy3742 ?

This comment has been minimized.

Copy link
@freakboy3742

freakboy3742 Aug 2, 2017

Member

Agreed. The state of the stack is an internal detail; and in this particular case, it's a transient detail - so go right ahead.

@abonie abonie force-pushed the abonie:with_statement branch from 57eed18 to c87eac0 Aug 2, 2017

@abonie abonie changed the title [WIP] `with` statement `with` statement Aug 2, 2017

Add `with` statement support to VM
Opcodes SETUP_WITH and WITH_CLEANUP has been implemented to trigger
context manager's `__enter__` and `__exit__` methods as necessary and
suppress exceptions when appropriate.

Note that Python 3.5+ replaces WITH_CLEANUP with WITH_CLEANUP_START and
WITH_CLEANUP_FINISH.

@abonie abonie force-pushed the abonie:with_statement branch from c87eac0 to d0d952d Aug 3, 2017

@freakboy3742
Copy link
Member

left a comment

with approve():
    looks_great()

@freakboy3742 freakboy3742 merged commit 20185c2 into beeware:master Aug 7, 2017

3 checks passed

beekeeper:0/beefore:eslint JavaScript lint checks passed.
Details
beekeeper:0/beefore:pycodestyle Python lint checks passed.
Details
beekeeper:1/smoke-test Smoke build (Python 3.4) passed.
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.