-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Add CodeType.replace() method #81213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Each type types.CodeType constructor changes, a lot of applications break. Python 3.8 added a new parameter which broke for example the Genshi project, just to name one. I propose to add a new CodeType.replace() method, similar to datetime.datetime.replace and namedtuple._replace() for example: $ python3
Python 3.7.3 (default, Mar 27 2019, 13:41:07) >>> import datetime
>>> d=datetime.datetime.now()
>>> d2=d.replace(year=2010)
>>> d, d2
(datetime.datetime(2019, 5, 24, 11, 25, 3, 839966), datetime.datetime(2010, 5, 24, 11, 25, 3, 839966))
>>> import collections
>>> Point = collections.namedtuple("Point", "x y")
>>> p=Point(1, 2)
>>> p2=p._replace(y=3)
>>> p, p2
(Point(x=1, y=2), Point(x=1, y=3)) |
Pablo fixed it in Genshi: |
See also bpo-37034: "Argument Clinic omits name of keyword-only parameter on _PyArg_BadArgument() call". |
Fix in Hypothesis: |
ipython fix: |
It's not like Python 3.8 is the first type in Python history that code constructor changed. The PyCode_New() function slowly growed from 3 parameters in 1990 to 16 paramters in 2019 :-) History of PyCode_New(). New "posonlyargcount" parameter: commit 8c77b8c
New "kwonlyargcount" parameter, bpo-1549670: commit 4f72a78
New "freevars" and "cellvars" parameters, PEP-227: commit 64949cb
New "firstlineno" and "lnotab" parameters: commit da4eb5c
New "stacksize" parameter: commit 8b993a9
New "argcount", "nlocals", "flags" and "varnames" parameters: commit 681d79a
New "name" parameter: commit 9bfef44
New "filename" parameter: commit 3f5da24
The very first version of the function was: static codeobject * From this commit: commit 10dc2e8
|
Oops, typo: It's not like Python 3.8 is the first *time* in Python history that the code constructor changed. |
Fix in Cloud Pickle: |
See bpo-36886 "Failed to construct CodeType on Python-3.8.0a4" for other failures caused by the addition of the "posonlyargcount" parameter to code constructor. |
Once Python 3.8 beta1 will be released, it would be interesting to patch all projects that I had to be fixed to handle the new "posonlyargcount" to use the new "replace()" method is available. Maybe even remove the fix for Python 3.8 alpha 4 and only use use replace() on Python 3.8 and newer. |
Is there anything more on this issue? |
Pablo: "Is there anything more on this issue?" I proposed "Once Python 3.8 beta1 will be released, it would be interesting to patch all projects that I had to be fixed to handle the new "posonlyargcount" to use the new "replace()" method is available." But that only concerns third party project,s no Python directly, so I close the issue :-) |
Thanks for this great improvement! Was about to suggest having something more futureproof when noticing this with pdbpp, found this, and submitted PRs to use it for ipython and hypothesis also. |
Cool! Thank you for doing that ;-) |
Found this great improvement years after my frustration, many thanks for this work! |
I'm glad that it helped you ;-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: