0% found this document useful (0 votes)
74 views

Python Os.o - NOCTTY Examples

The document provides 4 code examples showing how to use the os.O_NOCTTY flag in Python. The flag is used when opening a file descriptor to indicate that the open file descriptor should not be associated with a controlling terminal even if the process is associated with a controlling terminal. The examples show opening '/dev/tty' with os.O_RDWR | os.O_NOCTTY to disable echoing for password input while restoring the original terminal settings afterwards.

Uploaded by

Shivam Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Python Os.o - NOCTTY Examples

The document provides 4 code examples showing how to use the os.O_NOCTTY flag in Python. The flag is used when opening a file descriptor to indicate that the open file descriptor should not be associated with a controlling terminal even if the process is associated with a controlling terminal. The examples show opening '/dev/tty' with os.O_RDWR | os.O_NOCTTY to disable echoing for password input while restoring the original terminal settings afterwards.

Uploaded by

Shivam Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

05/06/2017 Python os.

O_NOCTTY Examples

Python os.O_NOCTTY Examples

The following are 7 code examples for showing how to use os.O_NOCTTY. They are extracted from open
source Python projects. You can click to vote up the examples you like, or click to vote down the
exmaples you don't like. Your votes will be used in our system to extract more high-quality examples.

You may also check out all available functions/classes of the module os , or try the search function .

Example 1
From project ubuntu-sdk-app-python-template-master, under directory lib/python3.4, in source file getpass.py.

def unix_getpass(prompt='Password: ', stream=None): Score: 11


"""Prompt for a password, with echo turned off.

Args:
prompt: Written on stream to ask for the input. Default: 'Password: '
stream: A writable file object to display the prompt. Defaults to
the tty. If no tty is available defaults to sys.stderr.
Returns:
The seKr3t input.
Raises:
EOFError: If our input tty or stdin was closed.
GetPassWarning: When we were unable to turn echo off on the input.

Always restores terminal settings before returning.


"""
passwd = None
with contextlib.ExitStack() as stack:
try:
# Always try reading and writing directly on the tty first.
fd = os.open('/dev/tty', os.O_RDWR|os. O_NOCTTY )
tty = io.FileIO(fd, 'w+')
stack.enter_context(tty)
input = io.TextIOWrapper(tty)
stack.enter_context(input)
if not stream:
stream = input
except OSError as e:
# If that fails, see if stdin can be controlled.
stack.close()
try:
fd = sys.stdin.fileno()
except (AttributeError, ValueError):
fd = None
passwd = fallback_getpass(prompt, stream)
input = sys.stdin
if not stream:
stream = sys.stderr

if fd is not None:
try:
old = termios.tcgetattr(fd) # a copy to save
new = old[:]
new[3] &= ~termios.ECHO # 3 == 'lflags'
tcsetattr_flags = termios.TCSAFLUSH
if hasattr(termios, 'TCSASOFT'):
http://www.programcreek.com/python/example/3931/os.O_NOCTTY 1/8
05/06/2017 Python os.O_NOCTTY Examples

tcsetattr_flags |= termios.TCSASOFT
try:
termios.tcsetattr(fd, tcsetattr_flags, new)
passwd = _raw_input(prompt, stream, input=input)
finally:
termios.tcsetattr(fd, tcsetattr_flags, old)
stream.flush() # issue7208
except termios.error:
if passwd is not None:
# _raw_input succeeded. The final tcsetattr failed. Reraise
# instead of leaving the terminal in an unknown state.
raise
# We can't control the tty or stdin. Give up and use normal IO.
# fallback_getpass() raises an appropriate warning.
if stream is not input:
# clean up unused file objects before blocking
stack.close()
passwd = fallback_getpass(prompt, stream)

stream.write('\n')
return passwd

Example 2
From project hue, under directory desktop/core/ext-py/Twisted/twisted/test, in source file test_process.py.

def __init__(self): Score: 10


"""
Initialiaze data structures.
"""
self.actions = []
self.closed = []
self.pipeCount = 0
self.O_RDWR = os.O_RDWR
self. O_NOCTTY = os. O_NOCTTY
self.WNOHANG = os.WNOHANG
self.WEXITSTATUS = os.WEXITSTATUS
self.WIFEXITED = os.WIFEXITED
self.seteuidCalls = []
self.setegidCalls = []

Example 3
From project play1, under directory python/Lib, in source file getpass.py.

def unix_getpass(prompt='Password: ', stream=None): Score: 8


"""Prompt for a password, with echo turned off.

Args:
prompt: Written on stream to ask for the input. Default: 'Password: '
stream: A writable file object to display the prompt. Defaults to
the tty. If no tty is available defaults to sys.stderr.
Returns:

http://www.programcreek.com/python/example/3931/os.O_NOCTTY 2/8
05/06/2017 Python os.O_NOCTTY Examples

The seKr3t input.


Raises:
EOFError: If our input tty or stdin was closed.
GetPassWarning: When we were unable to turn echo off on the input.

Always restores terminal settings before returning.


"""
fd = None
tty = None
try:
# Always try reading and writing directly on the tty first.
fd = os.open('/dev/tty', os.O_RDWR|os. O_NOCTTY )
tty = os.fdopen(fd, 'w+', 1)
input = tty
if not stream:
stream = tty
except EnvironmentError, e:
# If that fails, see if stdin can be controlled.
try:
fd = sys.stdin.fileno()
except:
passwd = fallback_getpass(prompt, stream)
input = sys.stdin
if not stream:
stream = sys.stderr

if fd is not None:
passwd = None
try:
old = termios.tcgetattr(fd) # a copy to save
new = old[:]
new[3] &= ~termios.ECHO # 3 == 'lflags'
try:
termios.tcsetattr(fd, termios.TCSADRAIN, new)
passwd = _raw_input(prompt, stream, input=input)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)
except termios.error, e:
if passwd is not None:
# _raw_input succeeded. The final tcsetattr failed. Reraise
# instead of leaving the terminal in an unknown state.
raise
# We can't control the tty or stdin. Give up and use normal IO.
# fallback_getpass() raises an appropriate warning.
del input, tty # clean up unused file objects before blocking
passwd = fallback_getpass(prompt, stream)

stream.write('\n')
return passwd

Example 4
From project mozbuilds, under directory mozilla-build/python/Lib, in source file getpass.py.

def unix_getpass(prompt='Password: ', stream=None): Score: 8


"""Prompt for a password, with echo turned off.

http://www.programcreek.com/python/example/3931/os.O_NOCTTY 3/8
05/06/2017 Python os.O_NOCTTY Examples

Args:
prompt: Written on stream to ask for the input. Default: 'Password: '
stream: A writable file object to display the prompt. Defaults to
the tty. If no tty is available defaults to sys.stderr.
Returns:
The seKr3t input.
Raises:
EOFError: If our input tty or stdin was closed.
GetPassWarning: When we were unable to turn echo off on the input.

Always restores terminal settings before returning.


"""
fd = None
tty = None
try:
# Always try reading and writing directly on the tty first.
fd = os.open('/dev/tty', os.O_RDWR|os. O_NOCTTY )
tty = os.fdopen(fd, 'w+', 1)
input = tty
if not stream:
stream = tty
except EnvironmentError, e:
# If that fails, see if stdin can be controlled.
try:
fd = sys.stdin.fileno()
except (AttributeError, ValueError):
passwd = fallback_getpass(prompt, stream)
input = sys.stdin
if not stream:
stream = sys.stderr

if fd is not None:
passwd = None
try:
old = termios.tcgetattr(fd) # a copy to save
new = old[:]
new[3] &= ~termios.ECHO # 3 == 'lflags'
tcsetattr_flags = termios.TCSAFLUSH
if hasattr(termios, 'TCSASOFT'):
tcsetattr_flags |= termios.TCSASOFT
try:
termios.tcsetattr(fd, tcsetattr_flags, new)
passwd = _raw_input(prompt, stream, input=input)
finally:
termios.tcsetattr(fd, tcsetattr_flags, old)
stream.flush() # issue7208
except termios.error, e:
if passwd is not None:
# _raw_input succeeded. The final tcsetattr failed. Reraise
# instead of leaving the terminal in an unknown state.
raise
# We can't control the tty or stdin. Give up and use normal IO.
# fallback_getpass() raises an appropriate warning.
del input, tty # clean up unused file objects before blocking
passwd = fallback_getpass(prompt, stream)

stream.write('\n')
return passwd

http://www.programcreek.com/python/example/3931/os.O_NOCTTY 4/8
05/06/2017 Python os.O_NOCTTY Examples

Example 5
From project Ironlanguage, under directory External.LCA_RESTRICTED/Languages/CPython/27/Lib, in
source file getpass.py.

def unix_getpass(prompt='Password: ', stream=None): Score: 8


"""Prompt for a password, with echo turned off.

Args:
prompt: Written on stream to ask for the input. Default: 'Password: '
stream: A writable file object to display the prompt. Defaults to
the tty. If no tty is available defaults to sys.stderr.
Returns:
The seKr3t input.
Raises:
EOFError: If our input tty or stdin was closed.
GetPassWarning: When we were unable to turn echo off on the input.

Always restores terminal settings before returning.


"""
fd = None
tty = None
try:
# Always try reading and writing directly on the tty first.
fd = os.open('/dev/tty', os.O_RDWR|os. O_NOCTTY )
tty = os.fdopen(fd, 'w+', 1)
input = tty
if not stream:
stream = tty
except EnvironmentError, e:
# If that fails, see if stdin can be controlled.
try:
fd = sys.stdin.fileno()
except (AttributeError, ValueError):
passwd = fallback_getpass(prompt, stream)
input = sys.stdin
if not stream:
stream = sys.stderr

if fd is not None:
passwd = None
try:
old = termios.tcgetattr(fd) # a copy to save
new = old[:]
new[3] &= ~(termios.ECHO|termios.ISIG) # 3 == 'lflags'
tcsetattr_flags = termios.TCSAFLUSH
if hasattr(termios, 'TCSASOFT'):
tcsetattr_flags |= termios.TCSASOFT
try:
termios.tcsetattr(fd, tcsetattr_flags, new)
passwd = _raw_input(prompt, stream, input=input)
finally:
termios.tcsetattr(fd, tcsetattr_flags, old)
stream.flush() # issue7208
except termios.error, e:
if passwd is not None:
# _raw_input succeeded. The final tcsetattr failed. Reraise
# instead of leaving the terminal in an unknown state.
raise

http://www.programcreek.com/python/example/3931/os.O_NOCTTY 5/8
05/06/2017 Python os.O_NOCTTY Examples

# We can't control the tty or stdin. Give up and use normal IO.
# fallback_getpass() raises an appropriate warning.
del input, tty # clean up unused file objects before blocking
passwd = fallback_getpass(prompt, stream)

stream.write('\n')
return passwd

Example 6
From project ModreX, under directory ModularRex/ScriptEngines/Lib, in source file getpass.py.

def unix_getpass(prompt='Password: ', stream=None): Score: 8


"""Prompt for a password, with echo turned off.

Args:
prompt: Written on stream to ask for the input. Default: 'Password: '
stream: A writable file object to display the prompt. Defaults to
the tty. If no tty is available defaults to sys.stderr.
Returns:
The seKr3t input.
Raises:
EOFError: If our input tty or stdin was closed.
GetPassWarning: When we were unable to turn echo off on the input.

Always restores terminal settings before returning.


"""
fd = None
tty = None
try:
# Always try reading and writing directly on the tty first.
fd = os.open('/dev/tty', os.O_RDWR|os. O_NOCTTY )
tty = os.fdopen(fd, 'w+', 1)
input = tty
if not stream:
stream = tty
except EnvironmentError, e:
# If that fails, see if stdin can be controlled.
try:
fd = sys.stdin.fileno()
except:
passwd = fallback_getpass(prompt, stream)
input = sys.stdin
if not stream:
stream = sys.stderr

if fd is not None:
passwd = None
try:
old = termios.tcgetattr(fd) # a copy to save
new = old[:]
new[3] &= ~termios.ECHO # 3 == 'lflags'
try:
termios.tcsetattr(fd, termios.TCSADRAIN, new)
passwd = _raw_input(prompt, stream, input=input)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old)

http://www.programcreek.com/python/example/3931/os.O_NOCTTY 6/8
05/06/2017 Python os.O_NOCTTY Examples

except termios.error, e:
if passwd is not None:
# _raw_input succeeded. The final tcsetattr failed. Reraise
# instead of leaving the terminal in an unknown state.
raise
# We can't control the tty or stdin. Give up and use normal IO.
# fallback_getpass() raises an appropriate warning.
del input, tty # clean up unused file objects before blocking
passwd = fallback_getpass(prompt, stream)

stream.write('\n')
return passwd

Example 7
From project PythonScript, under directory PythonLib/full, in source file getpass.py.

def unix_getpass(prompt='Password: ', stream=None): Score: 8


"""Prompt for a password, with echo turned off.

Args:
prompt: Written on stream to ask for the input. Default: 'Password: '
stream: A writable file object to display the prompt. Defaults to
the tty. If no tty is available defaults to sys.stderr.
Returns:
The seKr3t input.
Raises:
EOFError: If our input tty or stdin was closed.
GetPassWarning: When we were unable to turn echo off on the input.

Always restores terminal settings before returning.


"""
fd = None
tty = None
try:
# Always try reading and writing directly on the tty first.
fd = os.open('/dev/tty', os.O_RDWR|os. O_NOCTTY )
tty = os.fdopen(fd, 'w+', 1)
input = tty
if not stream:
stream = tty
except EnvironmentError, e:
# If that fails, see if stdin can be controlled.
try:
fd = sys.stdin.fileno()
except (AttributeError, ValueError):
passwd = fallback_getpass(prompt, stream)
input = sys.stdin
if not stream:
stream = sys.stderr

if fd is not None:
passwd = None
try:
old = termios.tcgetattr(fd) # a copy to save
new = old[:]
new[3] &= ~termios.ECHO # 3 == 'lflags'

http://www.programcreek.com/python/example/3931/os.O_NOCTTY 7/8
05/06/2017 Python os.O_NOCTTY Examples

tcsetattr_flags = termios.TCSAFLUSH
if hasattr(termios, 'TCSASOFT'):
tcsetattr_flags |= termios.TCSASOFT
try:
termios.tcsetattr(fd, tcsetattr_flags, new)
passwd = _raw_input(prompt, stream, input=input)
finally:
termios.tcsetattr(fd, tcsetattr_flags, old)
stream.flush() # issue7208
except termios.error, e:
if passwd is not None:
# _raw_input succeeded. The final tcsetattr failed. Reraise
# instead of leaving the terminal in an unknown state.
raise
# We can't control the tty or stdin. Give up and use normal IO.
# fallback_getpass() raises an appropriate warning.
del input, tty # clean up unused file objects before blocking
passwd = fallback_getpass(prompt, stream)

stream.write('\n')
return passwd

http://www.programcreek.com/python/example/3931/os.O_NOCTTY 8/8

You might also like