This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.araujo
Recipients JelleZijlstra, eric.araujo, openalmeida
Date 2022-01-23.00:01:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1642896067.89.0.0787572906381.issue46285@roundup.psfhosted.org>
In-reply-to
Content
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)
History
Date User Action Args
2022-01-23 00:01:07eric.araujosetrecipients: + eric.araujo, JelleZijlstra, openalmeida
2022-01-23 00:01:07eric.araujosetmessageid: <1642896067.89.0.0787572906381.issue46285@roundup.psfhosted.org>
2022-01-23 00:01:07eric.araujolinkissue46285 messages
2022-01-23 00:01:07eric.araujocreate