News for hackers. Stuff that matters.
Re:Google's Python style guide by yuvadam on Thursday April 19, 2012 @02:55AM (#3861753)
Weird. The recommendation for a shebang line is to use #!/usr/bin/python

That will definitely break stuff. Why not #!/usr/bin/env python?

Re:Google's Python style guide by nikcub on Thursday April 19, 2012 @03:13AM (#3861792)
env is nothing more than running it in a new shell, so slightly slower for each exec (since it starts a shell and then searches through PATH), may pick the wrong interpreter (because PATH is dependent on the user and a lot of other things outside of your control) and since this is all running on Google infrastructure they know what /usr/bin/python.

their goal isn't to write portable code, it is to write fast code that runs on google servers

Re:Google's Python style guide by AncientPC on Thursday April 19, 2012 @04:35AM (#3861961)
I wrote a few small scripts to test this out. Each contained a simple print statement, then looped it 5000 times and timed it.

There is ~2.1% overhead when using env.

This doesn't matter in most cases, but in a Google-sized company with standardized environments it's worth it.

Re:Google's Python style guide by etanol on Thursday April 19, 2012 @04:08AM (#3861906)
Sorry, but I fail to see where the "env" command spawns a shell:

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/env....

Could you please explain what am I missing?

Re:Google's Python style guide by nikcub on Thursday April 19, 2012 @06:09AM (#3862202)
what I mean is that it is just like running 'python' in a shell, not that each instance spawns a new one
Re:Google's Python style guide by zwp on Thursday April 19, 2012 @07:15AM (#3862367)
> like

One significant difference: when running python from a shell there's a fork() and an exec(). env(1) doesn't fork: there are not two processes. (In other words: the shell does not exit when you run a command, env vanishes).

There is obviously still some overhead to using env (and I just learnt from the source that env has argument processing). I tried to replicate AncientPC's test but on my machine both invocations take around 0.015s. (Perhaps their username is an indication as to why they see a (2%!) difference...).

But okay. What I really came here to say is: security. You can make all the efforts in the world to ensure that all programs get called with full pathnames but then one env shebang and you're suddenly open to running whatever's first in the user's $PATH and happens to call itself "python".

EDIT: eg http://portaudit.freebsd.org/d42e5b66-6ea0-11df-9c8d-00e0815...

Re:Google's Python style guide by nikcub on Thursday April 19, 2012 @07:34AM (#3862428)
That last point is important which I forgot to mention, especially in operating systems that have '.' first in PATH.

I notice the difference anecdotally without measuring it, but I don't know if that is a conception bias because I know env should be slower.

The best way would be an autoconf script in your package and an install run that finds and verifies the local framework.

I have to admit that I have never done this though. I have a few Python scripts with decent distribution and just rely on the direct path (and a batch file for win32)

Re:Google's Python style guide by AncientPC on Thursday April 19, 2012 @04:16PM (#3864908)
I've posted my full test scripts and results here: http://williamting.com/2012/04/19/performance-decrease-using...

I've been using this handle since '93 out of inertia. My tests were done on an idle i7-620M, sequentially.

If your invocation takes about 0.015s, that means you're not looping enough for differences to appear. A 2% increase from 0.015s is 0.0153s, invisible due to significant digits cut off.

Re:Google's Python style guide by nodata on Thursday April 19, 2012 @05:17AM (#3862074)
Maybe because it's a hack because to get around the following not working?

#!python

Re:Google's Python style guide by rbanffy on Thursday April 19, 2012 @07:19AM (#3862382)
Bad idea. Which python (or program named "python") will run your script?
Re:Google's Python style guide by nodata on Thursday April 19, 2012 @10:00AM (#3863005)
The first python in your path would run the script.
Re:Google's Python style guide by rbanffy on Thursday April 19, 2012 @10:34AM (#3863174)
So, if your PATH gets changed, say, by something that just lept out of your web browser's sandbox, that's the python that'll run your your scripts, right?
Re:Google's Python style guide by nodata on Thursday April 19, 2012 @10:39AM (#3863205)
Yes, that's how it would work, same as with env.
Re:Google's Python style guide by emmelaich on Thursday April 19, 2012 @06:24AM (#3862243)
Yes; #!python should be made to work.

Not to use the user's PATH, but some system ordained python (via some kernel work).

env is a hack.

Re:Google's Python style guide by rbanffy on Thursday April 19, 2012 @11:08AM (#3863353)
> via some kernel work

Kernels shouldn't be aware of the search path shells use.

Re:Google's Python style guide by emmelaich on Friday April 20, 2012 @03:48AM (#3866907)
I agree, as evidenced by the bit you didn't quote. (?!)
Re:Google's Python style guide by nodata on Thursday April 19, 2012 @11:17AM (#3863388)
He may be referring to binfmt.
Re:Google's Python style guide by emmelaich on Friday April 20, 2012 @03:51AM (#3866915)
I wasn't referring to implementation really, but the principle. The main python could be in /bin or /usr/bin or possibly /usr/local/bin. The poor user or developer shouldn't be required to anticipate where and should not need to resort to hacks such as env.