Windows C runtime error R6034 when importing requests in embedded python

When I import requests from python embedded in C++, e.g. by calling
PyRun_SimpleString("import requests")
I get the C runtime error R6034.  This happens in Visual Studio 2010 or 2013, using the latest version of requests.  Is there a workaround? 
Please copy vapor@ucar.edu on any response to this issue.


Sorry, but this issue has nothing to do with requests. Requests is a pure-Python library, meaning that if normal Python code works, it should work. The error almost certainly comes from your embedding. Please investigate there first, and reopen this issue if you find strong evidence that suggests that Requests is at fault. 


I would agree with you if all the modules that Requests imports are pure-Python.  However, since we are only seeing this problem with Requests (no other imports exhibit this problem), there must be something that Requests is importing that uses a C runtime.  Would you please tell me what imports are occurring in Requests?


Requests by default imports only the standard library. It optionally may attempt to import pyopenssl, which could have C bindings. 


Cory,
I hear what you are saying, and yet it is clear to me that there is a
problem with using Requests with embedded Python in Windows.  I have a
trivial (10-line) program that reproduces the issue (attached).  I also
have a visual studio project that uses this program, you can download it
from: http://vis.ucar.edu/~alan/TEMP/importrequest.zip
You should be able to demonstrate the C runtime error quite easily.
Please check it out,
-Alan

On Mon, Sep 26, 2016 at 2:44 PM, Cory Benfield notifications@github.com
wrote:

> Requests by default imports only the standard library. It optionally may
> attempt to import pyopenssl, which could have C bindings.
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> https://github.com/kennethreitz/requests/issues/3599#issuecomment-249691856,
> or mute the thread
> https://github.com/notifications/unsubscribe-auth/AG55D_C5-_voipHX232wItL9jGNi4Ljbks5quC7DgaJpZM4KG1ti
> .


I am not disputing that there is a problem using Requests with embedded Python in Windows: you're trying to do it, and you hit a C runtime error. I am suggesting that the problem is not _Requests_.

In this case, what you're doing is extremely far outside the expertise of this development team. At best, I can tell you that when I run your script on my Unix development box, I encounter no problem. Of course, I have had to remember to pass the appropriate compiler configuration (obtained from running `python-config --cflags` and `python-config --ldflags`) to ensure that any shared libraries that were compiled against that Python are appropriately linkable. If I do that, then the import behaves as expected with no errors.

That means, as far as I can see, there are two problems: either you are not passing the correct compiler/linker flags, or this problem is Windows specific. Unfortunately, I do not have sufficient experience or access to Windows environments to easily investigate this issue. That means that I need to rely on you to confirm that everything is functioning correctly. For example, it would be worth trying to import a few other 3rd-party libraries to check whether it really is only Requests, or whether the problem is broader than that.


Cory,
Thanks for your serious examination of this problem.
We do know that this is a Windows-specific problem.  The code that imports
Requests is working fine on Mac and Linux.  We have not performed an
exhaustive search, but we have imported many (i.e. dozens of) different
modules and Requests is the only one that causes the C runtime error.   If
you send me a list of all the modules that are imported by Requests, then I
could test each of them to see if one of them causes the runtime error.

We have verified that this problem occurs on different Windows platforms
and different Visual Studio versions.  If anyone on your team has Windows
expertise and can offer us any suggestions we would very much appreciate
it.  We are expecting to release VAPOR 2.6 (http://www.vapor.ucar.edu) in
the next month and one of our features depends on a working Requests
module.  About 1/3 of our users are Windows-based.
Regards,
-Alan

On Tue, Sep 27, 2016 at 3:49 AM, Cory Benfield notifications@github.com
wrote:

> I am not disputing that there is a problem using Requests with embedded
> Python in Windows: you're trying to do it, and you hit a C runtime error. I
> am suggesting that the problem is not _Requests_.
> 
> In this case, what you're doing is extremely far outside the expertise of
> this development team. At best, I can tell you that when I run your script
> on my Unix development box, I encounter no problem. Of course, I have had
> to remember to pass the appropriate compiler configuration (obtained from
> running python-config --cflags and python-config --ldflags) to ensure
> that any shared libraries that were compiled against that Python are
> appropriately linkable. If I do that, then the import behaves as expected
> with no errors.
> 
> That means, as far as I can see, there are two problems: either you are
> not passing the correct compiler/linker flags, or this problem is Windows
> specific. Unfortunately, I do not have sufficient experience or access to
> Windows environments to easily investigate this issue. That means that I
> need to rely on you to confirm that everything is functioning correctly.
> For example, it would be worth trying to import a few other 3rd-party
> libraries to check whether it really is only Requests, or whether the
> problem is broader than that.
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> https://github.com/kennethreitz/requests/issues/3599#issuecomment-249818599,
> or mute the thread
> https://github.com/notifications/unsubscribe-auth/AG55Dz0hmkzeABlJ0W_WB7b9JSbInPkQks5quOa8gaJpZM4KG1ti
> .


@alannorton Here are all the import statements in the requests codebase. I should note that, while I'm happy to help here, this is information that would have been just as easy for you to obtain as it was for me.

Firstly, the stdlib imports:

```
argparse
base64
binascii
calendar
cgi
codecs
collections
contextlib
copy
datetime
email.utils
encodings.idna
errno
functools
hashlib
hmac
io
itertools
logging
mimetypes
operator
os
os.path
re
select
six
socket
ssl
struct
sys
threading
time
types
uuid
warnings
zlib
```

Then the optional 3rd party imports:

```
OpenSSL.SSL
ntlm
pyasn1.codec.der
pyasn1.type
```


I have good news!  The Windows C runtime error goes away when we use Python
2.7.11 instead of 2.7.8.   So no fix is needed.

On Tue, Sep 27, 2016 at 10:21 AM, Cory Benfield notifications@github.com
wrote:

> @alannorton https://github.com/alannorton Here are all the import
> statements in the requests codebase. I should note that, while I'm happy to
> help here, this is information that would have been just as easy for you to
> obtain as it was for me.
> 
> Firstly, the stdlib imports:
> 
> argparse
> base64
> binascii
> calendar
> cgi
> codecs
> collections
> contextlib
> copy
> datetime
> email.utils
> encodings.idna
> errno
> functools
> hashlib
> hmac
> io
> itertools
> logging
> mimetypes
> operator
> os
> os.path
> re
> select
> six
> socket
> ssl
> struct
> sys
> threading
> time
> types
> uuid
> warnings
> zlib
> 
> Then the optional 3rd party imports:
> 
> OpenSSL.SSL
> ntlm
> pyasn1.codec.der
> pyasn1.type
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub
> https://github.com/kennethreitz/requests/issues/3599#issuecomment-249917076,
> or mute the thread
> https://github.com/notifications/unsubscribe-auth/AG55D_b-uoRWNGNwbI8aGifDJ2FxrAsRks5quUKVgaJpZM4KG1ti
> .