forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 5
Adding Deep Immutability to 3.12 #51
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
Open
matajoh
wants to merge
40
commits into
fxpl:v3.12.0
Choose a base branch
from
matajoh:immutable-pep
base: v3.12.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 37 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
7ad9131
Proposed code changes for [PEP XXX](link).
matajoh 3b832e6
Addressing PR comments
matajoh d52b222
Cleaning up the immutability code
matajoh 80b648c
deque working
matajoh 697d3b1
Adding write barriers for array
matajoh 4024677
Cleaning up function freezing + more tests
matajoh 9136246
Immutability for blake2 + test_freeze module
matajoh 61a0400
Fixing some holes in object/typeobject
matajoh 6a6b04b
Remove immutable keys from dictionary as no longer required
mjp41 0caa788
Dealing with buffer immutability
matajoh a5ceb51
_ctypes immutability
matajoh 2e54f32
_decimal
matajoh 32d3fe7
Adding a NotFreezable type
matajoh 37f2d76
_io
matajoh fe5de34
Fixing some broken tests
matajoh fef1fdb
Fixing the name of NotWritableError
matajoh ab10e21
_multiprocessing _sqlite3
matajoh a7dbd1a
cjkcodecs
matajoh dd611e4
_abc
matajoh 47fab24
_asyncio
matajoh 99f259c
Fixing import error
matajoh 4f7816a
bz2
matajoh 09fb9a7
_collections _csv
matajoh 11119d0
Moving Py_Freeze to public API
matajoh baff5f8
xxlimited, xxsubtype, zlib
xFrednet 93eca6d
_dbm etree
matajoh 2ab77d3
immutable module
matajoh a4d42da
All tests passing again
matajoh 5a0b5b7
_datetime
matajoh 2fab5cd
_struct
matajoh f621a0c
Making freezable types a weakset
matajoh a31e962
These don't need to be freezable
matajoh adc1319
Make mutable during finalisation. (#6)
mjp41 ccb893c
Dealing with more finalisation bugs
matajoh 30dd83f
Shadow cell
matajoh 57df4db
Removing isfreezable and adding some TODOs
matajoh 4866d04
Using import helper
matajoh 0387c12
Adding the bases tuple as discussed
matajoh c867271
Initial simple Python module.
81aebf3
Add .clang-format file.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #ifndef Py_CPYTHON_IMMUTABLE_H | ||
| # error "this header file must not be included directly" | ||
| #endif | ||
|
|
||
| PyAPI_DATA(PyTypeObject) _PyNotFreezable_Type; | ||
|
|
||
| PyAPI_FUNC(int) _PyImmutability_Freeze(PyObject*); | ||
| PyAPI_FUNC(int) _PyImmutability_RegisterFreezable(PyTypeObject*); |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef Py_IMMUTABILITY_H | ||
| #define Py_IMMUTABILITY_H | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
|
|
||
| #ifndef Py_LIMITED_API | ||
| # define Py_CPYTHON_IMMUTABLE_H | ||
| # include "cpython/immutability.h" | ||
| # undef Py_CPYTHON_IMMUTABLE_H | ||
| #endif | ||
|
|
||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif /* !Py_IMMUTABILITY_H */ |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #ifndef Py_INTERNAL_IMMUTABILITY_H | ||
| #define Py_INTERNAL_IMMUTABILITY_H | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| #ifndef Py_BUILD_CORE | ||
| # error "Py_BUILD_CORE must be defined to include this header" | ||
| #endif | ||
|
|
||
| struct _Py_immutability_state { | ||
| PyObject *module_locks; | ||
| PyObject *blocking_on; | ||
| PyObject *freezable_types; | ||
| PyObject *destroy_cb; | ||
| }; | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif /* !Py_INTERNAL_IMMUTABILITY_H */ |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,6 +78,25 @@ whose size is determined when the object is allocated. | |
| /* PyObject_HEAD defines the initial segment of every PyObject. */ | ||
| #define PyObject_HEAD PyObject ob_base; | ||
|
|
||
| /* | ||
| Immutability: | ||
|
|
||
| Immutability is tracked in the top bit of the reference count. The immutability | ||
| system also uses the second-to-top bit for managing immutable graphs. | ||
| */ | ||
|
|
||
| #if SIZEOF_VOID_P > 4 | ||
| #define _Py_REFCNT_MASK 0xFFFFFFFF | ||
| #define _Py_IMMUTABLE_FLAG 0x4000000000 | ||
| #define _Py_IMMUTABLE_SCC_FLAG 0x8000000000 | ||
| #else | ||
| #define _Py_REFCNT_MASK 0x1FFFFFFF | ||
| #define _Py_IMMUTABLE_FLAG 0x20000000 | ||
| #define _Py_IMMUTABLE_SCC_FLAG 0x40000000 | ||
| #endif | ||
|
|
||
| #define _Py_IMMUTABLE_MASK (_Py_IMMUTABLE_SCC_FLAG | _Py_IMMUTABLE_FLAG) | ||
|
|
||
| /* | ||
| Immortalization: | ||
|
|
||
|
|
@@ -112,17 +131,17 @@ be done by checking the bit sign flag in the lower 32 bits. | |
| #else | ||
| /* | ||
| In 32 bit systems, an object will be marked as immortal by setting all of the | ||
| lower 30 bits of the reference count field, which is equal to: 0x3FFFFFFF | ||
| lower 28 bits of the reference count field, which is equal to: 0x0FFFFFFF. | ||
|
|
||
| Using the lower 30 bits makes the value backwards compatible by allowing | ||
| Using the lower 28 bits makes the value backwards compatible by allowing | ||
| C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely | ||
| increase and decrease the objects reference count. The object would lose its | ||
| immortality, but the execution would still be correct. | ||
|
|
||
| Reference count increases and decreases will first go through an immortality | ||
| check by comparing the reference count field to the immortality reference count. | ||
| */ | ||
| #define _Py_IMMORTAL_REFCNT (UINT_MAX >> 2) | ||
| #define _Py_IMMORTAL_REFCNT (UINT_MAX >> 4) | ||
| #endif | ||
|
|
||
| // Make all internal uses of PyObject_HEAD_INIT immortal while preserving the | ||
|
|
@@ -208,7 +227,7 @@ PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y); | |
|
|
||
|
|
||
| static inline Py_ssize_t Py_REFCNT(PyObject *ob) { | ||
| return ob->ob_refcnt; | ||
| return ob->ob_refcnt & _Py_REFCNT_MASK; | ||
| } | ||
| #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 | ||
| # define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob)) | ||
|
|
@@ -242,7 +261,7 @@ static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op) | |
| #if SIZEOF_VOID_P > 4 | ||
| return _Py_CAST(PY_INT32_T, op->ob_refcnt) < 0; | ||
| #else | ||
| return op->ob_refcnt == _Py_IMMORTAL_REFCNT; | ||
| return (op->ob_refcnt & _Py_REFCNT_MASK) == _Py_IMMORTAL_REFCNT; | ||
| #endif | ||
| } | ||
| #define _Py_IsImmortal(op) _Py_IsImmortal(_PyObject_CAST(op)) | ||
|
|
@@ -254,16 +273,44 @@ static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) { | |
| # define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type)) | ||
| #endif | ||
|
|
||
| static inline Py_ALWAYS_INLINE int _Py_IsImmutable(PyObject *op) | ||
| { | ||
| return (op->ob_refcnt & _Py_IMMUTABLE_MASK) > 0; | ||
| } | ||
| #define _Py_IsImmutable(op) _Py_IsImmutable(_PyObject_CAST(op)) | ||
|
|
||
| // Check whether an object is writeable. | ||
| // This check will always succeed during runtime finalization. | ||
| #define Py_CHECKWRITE(op) ((op) && (!_Py_IsImmutable(op) || Py_IsFinalizing())) | ||
| #define Py_REQUIREWRITE(op, msg) {if (Py_CHECKWRITE(op)) { _PyObject_ASSERT_FAILED_MSG(op, msg); }} | ||
|
|
||
| static inline Py_ALWAYS_INLINE int _Py_IsImmortalOrImmutable(PyObject *op) | ||
| { | ||
| // TODO(Immutable): Does this work for both 32 and 64bit? | ||
| return op->ob_refcnt >= _Py_IMMORTAL_REFCNT; | ||
| } | ||
| #define _Py_IsImmortalOrImmutable(op) _Py_IsImmortalOrImmutable(_PyObject_CAST(op)) | ||
|
|
||
| static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { | ||
| // This immortal check is for code that is unaware of immortal objects. | ||
| // The runtime tracks these objects and we should avoid as much | ||
| // as possible having extensions inadvertently change the refcnt | ||
| // of an immortalized object. | ||
| if (_Py_IsImmortal(ob)) { | ||
| if (_Py_IsImmortalOrImmutable(ob)) | ||
| { | ||
| if (_Py_IsImmortal(ob)) { | ||
| return; | ||
| } | ||
| assert(_Py_IsImmutable(ob)); | ||
| // TODO(Immutable): It is dangerous to set the reference count of an | ||
| // immutable object. The majority of calls appear to be where the rc | ||
| // has reached 0 and a finalizer is running. This seems a reasonable | ||
| // place to allow the refcnt to be set to 1, and clear the immutable flag. | ||
| assert((ob->ob_refcnt & _Py_REFCNT_MASK) == 0); | ||
| ob->ob_refcnt = refcnt; | ||
| return; | ||
| } | ||
| ob->ob_refcnt = refcnt; | ||
| ob->ob_refcnt = (ob->ob_refcnt & _Py_IMMUTABLE_MASK) | (refcnt & _Py_REFCNT_MASK); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the immutability flag always be 0 here due to the if statement above? |
||
| } | ||
| #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 | ||
| # define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt)) | ||
|
|
@@ -622,6 +669,8 @@ PyAPI_FUNC(void) Py_DecRef(PyObject *); | |
| PyAPI_FUNC(void) _Py_IncRef(PyObject *); | ||
| PyAPI_FUNC(void) _Py_DecRef(PyObject *); | ||
|
|
||
| PyAPI_FUNC(int) _Py_DecRef_Immutable(PyObject *op); | ||
|
|
||
| static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op) | ||
| { | ||
| #if defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG)) | ||
|
|
@@ -682,12 +731,21 @@ static inline void Py_DECREF(const char *filename, int lineno, PyObject *op) | |
| if (op->ob_refcnt <= 0) { | ||
| _Py_NegativeRefcount(filename, lineno, op); | ||
| } | ||
| if (_Py_IsImmortal(op)) { | ||
|
|
||
| if (_Py_IsImmortalOrImmutable(op)) | ||
| { | ||
| if (_Py_IsImmortal(op)) { | ||
| return; | ||
| } | ||
| assert(_Py_IsImmutable(op)); | ||
| if (_Py_DecRef_Immutable(op)) | ||
| _Py_Dealloc(op); | ||
| return; | ||
| } | ||
| _Py_DECREF_STAT_INC(); | ||
| _Py_DECREF_DecRefTotal(); | ||
| if (--op->ob_refcnt == 0) { | ||
| op->ob_refcnt -= 1; | ||
| if ((op->ob_refcnt & _Py_REFCNT_MASK) == 0) { | ||
| _Py_Dealloc(op); | ||
| } | ||
| } | ||
|
|
@@ -698,11 +756,19 @@ static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op) | |
| { | ||
| // Non-limited C API and limited C API for Python 3.9 and older access | ||
| // directly PyObject.ob_refcnt. | ||
| if (_Py_IsImmortal(op)) { | ||
| if (_Py_IsImmortalOrImmutable(op)) | ||
| { | ||
| if (_Py_IsImmortal(op)) { | ||
| return; | ||
| } | ||
| assert(_Py_IsImmutable(op)); | ||
| if (_Py_DecRef_Immutable(op)) | ||
| _Py_Dealloc(op); | ||
| return; | ||
| } | ||
| _Py_DECREF_STAT_INC(); | ||
| if (--op->ob_refcnt == 0) { | ||
| op->ob_refcnt -= 1; | ||
| if ((op->ob_refcnt & _Py_REFCNT_MASK) == 0) { | ||
| _Py_Dealloc(op); | ||
| } | ||
| } | ||
|
|
||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import os | ||
| from test.support import load_package_tests | ||
|
|
||
|
|
||
| def load_tests(*args): | ||
| return load_package_tests(os.path.dirname(__file__), *args) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from . import load_tests | ||
| import unittest | ||
|
|
||
| unittest.main() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| a = 1 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean that different interpreters have different views on what is freezable?