[issue1384175] random module - Provider DLL failed to initialize correctly

2015-08-21 Thread eryksun

eryksun added the comment:

I forgot to set errcheck:

import ctypes

userenv = ctypes.WinDLL('userenv', use_last_error=True)

def errcheck_bool(result, func, args):
if not result:
raise ctypes.WinError(ctypes.get_last_error())
return args

userenv.CreateEnvironmentBlock.argtypes = (ctypes.POINTER(ctypes.c_void_p),
   ctypes.c_void_p,
   ctypes.c_int)
userenv.DestroyEnvironmentBlock.argtypes = (ctypes.c_void_p,)

userenv.CreateEnvironmentBlock.errcheck = errcheck_bool
userenv.DestroyEnvironmentBlock.errcheck = errcheck_bool

# ...

--

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



[issue1384175] random module - Provider DLL failed to initialize correctly

2015-08-21 Thread eryksun

eryksun added the comment:

Albert, this issue was opened for Python 2.4 and was closed over 6 years ago. 
Open a new issue if you believe there's a bug or room for improvement in a 
currently supported Python version. 

FYI, on Windows _PyOS_URandom first initializes the Crypto API by calling 
[CryptAcquireContext][1]. As shown in the debug session below, this function 
requires the SystemRoot environment variable to be correctly defined. 

C:\cdb -g C:\Program Files\Python27\python.exe

Microsoft (R) Windows Debugger Version 6.12.0002.633 AMD64
Copyright (c) Microsoft Corporation. All rights reserved.

...

 import os
 from ctypes import *
...
 PROV_RSA_FULL = 1
 CRYPT_VERIFYCONTEXT = 0xf000
 CryptAcquireContextA = WinDLL('advapi32').CryptAcquireContextA 
 hcrypt = c_void_p()

 del os.environ['SystemRoot']
 CryptAcquireContextA(byref(hcrypt), None, None, PROV_RSA_FULL, 
CRYPT_VERIFYCONTEXT)
ModLoad: 07fe`fc48 07fe`fc498000   
C:\Windows\system32\CRYPTSP.dll
CryptAcquireContext:  CheckSignatureInFile failed at cryptapi.c line 5198
CryptAcquireContext:  Failed to read registry signature value at cryptapi.c 
line 873
0

 os.environ['SystemRoot'] = 'C:\\Windows'
 CryptAcquireContextA(byref(hcrypt), None, None, PROV_RSA_FULL, 
CRYPT_VERIFYCONTEXT)
ModLoad: 07fe`fc18 07fe`fc1c7000   
C:\Windows\system32\rsaenh.dll
ModLoad: 07fe`fcae 07fe`fcaef000   
C:\Windows\system32\CRYPTBASE.dll
1
 hcrypt
c_void_p(4407952L)

It has always struck me as odd that this API uses the environment variable 
instead of the object manager's secured \SystemRoot object symbolic link. C'est 
la vie. 

Anyway, I don't think Python's os and subprocess modules should prevent you 
from shooting yourself in the foot here. When creating an environment block for 
a child process, excluding SystemRoot is generally a bad idea, but the language 
shouldn't get in the way. 

I think it's prudent to include at least all of the system-defined variables. 
Here's a function that calls [CreateEnvironmentBlock][2] and returns the system 
variables in a dict. It excludes user variables and doesn't inherit from the 
current process.

import ctypes

userenv = ctypes.WinDLL('userenv', use_last_error=True)

def errcheck_bool(result, func, args):
if not result:
raise ctypes.WinError(ctypes.get_last_error())
return args

userenv.CreateEnvironmentBlock.argtypes = (ctypes.POINTER(ctypes.c_void_p),
   ctypes.c_void_p,
   ctypes.c_int)
userenv.DestroyEnvironmentBlock.argtypes = (ctypes.c_void_p,)

def get_system_environ():
WCHAR_SIZE = ctypes.sizeof(ctypes.c_wchar)
environ = {}
cenv = ctypes.c_void_p()
userenv.CreateEnvironmentBlock(ctypes.byref(cenv), None, 0)
addr = cenv.value
try:
while True:
s = ctypes.c_wchar_p(addr).value
if not s:
break
i = s.find('=', 1)
if i != -1:
environ[s[:i]] = s[i+1:]
addr += (len(s) + 1) * WCHAR_SIZE
finally:
userenv.DestroyEnvironmentBlock(cenv)
return environ

[1]: https://msdn.microsoft.com/en-us/library/aa379886
[2]: https://msdn.microsoft.com/en-us/library/bb762270

--
nosy: +eryksun

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



[issue1384175] random module - Provider DLL failed to initialize correctly

2015-08-21 Thread Albert Zeyer

Albert Zeyer added the comment:

Ah thanks, that explains why it failed for me, and why it works after my fix, 
which was anyway what I intended.

I mostly posted my comment here in case someone else hits this, so he has 
another thing to check/debug.

I don't think that there is a bug on Python's side. Maybe a nice thing would be 
a small check in _PyOS_URandom for os.environ['SystemRoot'] and print a more 
helpful error.

--

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



[issue1384175] random module - Provider DLL failed to initialize correctly

2015-08-21 Thread Albert Zeyer

Albert Zeyer added the comment:

Note that there are still people who get this error in some strange cases, me 
included.

E.g.:
http://stackoverflow.com/questions/27904936/python-exe-file-crashes-while-launching-on-windows-xp/32137554#32137554

This happened at a call to `os.urandom` for me.
This was in a subprocess.

The bug for me was that I called `_subprocess.CreateProcess` with an 
`env_mapper = {'foo': 'bar'}`. The fix:

env_mapper = os.environ.copy()
env_mapper.update({'foo': 'bar'})

There is another related question 
[here](http://stackoverflow.com/questions/21791005/windows-error-provider-dll-failed-to-initialize-correctly-on-import-of-cgi-mo).
And some discussion on [this GitHub 
issue](https://github.com/aws/aws-cli/issues/1226).
All those seem to be related to `crypt32.dll` in a frozen Python app, or via 
py2app.

--
nosy: +Albert.Zeyer

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



[issue1384175] random module - Provider DLL failed to initialize correctly

2009-02-15 Thread Silas S. Brown

Silas S. Brown ss...@users.sourceforge.net added the comment:

I got a very similar error on an Otek Pocket PC running Windows Mobile
2003 SE and the latest version of pythonce from
pythonce.sourceforge.net.  The error is:

File
C:\devl\release\PythonCE-2.5-20061219\Python-2.5-wince\Lib\random.py,
line 108, in seed
type 'exceptions.WindowsError': [Error 87] Provider DLL failed to
initialize correctly

Although this was thrown up at the import random at the start of my
program, the actual change I made that resulted in this error was much
later in the program, and it was to change the lines

try: justSynthesize=raw_input(Say: )
except EOFError: break

into

try:
justSynthesize=raw_input(cond(winCEsound,.join(warnings_printed)+cond(warnings_printed,\n,),)+Say:
) # (WinCE uses an input box so need to repeat the warnings if any)
except EOFError: break

where cond is an if a then b else c function.

A little more investigation showed that the culprit was the comment! 
Removing the comment after the raw_input() call (or putting it on a
different line) causes the program to work again.

I confirmed that adding any raw_input() call to any function, with a
prompt parameter and a comment afterwards, causes this error to happen
on the import random near the top of the program.  This is a very
strange bug.

--
nosy: +ssb22

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



[issue1384175] random module - Provider DLL failed to initialize correctly

2009-02-15 Thread Silas S. Brown

Silas S. Brown ss...@users.sourceforge.net added the comment:

After further investigation I'm suspecting that this issue is actually
due to the process running out of RAM.

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



[issue1384175] random module - Provider DLL failed to initialize correctly

2009-02-08 Thread Daniel Diniz

Daniel Diniz aja...@gmail.com added the comment:

Given MvL's diagnostics and lack of response from OP, suggest closing.

--
nosy: +ajaksu2

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



[issue1384175] random module - Provider DLL failed to initialize correctly

2009-02-08 Thread Martin v. Löwis

Changes by Martin v. Löwis mar...@v.loewis.de:


--
resolution:  - works for me
status: open - closed
versions: +3rd party -Python 2.4

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