[issue29960] _random.Random state corrupted on exception
Changes by Bryan G. Olson : -- pull_requests: +1181 ___ Python tracker <http://bugs.python.org/issue29960> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29960] _random.Random state corrupted on exception
Changes by Bryan G. Olson : -- pull_requests: +1135 ___ Python tracker <http://bugs.python.org/issue29960> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29960] _random.Random state corrupted on exception
Bryan G. Olson added the comment: I'm going through https://docs.python.org/devguide/pullrequest.html and would like to be assigned this issue. -- ___ Python tracker <http://bugs.python.org/issue29960> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29960] _random.Random state corrupted on exception
New submission from Bryan G. Olson: Demo: Run the Python library's test_random.py under the Python debugger and check the generator at the start of test_shuffle(): C:\bin\Python36>python -m pdb Lib\test\test_random.py > c:\bin\python36\lib\test\test_random.py(1)() -> import unittest (Pdb) break 61 Breakpoint 1 at c:\bin\python36\lib\test\test_random.py:61 (Pdb) continue .> c:\bin\python36\lib\test\test_random.py(61)test_shuffle() -> shuffle = self.gen.shuffle (Pdb) list 56 # randomness source is not available. 57 urandom_mock.side_effect = NotImplementedError 58 self.test_seedargs() 59 60 def test_shuffle(self): 61 B-> shuffle = self.gen.shuffle 62 lst = [] 63 shuffle(lst) 64 self.assertEqual(lst, []) 65 lst = [37] 66 shuffle(lst) (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 (Pdb) p self.gen.getrandbits(31) 2137781566 That's not random. Diagnosis: The order in which test functions run is the lexicographic order of their names. Thus unittest ran test_setstate_middle_arg() before running test_shuffle(). test_setstate_middle_arg() did some failed calls to _random.Random.setstate(), which raised exceptions as planned, but also trashed the state of the generator. test_random.py continues to use the same instance of _random.Random after setstate() raises exceptions. The documentation for Random.setstate() does not specify what happens to the state of the generator if setstate() raises an exception. Fortunately the generator recommended for secure applications, SystemRandom, does not implement setstate(). Solution: The fix I prefer is a small change to random_setstate() in _randommodule.c, so that it does not change the state of the generator until the operation is sure to succeed. -- components: Library (Lib) messages: 290977 nosy: bryangeneolson priority: normal severity: normal status: open title: _random.Random state corrupted on exception type: behavior versions: Python 3.7 ___ Python tracker <http://bugs.python.org/issue29960> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24732] 3.5.0b3 Windows accept() on unready non-blocking socket raises PermissionError
New submission from Bryan G. Olson: In Python 3.4 on Windows 7, the code: import socket sock = socket.socket() sock.bind(('127.0.0.1', 52384)) sock.listen(5) sock.setblocking(False) csock, addr = sock.accept() Raised: Traceback (most recent call last): File "socket_bug_test.py", line 8, in csock, addr = sock.accept() File "c:\bin\Python34\lib\socket.py", line 187, in accept fd, addr = self._accept() BlockingIOError: [WinError 10035] A non-blocking socket operation could not be completed immediately In Python 3.5.0b3 on the same system, it is raising: Traceback (most recent call last): File "socket_bug_test.py", line 8, in csock, addr = sock.accept() File "c:\bin\Python35\lib\socket.py", line 195, in accept fd, addr = self._accept() PermissionError: [WinError 5] Access is denied This is a problem for other parts of the Standard Library. I hit it playing with asyncio, where in Lib\asyncio\selector_events.py the function BaseSelectorEventLoop._sock_accept() is prepared for an unready socket to raise BlockingIOError or InterruptedError, but does not catch the WinError: try: conn, address = sock.accept() conn.setblocking(False) except (BlockingIOError, InterruptedError): self.add_reader(fd, self._sock_accept, fut, True, sock) except Exception as exc: fut.set_exception(exc) else: fut.set_result((conn, address)) There are other calls to accept in accept() in asyncio, that I haven't tested but also look adversely affected. The older asyncore module looks to have a similar problem. In dispatcher.accept(): def accept(self): # XXX can return either an address pair or None try: conn, addr = self.socket.accept() except TypeError: return None except OSError as why: if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN): return None else: raise else: return conn, addr The 'except OSError as why' will catch the WinError, but why.args[0] will be errno.EACCES = 13, permission denied, which is not equal to any of anticipated errors. My system according to Python: >>> import sys >>> sys.version '3.5.0b3 (default, Jul 5 2015, 07:00:46) [MSC v.1900 64 bit (AMD64)]' >>> sys.getwindowsversion() sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') That's Windows 7 Professional, 64-bit, with Service pack 1. It has an AND Phenom II X6 1055T Processor 2.8 GHz, and 8GB of memory. -- components: Windows messages: 247452 nosy: bryangeneolson, paul.moore, steve.dower, tim.golden, zach.ware priority: normal severity: normal status: open title: 3.5.0b3 Windows accept() on unready non-blocking socket raises PermissionError type: behavior versions: Python 3.5 ___ Python tracker <http://bugs.python.org/issue24732> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue17010] Windows launcher ignores active virtual environment
New submission from Bryan G. Olson: Python 3.3 includes PEP 397, a Python launcher for Windows, and PEP 405, virtual environment support in core. Unfortunately the Windows launcher does not respect virtual environments. Even with with a virtual environment activated and the current directory at the virtual environment's root, the Windows launcher will start python with the system environment, not the active virtual environment. To demo: Install python 3.3 for windows. Create a virtual environment: C:\>c:\Python33\Tools\Scripts\pyvenv.py c:\virtpy Activate the virtual environment: C:\>virtpy\Scripts\activate.bat (virtpy) C:\> Optionally cd to the virtual environment: (virtpy) C:\>cd virtpy (virtpy) C:\virtpy> Start Python 3 with the new windows launcher: (virtpy) C:\virtpy>py -3 Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> Check sys.path >>> import sys >>> >>> sys.path ['', 'C:\\Windows\\system32\\python33.zip', 'C:\\bin\\Python33\\DLLs', 'C:\\bin\\Python33\\lib', 'C:\\bin\\Python33', 'C:\\bin\\Python33\\lib\\site-packages'] >>> The worst effect I've found is that installation of a package with windows launcher, "py -3 setup.py install", will ignore the active virtual environment and will change the system Python environment. That's bad because users frequently employ virtual environments to isolate changes that could damage a system configuration. -- components: Installation, Windows messages: 180362 nosy: bryangeneolson priority: normal severity: normal status: open title: Windows launcher ignores active virtual environment type: behavior versions: Python 3.3 ___ Python tracker <http://bugs.python.org/issue17010> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com