Requester icon indicating copy to clipboard operation
Requester copied to clipboard

import in hooks definde in env/envfile doesn't work

Open bmlynarczyk-lingaro opened this issue 5 years ago • 2 comments

Windows 10 Pro/19042.928 Sublime Text 4/Build 4102

import in hooks defined in env/envfile doesn't work request:

###env
import json

class TokenAuth:

    def __call__(self, r, stream, cert, timeout, verify, proxies):
        json.loads('{"a":"a"}')
###env

requests.post('https://wp.pl', hooks={'response': TokenAuth()})

console output:

error: requests.post('https://wp.pl', hooks={'response': TokenAuth()})
Other Error: 'NoneType' object has no attribute 'loads'

bmlynarczyk-lingaro avatar Apr 27 '21 22:04 bmlynarczyk-lingaro

Thanks for filing this issue. Try this:

###env

class TokenAuth:

    def __call__(self, r, stream, cert, timeout, verify, proxies):
        import json
        json.loads('{"a":"a"}')
###env

requests.post('https://wp.pl', hooks={'response': TokenAuth()})

This is obviously not ideal. Something about how the env dict is built is preventing the import json statement from having any effect, and not being able to import certain modules from the standard library is bad

When I have some time I'll try to figure this out. ~~I noticed that this works, so we can import other modules from the standard library without trouble~~. Nvm, zlib below is also None

###env
import zlib


class TokenAuth:

    def __call__(self, r, stream, cert, timeout, verify, proxies):
        import json
        print(zlib)
        json.loads('{"a":"a"}')
###env

requests.post('https://wp.pl', hooks={'response': TokenAuth()})

kylebebak avatar May 12 '21 19:05 kylebebak

Thank you @kylebebak! Workaround works as you write above. But importing others modules doesn't work as well. Please look into console output, in mine there is None when zlib is printed. So this is same behavior like in case of json, but in this situation any method isn't invoked on zlib so error doesn't appear during request sending.

bmlynarczyk-lingaro avatar May 12 '21 20:05 bmlynarczyk-lingaro