with-statement

Javascript Sandbox

为君一笑 提交于 2019-12-04 06:29:33
I want to have developers write some custom apps for a site in Javascript but I want to sandbox it so they can't do anything naughty like redirect the user, set the body display to none etc etc. I have a namespace in Javascript where all the functions they'll ever need exist in there so I was thinking to create a sandbox would be a matter of: with(Namespace) { //App code goes here where they can only access Namespace.* } How is easy is it to get around this and what other methods can be done? Would rather not have to moderate every submitted app. To enforce a sandbox, you would have to inspect

Chain dynamic iterable of context managers to a single with statement

柔情痞子 提交于 2019-12-04 04:04:24
问题 I have a bunch of context managers that I want to chain. On the first glance, contextlib.nested looked like a fitting solution. However, this method is flagged as deprecated in the documentation which also states that the latest with statement allows this directly: Deprecated since version 2.7: The with-statement now supports this functionality directly (without the confusing error prone quirks). However I could not get Python 3.4.3 to use a dynamic iterable of context managers: class Foo():

Is it wise to use with with statements in generators?

扶醉桌前 提交于 2019-12-04 03:56:30
问题 Consider the following Python code: def values(): with somecontext(): yield 1 yield 2 for v in values(): print(v) break In this case, does Python guarantee that the generator is properly closed and, thus, that the context is exited? I realize that it, in practice, is going to be the case in CPython due to reference counting and eager destruction of the generator, but does Python guarantee this behavior? I do notice that it does indeed not work in Jython, so should that be considered a bug or

Is it possible to have an optional with/as statement in python?

↘锁芯ラ 提交于 2019-12-04 00:44:06
Instead of this: FILE = open(f) do_something(FILE) FILE.close() it's better to use this: with open(f) as FILE: do_something(FILE) What if I have something like this? if f is not None: FILE = open(f) else: FILE = None do_something(FILE) if FILE is not None: FILE.close() Where do_something also has an "if FILE is None" clause, and still does something useful in that case - I don't want to just skip do_something if FILE is None. Is there a sensible way of converting this to with/as form? Or am I just trying to solve the optional file problem in a wrong way? If you were to just write it like this:

“with” macro in C

故事扮演 提交于 2019-12-03 15:35:31
I was looking for a macro that will resemble the with-construct. The usage should be something like: with (lock(&x), unlock(&x)) { ... } It might be useful for some other purposes. I came up with this macro: #define __with(_onenter, _onexit, v) \ for (int __with_uniq##v=1; __with_uniq##v > 0; )\ for (_onenter; __with_uniq##v > 0; _onexit) \ while (__with_uniq##v-- > 0) #define _with(x, y, z) __with(x, y, z) #define with(_onenter, _onexit) _with(_onenter, _onexit, __COUNTER__) It has 3 nested loops because it should: Initialize loop counter (C99 only, of course) Possibly initialize variable

Catching an exception while using a Python 'with' statement - Part 2

試著忘記壹切 提交于 2019-12-03 11:58:31
问题 this is a continuation of question Catching an exception while using a Python 'with' statement. I'm quite e newbie and I tested the following code with Python 3.2 on GNU/linux. In the above-mentioned question, something similar to this was proposed to catch an exception from a 'with' statement: try: with open('foo.txt', 'a'): # # some_code # except IOError: print('error') That makes me wonder: what happens if some_code raises an IOError without catching it? It's obviously caught by the outer

Is it good practice to depend on python's with…as statement

拈花ヽ惹草 提交于 2019-12-03 11:52:40
问题 I'm curious if it is considered safe or good practice to depend on python's with...as statement. For example when opening a file: with open("myfile","w") as myFile: #do something So in this example I neglected to explicitly call myFile.close() however I can assume it was called when python exited the with...as statement by calling the objects __exit__() method. Is it good practice/safe to depend upon this or would it be better to always explicitly call file.close() 回答1: This is what context

PostgreSQL WITH RECURSIVE performance

北城余情 提交于 2019-12-03 11:40:43
I have a simple question. Somehow I was unable to find a definitive answer. How much is WITH RECURSIVE syntax optimized in PostgreSQL? By that I mean: is it merely a syntactic sugar for a series of non recursive queries, OR is it more of a single statement that despite its complicated semantics has been optimized as a whole. A follow-up question - just about how much is it possible to optimize this kind of syntax? Of course some concrete data on the matter is most welcome. My experience is that it is indeed very well optimized. Check out the execution plan for your query generated by EXPLAIN

Oracle SQL insert into with With clause

江枫思渺然 提交于 2019-12-03 09:20:59
问题 I'm new to sql, so maybe it is a dumb question, but is there any possibility to use With clause with Insert Into? Or are there any common workarounds? I mean something like this: With helper_table As ( Select * From dummy2 ) Insert Into dummy1 Values (Select t.a From helper_table t Where t.a = 'X' ); Thx! My example is too dummy, so I add some extended code (thx for the answers so far). INSERT INTO dummy values (a,b) //more values WITH helper_table AS ( SELECT * FROM dummy2 ) WITH helper

Can I use python with statement for conditional execution?

故事扮演 提交于 2019-12-03 09:16:43
问题 I'm trying to write code that supports the following semantics: with scope('action_name') as s: do_something() ... do_some_other_stuff() The scope, among other things (setup, cleanup) should decide if this section should run. For instance, if the user configured the program to bypass 'action_name' than, after Scope() is evaluated do_some_other_stuff() will be executed without calling do_something() first. I tried to do it using this context manager: @contextmanager def scope(action): if