gh-106078: Convert _decimal types to heap types (#106079)

- Establish global state struct
- Convert static types to heap types and add them to global state:
    * PyDecContextManager_Type
    * PyDecContext_Type
    * PyDecSignalDictMixin_Type
    * PyDec_Type
- Add to global state:
    * PyDecSignalDict_Type
    * DecimalTuple

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Erlend E. Aasland <erlend@python.org>
This commit is contained in:
Charlie Zhao 2023-06-29 18:27:20 +08:00 committed by GitHub
parent 0e24499129
commit fb0d9b9ac1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 433 additions and 390 deletions

View file

@ -34,7 +34,8 @@ import numbers
import locale
from test.support import (is_resource_enabled,
requires_IEEE_754, requires_docstrings,
requires_legacy_unicode_capi, check_sanitizer)
requires_legacy_unicode_capi, check_sanitizer,
check_disallow_instantiation)
from test.support import (TestFailed,
run_with_locale, cpython_only,
darwin_malloc_err_warning, is_emscripten)
@ -5681,6 +5682,24 @@ class CWhitebox(unittest.TestCase):
self.assertEqual(Decimal(4) / 2, 2)
self.assertEqual(Decimal(400) ** -1, Decimal('0.0025'))
def test_c_immutable_types(self):
SignalDict = type(C.Context().flags)
SignalDictMixin = SignalDict.__bases__[0]
ContextManager = type(C.localcontext())
types = (
SignalDictMixin,
ContextManager,
C.Decimal,
C.Context,
)
for tp in types:
with self.subTest(tp=tp):
with self.assertRaisesRegex(TypeError, "immutable"):
tp.foo = 1
def test_c_disallow_instantiation(self):
ContextManager = type(C.localcontext())
check_disallow_instantiation(self, ContextManager)
@requires_docstrings
@requires_cdecimal