Éric Araujo <[email protected]> added the comment:
I think we have a valid bug, and you correctly attributed it to the use of
partial!
(No worry about your English — thank you for reporting the problem here!)
To answer your questions:
1) partial:
Imagine we have a function with many parameters:
def frob(source, target, backup=False): ...
and we want to call it many times without passing backup=True every time; we
can make a lambda:
frob_backup = lambda source, target: frob(source, target, backup=True)
# later in the file
frob_backup(src1, tgt1)
frob_backup(src2, tgt2)
or a partial:
frob_backup = partial(frob, backup=True)
# then
frob_backup(src3, tgt3)
As you can see, they both work in the same way. They are equivalent ways of
making a shortcut to call a function with some parameters pre-defined. When
called, the lambda will call the frob function and return its return value.
When called, the partial object will call the frob function and return its
return value.
2) closures:
Closures are variables that are resolved in the parent scope (namespace) of the
normal scope.
A function (including a function created by a lambda) can have closures.
def make_callback(backup_default):
callback = lambda source, target: frob(source, target, backup_default)
return callback
Here when callback is called, the backup_default variable is not found in the
local variables of the callback function, but in the scope of make_callback.
It will be true or false depending on how make_callback was called.
3) http.server.test
There aren’t any closures in http.server.test; the issue is that HandlerClass
(defined in the `if name is main` block and passed to `test`) is a proper
handler class in one case, or a partial object! So inside test, setting
attributes on the passed handler class will do nothing if that class is
actually a partial instance. When it is called, it will return an instance of
SimpleHttpRequestHandler, which won’t see the protocol attribute that we wanted.
Conclusion:
- maybe we should pass all parameters to test instead of smuggling through
params
- setting class attributes seems fishy to me
- cgi handler and regular handler should have the same features (see #46436)
----------
nosy: +Jelle Zijlstra
resolution: not a bug ->
stage: resolved -> needs patch
status: closed -> open
title: http/server.py wont respect its protocol_version -> protocol_version in
http.server.test can be ignored
versions: +Python 3.10, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue46285>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com