Does This Scare You?

eryk sun eryksun at gmail.com
Mon Aug 22 11:54:46 EDT 2016


On Mon, Aug 22, 2016 at 1:17 PM, Chris Angelico <rosuav at gmail.com> wrote:
> I tried things like "con.txt" and it simply failed (no such file or
> directory), without printing anything to the console.

Are you using IDLE or some other IDE that uses pythonw.exe instead of
python.exe? If so, first use ctypes to allocate a console:

    import ctypes
    ctypes.WinDLL('kernel32').AllocConsole()

The CON device should work if the process is attached to a console
(i.e. a conhost.exe instance).

You can detect any of the classic DOS devices via isatty(), since
they're all character devices. Below I'm using Windows 10, so the
console uses a kernel device (condrv.sys was added in Windows 8), for
which the name can be queried:

    >>> f = open('con.txt')
    >>> h = msvcrt.get_osfhandle(f.fileno())
    >>> devname =  UNICODE_STRING()
    >>> ctypes.resize(devname, sizeof(devname) + 32767*2)
    >>> ntdll.NtQueryObject(h, 1, byref(devname), sizeof(devname))
    0
    >>> print(devname.Buffer)
    \Device\ConDrv

> I wouldn't accept file names from untrusted sources on *any* system

There are still desktop applications that ask users to name their files.



More information about the Python-list mailing list