{{ message }}
gh-77188: Add support for pickling private methods and nested classes#21480
Merged
serhiy-storchaka merged 12 commits intoFeb 5, 2026
Merged
Conversation
MojoVampire
approved these changes
Oct 16, 2020
nanjekyejoannah
left a comment
Contributor
There was a problem hiding this comment.
Makes sense, please fix the conflict for merge
Contributor
Contributor
Author
|
Yes, I will update this PR. |
Member
|
In general, LGTM. But how much it affects the performance? Could you please run some microbenchmarks to measure the changes in the pickling time for non-private methods? If the difference is insignificant, it is good, but if it is large, it could be worth to optimize the code for non-private methods. |
vyrjana
added a commit
to vyrjana/lmfit-py
that referenced
this pull request
Nov 23, 2024
…kling - Attempting to make use of multiprocessing.Pool while providing, e.g., Minimizer.least_squares a function for calculating the Jacobian did not work prior to the renaming of the method due to an issue related to pickling and unpickling of private methods. See the (currently) open issue (python/cpython#77188) and pull request (python/cpython#21480) for more information.
newville
pushed a commit
to lmfit/lmfit-py
that referenced
this pull request
Dec 5, 2024
* Updated the Minimizer.least_squares method to wrap the Jacobian function - The Jacobian function provided by the user is now handled similarly to how it is handled in, e.g., Minimizer.leastsq. - If the Jacobian function is called 'Dfun' in the keyword arguments, then it is also accepted (Minimizer.scalar_minimize already does this). * Updated the Minimizer.__jacobian method signature to fix exception - The signature now matches the signature of Minimizer.__residual. This was changed because the following exception was being raised whenever the user provided a function for calculating the Jacobian: "TypeError: Minimizer.__jacobian() got an unexpected keyword argument 'apply_bounds_transformation'". * Updated the Minimizer.__jacobian method to use the new parameter - The updating of the parameter values is now similar to how it is done in Minimizer.__residual. * Updated test_least_squares_jacobian_types to handle the wrapped Jacobian function - The params argument of the jac_array function is now an actual Parameters object like it is in the f function, which means that the parameters can be accessed via string keys. * Updated the coerce_float64 function - Added explicit support for sparse matrices (e.g., Block Sparse Row) and LinearOperator objects. This change is needed because the updated test_least_squares_jacobian_types would otherwise fail. Similar to how these types are handled when calculating the Hessian matrix in Minimizer.least_squares. * Renamed the Minimizer.__jacobian method to avoid a bug related to pickling - Attempting to make use of multiprocessing.Pool while providing, e.g., Minimizer.least_squares a function for calculating the Jacobian did not work prior to the renaming of the method due to an issue related to pickling and unpickling of private methods. See the (currently) open issue (python/cpython#77188) and pull request (python/cpython#21480) for more information. * Added a test for when the "least_squares" method is provided a function for the Jacobian - Verifies that a) the fitted parameters have the expected values, b) the jac function is actually called, and c) that there are indeed fewer function evaluations as a result. * Updated the docstring of Minimizer._jacobian - The docstring is updated to include the parameters and return values. * Added tests for (un)pickling Minimizer when a Jacobian function is provided * Updated timeout value in test_jacobian_with_forkingpickler - Increased the timeout value so that slow runners can actually succeed instead of timing out. If no timeout was specified, then a failing test could end up hanging instead. * Added an example that demonstrates the benefits of providing a Jacobian function * Updated the list of contributors
gpshead
reviewed
Jan 6, 2026
| (len = PyUnicode_GET_LENGTH(funcname)) > 2 && | ||
| PyUnicode_READ_CHAR(funcname, 0) == '_' && | ||
| PyUnicode_READ_CHAR(funcname, 1) == '_' && | ||
| !(PyUnicode_READ_CHAR(funcname, len-1) == '_' && |
Member
There was a problem hiding this comment.
This would also fail to trigger on methods named ___ and ____ (3 and 4 underscores). it seems rather pedantic to care, but perhaps add a len > 4 check before checking for trailing _s and cover def ____(self): in the regression test?
Member
thunder-coding
pushed a commit
to thunder-coding/cpython
that referenced
this pull request
Feb 15, 2026
…lasses (pythonGH-21480) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
ljfp
pushed a commit
to ljfp/cpython
that referenced
this pull request
Apr 25, 2026
…lasses (pythonGH-21480) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

https://bugs.python.org/issue33007
#77188