Strange behavior using tempfile.mkdtemp() in virtualenvs when launched in Cygwin - python

I am seeing inconsistent behavior when creating temporary directories in Python:
# System Python, Windows Console
C:\Python33>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmpte7fcc'
# Virtualenv Python, Windows Console
Scripts>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmprziefb'
# System Python, Cygwin Console
$ /cygdrive/c/Python33/python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\windows\\temp\\tmprk4fcu'
# Virtualenv Python, Cygwin Console
$ Scripts/python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tempfile
>>> tempfile.mkdtemp()
'c:\\cygwin\\tmp\\tmppeozcl
The first three cases create temporary directories in C:\Windows\Temp (as expected). Why does the fourth case create temporary directories somewhere else?
EDIT: Additional data requested in the comments:
# System Python, Windows Console
C:\Python33>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\Python33']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP']
# Virtualenv Python, Windows Console
Scripts>python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\WINDOWS\\TEMP', 'C:\\WINDOWS\\TEMP']
# System Python, Cygwin Console
$ /cygdrive/c/Python33/python.exe
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\Cygwin\\tmp', 'C:\\WINDOWS\\TEMP', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\Cygwin\\tmp', 'C:\\WINDOWS\\TEMP']
# Virtualenv Python, Cygwin Console
$ Scripts/python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import tempfile
>>> tempfile._candidate_tempdir_list()
['C:\\Cygwin\\tmp', 'C:\\Cygwin\\tmp', 'c:\\temp', 'c:\\tmp', '\\temp', '\\tmp', 'C:\\_PROJECTS\\python-veracity']
>>> [os.environ.get(envname) for envname in ('TMPDIR', 'TEMP', 'TMP')]
[None, 'C:\\Cygwin\\tmp', 'C:\\Cygwin\\tmp']

The tempfile module looks for the environment variables TMPDIR, TEMP and TMP. If one of these variables is set then its value is used as base directory for temporary files and directories. I guess Cygwin sets one of these to C:\cygwin\tmp.

Related

How to check if a PosixPath is a directory or a file

With Path I can run path.is_dir(), how can I do the same thing with PosixPath?
How can I do the same thing with PosixPath?
Exactly the same way. PosixPath extends Path so has all the underlying methods:
Python 3.9.0 (default, Oct 12 2020, 02:44:01)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pathlib, os
>>> pathlib.PosixPath("/").is_dir()
True
>>> os.system("touch /tmp/aFile.txt")
>>> pathlib.PosixPath("/tmp/aFile.txt").is_dir()
False
>>> pathlib.PosixPath("/tmp/aFile.txt").is_file()
True

Python can't handle Windows long paths with parent directory parts

I'm trying to use .. in a Windows long filename within Python and it is producing unexpected results.
Here is the output from the Python REPL:
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.exists(u'\\\\?\\C:\\BitBucket\\crab6\\product\\installer\\windows\\..\\usage.txt')
False
>>> os.path.exists(u'\\\\?\\C:\\BitBucket\\crab6\\product\\installer\\usage.txt')
True
(I realize the path above isn't over 260 characters and therefore the long path syntax is unnecessary, but for the purposes of this post I've made it more manageable. The outcome is the same with very long paths.)
Has anyone else encountered this, and is it a purposeful limitation of long filenames?

Is there easy way to prevent echo from input?

How to prevent echo from input ??
Have tried "getpass()" but no luck.
On Windows IDLE, it doesn't work
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import getpass
>>> p = getpass.getpass(prompt="Input: ")
Warning: Password input may be echoed.
Input: abc <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< It still echos..
On the terminal of the Windows, it works
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import getpass
>>> p = getpass.getpass(prompt="Input: ")
Input:
>>>
Is there a easy way to prevent echo from input ?
I'm assuming your first example there is in IDLE.
From getpass.win_getpass():
if sys.stdin is not sys.__stdin__:
return fallback_getpass(prompt, stream)
IDLE replaces sys.stdin with a different object. getpass detects that somebody has wrapped stdin and fails for security reasons.
See: http://bugs.python.org/issue9290

Python sqlite3 version

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> sqlite3.version
'2.4.1'
Questions:
Why is the version of the sqlite3 module '2.4.1'
Whats the reason behind bundling such an old sqlite with Python? The sqlite releaselog says 2002 Mar 13 (2.4.1).
Python 2.5.1
>>> import sqlite3
>>> sqlite3.version
'2.3.2'
>>> sqlite3.sqlite_version
'3.3.4'
version - pysqlite version
sqlite_version - sqlite version

Is the Python 3.x signal library for Windows incomplete?

I went to write a system script using 3.0 and found the SIGALRM signal and signal.alarm() call missing amongst many others on the Windows deployment. Does anyone know why these are missing? Below is a dir() of the 2.5 vs 3.0 signal packages on windows. I haven't found any 3.0 docs yet mentioning that this was moved
EDIT: signals do work in python25 on windows, they were removed in 3.0. I should reword my request as 'where did they go or get turned into' or is the windows 3.0 release just not complete yet?
python25> python
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> dir(signal)
['NSIG', 'SIGABRT', 'SIGALRM', 'SIGBUS', 'SIGCHLD', 'SIGCLD',
'SIGCONT', 'SIGEMT', 'SIGFPE', 'SIGHUP', 'SIGILL', 'SIGINT ', 'SIGIO',
'SIGKILL', 'SIGPIPE', 'SIGPOLL', 'SIGPROF', 'SIGQUIT', 'SIGRTMAX',
'SIGRTMIN', 'SIGSEGV', 'SIGSTOP', 'SIGSYS ', 'SIGTERM', 'SIGTRAP',
'SIGTSTP', 'SIGTTIN', 'SIGTTOU', 'SIGURG', 'SIGUSR1', 'SIGUSR2',
'SIGVTALRM', 'SIGWINCH', 'SIGX CPU', 'SIGXFSZ', 'SIG_DFL', 'SIG_IGN',
'__doc__', '__name__', 'alarm', 'default_int_handler', 'getsignal',
'pause', 'sig nal']
>>> exit()
python25> cd ../python31
python31> python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> dir(signal)
['NSIG', 'SIGABRT', 'SIGBREAK', 'SIGFPE', 'SIGILL', 'SIGINT',
'SIGSEGV', 'SIGTERM', 'SIG_DFL', 'SIG_IGN', '__doc__', '__ name__',
'__package__', 'default_int_handler', 'getsignal', 'set_wakeup_fd',
'signal']
>>>
It seems you are running your 2.5 in cygwin, which is probably the reason that it shows up there.
Python 2.5.1 (r251:54863, May 18 2007, 16:56:43)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
In my 2.5 for win32 it looks just like in your 3.1:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> dir(signal)
['NSIG', 'SIGABRT', 'SIGBREAK', 'SIGFPE', 'SIGILL', 'SIGINT', 'SIGSEGV', 'SIGTERM', 'SIG_DFL', 'SIG_IGN', '__doc__', '__
name__', 'default_int_handler', 'getsignal', 'signal']
Windows is NOT posix compliant OS so it does not have all signals - my guess is that on 3.0 the missing signals do not show up there any longer.

Categories

Resources