[web2py] Re: AssertionError: Header names/values must be of type str

2023-10-25 Thread Massimo Di Pierro
I believe this is now fixed in master, thanks to Carlos' suggestion. Can 
you please give it a try?

On Monday, 23 October 2023 at 13:30:34 UTC-7 alex wrote:

> Hi, Running web2py in virtualenviroment (python 3.9.5, it happens also in 
> python 3.10.10)
>
> 
>
> ERROR:Rocket.Errors.Thread-3:Traceback (most recent call last):
>
>   File "/Users/z/dev/web2py/gluon/rocket.py", line 1294, in run
> self.run_app(conn)
>
>   File "/Users/z/dev/web2py/gluon/rocket.py", line 1796, in run_app
> output = self.app(environ, self.start_response)
>
>   File "/Users/z/dev/web2py/gluon/main.py", line 648, in app_with_logging
> ret[0] = wsgiapp(environ, responder2)
>
>   File "/Users/z/dev/web2py/gluon/main.py", line 562, in wsgibase
> return http_response.to(responder, env=env)
>
>   File "/Users/z/dev/web2py/gluon/http.py", line 129, in to
> responder(status, rheaders)
>
>   File "/Users/z/dev/web2py/gluon/main.py", line 643, in responder2
> return responder(s, h)
>
>   File "/Users/z/dev/web2py/gluon/rocket.py", line 1759, in start_response
> self.header_set = Headers(response_headers)
>
>   File "/Users/z/.pyenv/versions/3.9.5/lib/python3.9/wsgiref/headers.py", 
> line 39, in __init__
> self._convert_string_type(v)
>
>   File "/Users/z/.pyenv/versions/3.9.5/lib/python3.9/wsgiref/headers.py", 
> line 45, in _convert_string_type
> raise AssertionError("Header names/values must be"
>
> AssertionError: Header names/values must be of type str (got 50)
>
> 
> Any hints? 
> Thank you
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/2f971066-7e75-440e-80e0-cc132afacc93n%40googlegroups.com.


[web2py] Re: File Download Broken after Pull Request #2471

2023-10-25 Thread Massimo Di Pierro
I agree with you. I committed your proposed change to master. Please 
confirm this indeed fixes it and I will make another release.

On Wednesday, 4 October 2023 at 08:26:46 UTC-7 CarlosDB wrote:

> Hello,
>
> Today, I downloaded web2py from GitHub to start testing a future update. I 
> have several "upload" fields for images, and they have all started to fail.
>
> After some investigation, I found that this issue started after Pull 
> Request (PR) #2471 (Sanitize Headers) was merged. Link to PR 
> .
>  
> This PR modifies the file gluon/http.py.
>
> The following code:
> @cache.action(time_expire=86400)
> def download():
> return response.download(request, db)
>
> has stopped working.
>
> If I remove the line "@cache.action(time_expire=86400)", it works, but 
> that's not a good solution.
>
> While investigating the root of the problem, I found the issue around line 
> 123 of gluon/http.py. It used to be like this:
>
> rheaders = []
> for k, v in iteritems(headers):
> if isinstance(v, list):
> rheaders += [(k, item) for item in v]
> else:
> rheaders.append((k, v))
> responder(status, rheaders)
>
> Now it looks like this:
>
> rheaders = []
> for k, v in iteritems(headers):
> if isinstance(v, list):
> rheaders += [(k, str(item)) for item in v]
> elif v is not None:
> rheaders.append((k, str(v)))
> responder(status, rheaders)
>
>
> The variable "rheaders" contains the HTTP headers, but with these 
> modifications, some values are int or None, but they should be str. For 
> example:
> ('Content-Length', 29563)
> ('Pragma', None)
>
> ('Content-Length', 29563) should be ('Content-Length', '29563'), and I 
> believe ('Pragma', None) is a deprecated header that should be removed.
>
> A possible solution could be to revert part of the PR, but I think there 
> might be a better solution.
>
> The real issue seems to originate from the files gluon/streamer.py and 
> gluon/cache.py. I have commented on the lines that have problems:
>
> *In gluon/streamer.py, line 127*:
>
> # in all the other cases (not 304, not 206, but 200 or error page)
> if status != 206:
> enc = request.env.http_accept_encoding
> if enc and 'gzip' in enc and not 'Content-Encoding' in headers:
> gzipped = static_file + '.gz'
> if os.path.isfile(gzipped) and os.path.getmtime(gzipped) >= 
> modified:
> static_file = gzipped
> fsize = os.path.getsize(gzipped)
> headers['Content-Encoding'] = 'gzip'
> headers['Vary'] = 'Accept-Encoding'
> try:
> stream = open(static_file, 'rb')
> except IOError as e:
> # this better not happen when returning an error page ;-)
> if e.errno in (errno.EISDIR, errno.EACCES):
> raise HTTP(403)
> else:
> raise HTTP(404)
> headers['Content-Length'] = fsize   # should be: 
> headers['Content-Length'] = str(fsize)
>
> *In gluon/cache.py, line 676:*
>
> if send_headers:
> headers = {'Pragma': None,  # Deprecated: This 
> feature is no longer recommended
>'Expires': expires,
>'Cache-Control': cache_control}
>
>
> What is your opinion?
> Best way to solve it? 
>
> Carlos.

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/67791b69-f9c6-41cc-a663-0951d8288127n%40googlegroups.com.


[web2py] Re: app not loading external python library

2023-10-25 Thread Massimo Di Pierro
Hello Alex,

thank for reporting this problem. Is it only occurring with web2py 2.25.1? 

On Monday, 23 October 2023 at 14:05:15 UTC-7 alex wrote:

> Hi, 
>
> I am running web2py Version 2.25.1-stable in virtualenv (python 3.9.5, it 
> happens also in python 3.10.10)
>
> After activating the virtualenv, I installed requests
>
> $ pip install request
> $ pip show requests
> Name: requests
> Version: 2.31.0
> Summary: Python HTTP for Humans.
> Home-page: https://requests.readthedocs.io
> Author: Kenneth Reitz
> Author-email: m...@kennethreitz.org
> License: Apache 2.0
> Location: 
> /Users/z/dev/web2py/.direnv/python-3.9/lib/python3.9/site-packages
> Requires: certifi, charset-normalizer, idna, urllib3
> Required-by:
>
> Importing requests from bpython gives no errors.
>
> Importing requests in myapp gives an error.
> I also tried with other external libraries, some of them do work, some 
> others cannot be imported.
>
> About this case, I have tried the following: 
>
> In "web2py/applications/myapp/models/0.py":
>
> # - - - - - - - - - - - - - - -
>
> # added the virtualenv path to sys.path
>
> site_package_path = 
> "/Users/z/dev/web2py/.direnv/python-3.9/lib/python3.9/site-packages"
>
> if site_package_path not in sys.path:
> sys.path.append(site_package_path)
>
> try:
> import requests
> except:
> raise Exception('import error: "requests" not found')
>
>
> # - - - - - - - - - - - - - - -
>
> I am running web2py with: 
>
> $python web2py.py -a 'pwd'
>
> Error log:
>
> # - - - - - - - - - - - - - - -
> Ticket ID
> 127.0.0.1.2023-10-24.05-48-51.a557dcfb-55a3-4063-ac27-879cad8fe799
>
>  import error: "requests" not found
> Version
> web2py™ Version 2.25.1-stable+timestamp.2023.10.08.18.44.43
> Python Python 3.9.5: /Users/z/dev/web2py/.direnv/python-3.9/bin/python 
> (prefix: /Users/z/dev/web2py/.direnv/python-3.9)
>
> ...
>
> Traceback (most recent call last):
>   File "/Users/z/dev/web2py/gluon/custom_import.py", line 78, in 
> custom_importer
> result = sys.modules[modules_prefix]
> KeyError: 'applications.wh.modules.requests'
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>   File "/Users/z/dev/web2py/applications/wh/models/0.py", line 29, in 
> 
> import requests
>   File "/Users/z/dev/web2py/gluon/custom_import.py", line 80, in 
> custom_importer
> raise ImportError("No module named %s" % modules_prefix)
> ImportError: No module named applications.wh.modules.requests
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
>   File "/Users/z/dev/web2py/gluon/restricted.py", line 219, in restricted
> exec(ccode, environment)
>   File "/Users/z/dev/web2py/applications/wh/models/0.py", line 31, in 
> 
> raise Exception('import error: "requests" not found')
> Exception: import error: "requests" not found
> Error snapshot help
> Exception(import error: "requests" not found)
>
> inspect attributes
>
> Exception instance attributes
> __cause__ None
> __class__ 
> __context__ ImportError('No module named 
> applications.wh.modules.requests')
> __delattr__ 
> __dict__ {}
> __dir__ 
> __doc__ 'Common base class for all non-exit exceptions.'
> __eq__ 
> __format__ 
> __ge__ 
> __getattribute__ 
> __gt__ 
> __hash__ 
> __init__ 
> __init_subclass__ 
> __le__ 
> __lt__ 
> __ne__ 
> __new__ 
> __reduce__ 
> __reduce_ex__ 
> __repr__ 
> __setattr__ 
> __setstate__ 
> __sizeof__ 
> __str__ 
> __subclasshook__ 
> __suppress_context__ False
> __traceback__ 
> args ('import error: "requests" not found',)
> with_traceback 
> Frames
> File /Users/z/dev/web2py/gluon/restricted.py in restricted at line 219 
> code arguments variables
>
> File /Users/z/dev/web2py/applications/wh/models/0.py in  at line 
> 31 code arguments variables
>
> Function argument list
> ()
>
> Context locals
>
> ...
>
>
> 
> iteritems : 
> >
> local_import : 
> .>
> myconf : 
> {'app': {'name': 'WH', 'author': 'Your Name  'bootstrap3_inline', 'separator': ''}}
> redirect : 
> 
> reduce : 
> 
> request : 
> , 
> '_custom_import_track_changes': True}>
> response : 
>  'view': 'default/index.html'}>
> session : 
> 
> site_package_path : 
> "'/Users/z/dev/web2py/.direnv/python-3.9/lib/python3.9/site-packages'"
> sys : 
> 
> to_bytes : 
> 
> to_native : 
> 
> track_changes : 
> 
> xmlescape : 
> 
> xrange : 
> 
> session
> response
> body : 
> <_io.StringIO object at 0x10509de50>
> cookies : 
> session_id_wh : 
> 127.0.0.1-3599de20-98fe-4a6f-a572-aa7d12f28231
> comment : 
> domain : 
> expires : 
> httponly : 
> max-age : 
> path : 
> /
> samesite : 
> secure : 
> version : 
> delimiters : 
> {{
> }}
> files : 
> flash : 
> form_label_separator : 
> :
> formstyle : 
> table3cols
> generic_patterns : 
> *
> headers : 
> X-Powered-By : 
> web2py
> menu : 
> meta : 
> models_to_run : 
> ^\w+\.py$
> ^default/\w+\.py$
> ^default/index/\w+\.py$
> postprocessing : 
> session_client : 
> 127.0.0.1
>