Re: [Python-Dev] __file__ is not always an absolute path

2010-02-17 Thread Dan Villiom Podlaski Christiansen
On 7 Feb 2010, at 05:27, exar...@twistedmatrix.com wrote:

> Do you know of a case where it's actually slow?  If not, how convincing 
> should this argument really be?  Perhaps we can measure it on a few platforms 
> before passing judgement.

On Mac OS X at least, system calls are notoriously slow. I think it has to do 
with Mach overhead, or something…

$ arch -arch ppc /usr/bin/python2.6 -m timeit -s 'def f(): pass' 'f()'
100 loops, best of 3: 0.476 usec per loop
$ arch -arch ppc /usr/bin/python2.6 -m timeit -s 'from os import getcwd' 
'getcwd()'
1 loops, best of 3: 21.9 usec per loop
$ arch -arch i386 /usr/bin/python2.6 -m timeit -s 'def f(): pass' 'f()'
100 loops, best of 3: 0.234 usec per loop
$ arch -arch i386 /usr/bin/python2.6 -m timeit -s 'from os import getcwd' 
'getcwd()'
10 loops, best of 3: 14.1 usec per loop
$ arch -arch x86_64 /usr/bin/python2.6 -m timeit -s 'def f(): pass' 'f()'
1000 loops, best of 3: 0.182 usec per loop
$ arch -arch x86_64 /usr/bin/python2.6 -m timeit -s 'from os import getcwd' 
'getcwd()'
10 loops, best of 3: 11 usec per loop

For maximum reproducibility, I used the stock Python 2.6.1 included in Mac OS X 
10.6.2. In other words ‘os.getcwd()’ is more than fifty times as slow as a 
regular function call when using Mac OS X.

--

Dan Villiom Podlaski Christiansen
dan...@gmail.com



smime.p7s
Description: S/MIME cryptographic signature
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] os.path.normcase rationale?

2010-10-03 Thread Dan Villiom Podlaski Christiansen

On 3 Oct 2010, at 02:35, Nir Soffer wrote:


On Sat, Sep 25, 2010 at 1:36 AM, James Y Knight  wrote:


An OSX code sketch is available here (summary: call FSPathMakeRef  
to get an
FSRef from a path string, then FSRefMakePath to make it back into a  
path,
which will then have the correct case). And note that it only works  
if the

file actually exists.


http://stackoverflow.com/questions/370186/how-do-i-find-the-correct-case-of-a-filename

It would indeed be useful to have that be available in Python.



There is a much simpler way:


from Carbon import File
File.FSRef('/tmp/foo').as_pathname()

'/private/tmp/Foo'

Note that this is much slower compared to os.path.exists.


This won't work in py3k; the Carbon modules were removed in 3.0. A  
simpler alternative would probably be the F_GETPATH fcntl. An example:


Python 3.1.2 (r312:79147, Jul 11 2010, 18:21:56)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from fcntl import fcntl
>>> from os.path import basename, exists
>>> from os import remove
>>>
>>> F_GETPATH = 50
>>>
>>> if exists('/tmp/å'):
...   remove('/tmp/å')
...
>>> open('/tmp/å', 'w').close()
>>> f = open(b'/tmp/A\xcc\x8a')
>>>
>>> a = f.name
>>> b = fcntl(f, F_GETPATH, b'\0' * 1024).rstrip(b'\0')
>>>
>>> a, b
(b'/tmp/A\xcc\x8a', b'/private/tmp/\xc3\xa5')
>>> a.decode('utf-8'), b.decode('utf-8')
('/tmp/Å', '/private/tmp/å')

--

Dan Villiom Podlaski Christiansen
dan...@gmail.com



smime.p7s
Description: S/MIME cryptographic signature
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com