[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-06 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

>>> getpass.getpass('Password: ', open('/dev/stdout', 'w'))
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/serhiy/py/cpython/Lib/getpass.py", line 72, in unix_getpass
passwd = _raw_input(prompt, stream, input=input)
  File "/home/serhiy/py/cpython/Lib/getpass.py", line 143, in _raw_input
stream.write(bytes(prompt, tty_encoding))
TypeError: must be str, not bytes

It seems that you are moving in the wrong direction. No need to test explicitly 
for stdout/stderr/etc, the code should work with arbitrary text stream.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-06 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Fixing IO leak resource would fix this bug but leave another bug opened which I 
try to fix as well.

These statements with Python3 under Linux will always fail because we need to 
open /dev/tty file in binary mode (for whatever reason).

fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
tty = os.fdopen(fd, 'w+', 1)

So I guess the correct fix would be to open /dev/tty in binary mode (and set 
buffering off) and go on from there.

Of course, one can argue whether this bug should be separated from the original 
bug (resource warning). I am not sure either.

Anyway, here is the patch that will work with stream StringIO and stdout.

Thank you for Serhiy for pointing out my mistakes.

--
Added file: 
http://bugs.python.org/file30483/getpass_fix_works_with_stringio_and_stdout_stream.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-06 Thread Vajrasky Kok

Vajrasky Kok added the comment:

So the correct fix should be:
1. Make sure we can open /dev/tty in non-binary mode,
2. We can write string to /dev/tty,
3. Close the leak if we can not open /dev/tty.

Is it?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-06 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

The problem is in io.open, not in getpass. Here is a test script.

$ ./python buffered_open_fd_leak.py 
buffered_open_fd_leak.py:7: ResourceWarning: unclosed file <_io.FileIO name=3 
mode='rb+'>
  tty = io.open(fd, 'w+', 1)

--
versions: +Python 3.4
Added file: http://bugs.python.org/file30482/buffered_open_fd_leak.py

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-06 Thread Serhiy Storchaka

Changes by Serhiy Storchaka :


--
components: +IO
nosy: +hynek, pitrou, stutzbach

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-06 Thread Serhiy Storchaka

Serhiy Storchaka added the comment:

>>> getpass.getpass('Password: ', sys.stdout)
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/serhiy/py/cpython/Lib/getpass.py", line 72, in unix_getpass
passwd = _raw_input(prompt, stream, input=input)
  File "/home/serhiy/py/cpython/Lib/getpass.py", line 146, in _raw_input
stream.write(bytes(prompt, tty_encoding))
TypeError: must be str, not bytes

>>> getpass.getpass('Password: ', io.StringIO())
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/serhiy/py/cpython/Lib/getpass.py", line 72, in unix_getpass
passwd = _raw_input(prompt, stream, input=input)
  File "/home/serhiy/py/cpython/Lib/getpass.py", line 146, in _raw_input
stream.write(bytes(prompt, tty_encoding))
TypeError: string argument expected, got 'bytes'

--
nosy: +serhiy.storchaka

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-04 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Based on the input from Benjamin Peterson, I grab encoding from the os module 
in the getpass module.

I put some test as well.

Until the whole function is rewritten, I hope this patch will suffice and do 
the job properly.

--
Added file: http://bugs.python.org/file30463/getpass_tty_with_test.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-03 Thread Benjamin Peterson

Benjamin Peterson added the comment:

This code is pretty broken. I don't think ttys are ever seekable, so the 
os.fdopen has probably been always failing since 3.0. It thus always leaks an 
fd to '/dev/tty' if the first os.open succeeds. The whole function should 
probably be rewriten to work with byte streams encoding the prompt with 
os.device_encoding(tty_fd) falling back on locale.getpreferredencoding().

--
nosy: +benjamin.peterson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-02 Thread Vajrasky Kok

Vajrasky Kok added the comment:

Sorry,

My previous patch breaks the test. This one should pass the test and fix the 
bug.

Still, there are ugly code in the patch that I hope better programmers could 
fix.

--
Added file: 
http://bugs.python.org/file30445/getpass_fix_resourcewarning_pass_the_test.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-02 Thread Vajrasky Kok

Vajrasky Kok added the comment:

I have investigated this problem and come up with the patch to fix the problem. 
This patch does the job. Caution: only for Python 3.4. But translating this 
patch to Python 3.3 should be straightforward.

I hope this patch could be the foundation for better programmers to create 
better patch.

Some of the issues with this patch are: I am not sure how to handle encoding 
and where the best place to close tty is.

Reference:
https://github.com/stefanholek/term/issues/1
http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface

--
Added file: http://bugs.python.org/file30444/getpass_fix_resourcewarning.patch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-02 Thread Vajrasky Kok

Vajrasky Kok added the comment:

I isolate the bug. It happens in these lines:

# 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)

So to produce the bug more specifically, you can try this python file:

# bugme2.py
import os

fd = os.open('/dev/tty', os.O_RDWR|os.O_NOCTTY)
os.fdopen(fd, 'w+', 1)
# end of bugme2.py

In Linux Fedora 18, I would get this error:

/home/sky/Code/python/programming_language/cpython/Lib/os.py:1025: 
ResourceWarning: unclosed file <_io.FileIO name=3 mode='rb+'>
  return io.open(fd, *args, **kwargs)
Traceback (most recent call last):
  File "/tmp/bugme2.py", line 4, in 
os.fdopen(fd, 'w+', 1)
  File "/home/sky/Code/python/programming_language/cpython/Lib/os.py", line 
1025, in fdopen
return io.open(fd, *args, **kwargs)
io.UnsupportedOperation: File or stream is not seekable.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-01 Thread Vajrasky Kok

Vajrasky Kok added the comment:

This bug happens in Python 3.4 as well.

[sky@localhost cpython]$ ./python --version
Python 3.4.0a0
[sky@localhost cpython]$ ./python /tmp/bugme.py
/home/sky/Code/python/programming_language/cpython/Lib/os.py:1025: 
ResourceWarning: unclosed file <_io.FileIO name=3 mode='rb+'>
  return io.open(fd, *args, **kwargs)
What's up?

I tried to apply the patch manually (by copying, cutting and pasting) from Alex 
but Nikolaus is right. The patch does not work. The bug still happens

--
nosy: +vajrasky

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-01 Thread Nikolaus Rath

Nikolaus Rath added the comment:

Yes, I'm pretty sure:

[0] nikratio@vostro:~/tmp$ cp  /usr/lib/python3.3/getpass.py  .
[0] nikratio@vostro:~/tmp$ patch -p2 < issue18116.diff 
patching file getpass.py
Hunk #1 succeeded at 57 (offset -1 lines).

[0] nikratio@vostro:~/tmp$ python3 bugme.py 
/usr/lib/python3.3/os.py:1043: ResourceWarning: unclosed file <_io.FileIO 
name=3 mode='rb+'>
  return io.open(fd, *args, **kwargs)
What's up?

# Test if we're using the patched getpass.py...
[0] nikratio@vostro:~/tmp$ vim getpass.py
[0] nikratio@vostro:~/tmp$ python3 bugme.py 
Hello
/usr/lib/python3.3/os.py:1043: ResourceWarning: unclosed file <_io.FileIO 
name=3 mode='rb+'>
  return io.open(fd, *args, **kwargs)
What's up?

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-01 Thread Alex Gaynor

Alex Gaynor added the comment:

Are you sure you applied it correctly? With and without:

Alexanders-MacBook-Pro:cpython alex_gaynor$ ./python.exe x.py
What's up?
Alexanders-MacBook-Pro:cpython alex_gaynor$ hg revert --all --no-backup
reverting Lib/getpass.py
Alexanders-MacBook-Pro:cpython alex_gaynor$ ./python.exe x.py
What's up?
x.py:6: ResourceWarning: unclosed file <_io.TextIOWrapper name=3 mode='w+' 
encoding='UTF-8'>
  getpass.getpass("What's up?")

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-01 Thread Nikolaus Rath

Nikolaus Rath added the comment:

No, it doesn't.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-01 Thread Alex Gaynor

Alex Gaynor added the comment:

Attached patch should fix this issue.

--
keywords: +patch
nosy: +alex
Added file: http://bugs.python.org/file30442/issue18116.diff

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue18116] getpass.getpass() triggers ResourceWarning

2013-06-01 Thread Nikolaus Rath

New submission from Nikolaus Rath:

[0] nikratio@vostro:~/tmp$ cat bugme.py 
#!python
import getpass
import warnings

warnings.simplefilter('default')
getpass.getpass("What's up?")

[0] nikratio@vostro:~/tmp$ python3 --version
Python 3.3.2

[0] nikratio@vostro:~/tmp$ python3 bugme.py 
/usr/lib/python3.3/os.py:1043: ResourceWarning: unclosed file <_io.FileIO 
name=3 mode='rb+'>
  return io.open(fd, *args, **kwargs)
What's up?

--
components: Library (Lib)
messages: 190458
nosy: Nikratio
priority: normal
severity: normal
status: open
title: getpass.getpass() triggers ResourceWarning
type: resource usage
versions: Python 3.3

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com