From 3e036b7c84f5ae77bf529a801d9db850b570db73 Mon Sep 17 00:00:00 2001 From: Ingmar Schoegl Date: Sun, 31 Mar 2024 10:56:16 -0500 Subject: [PATCH] [Python] Remove most weakproxy sentinels --- interfaces/cython/cantera/_onedim.pxd | 1 - interfaces/cython/cantera/_onedim.pyx | 10 ---------- interfaces/cython/cantera/composite.py | 12 +++++------- interfaces/cython/cantera/mixture.pxd | 1 - interfaces/cython/cantera/mixture.pyx | 6 ------ interfaces/cython/cantera/reactor.pxd | 1 - interfaces/cython/cantera/reactor.pyx | 8 -------- interfaces/cython/cantera/solutionbase.pxd | 1 - interfaces/cython/cantera/thermo.pyx | 10 ++++------ 9 files changed, 9 insertions(+), 41 deletions(-) diff --git a/interfaces/cython/cantera/_onedim.pxd b/interfaces/cython/cantera/_onedim.pxd index 5ca47b00d..ab0eb7937 100644 --- a/interfaces/cython/cantera/_onedim.pxd +++ b/interfaces/cython/cantera/_onedim.pxd @@ -153,7 +153,6 @@ cdef class Domain1D: cdef shared_ptr[CxxDomain1D] _domain cdef CxxDomain1D* domain cdef _SolutionBase gas - cdef object _weakref_proxy cdef public pybool have_user_tolerances cdef class Boundary1D(Domain1D): diff --git a/interfaces/cython/cantera/_onedim.pyx b/interfaces/cython/cantera/_onedim.pyx index 0516270d8..3721c20f5 100644 --- a/interfaces/cython/cantera/_onedim.pyx +++ b/interfaces/cython/cantera/_onedim.pyx @@ -9,23 +9,16 @@ from ._utils cimport stringify, pystr, anymap_to_py from ._utils import CanteraError from cython.operator import dereference as deref -# Need a pure-python class to store weakrefs to -class _WeakrefProxy: - pass - cdef class Domain1D: _domain_type = "none" def __cinit__(self, _SolutionBase phase not None, *args, **kwargs): self.domain = NULL def __init__(self, phase, *args, **kwargs): - self._weakref_proxy = _WeakrefProxy() if self.domain is NULL: raise TypeError("Can't instantiate abstract class Domain1D.") self.gas = phase - # Block species from being added to the phase as long as this object exists - self.gas._references[self._weakref_proxy] = True self.set_default_tolerances() property phase: @@ -424,7 +417,6 @@ cdef class ReactingSurface1D(Boundary1D): _domain_type = "reacting-surface" def __init__(self, _SolutionBase phase, name=None): - self._weakref_proxy = _WeakrefProxy() gas = None for val in phase._adjacent.values(): if val.phase_of_matter == "gas": @@ -435,8 +427,6 @@ cdef class ReactingSurface1D(Boundary1D): super().__init__(gas, name=name) self.surface = phase - # Block species from being added to the phase as long as this object exists - self.surface._references[self._weakref_proxy] = True property phase: """ diff --git a/interfaces/cython/cantera/composite.py b/interfaces/cython/cantera/composite.py index 6ef85a6ed..7aa78c1b7 100644 --- a/interfaces/cython/cantera/composite.py +++ b/interfaces/cython/cantera/composite.py @@ -142,6 +142,11 @@ class DustyGas(DustyGasTransport, Kinetics, ThermoPhase): __slots__ = () +# A pure-Python class to store weakrefs to +class _WeakrefProxy: + pass + + class Quantity: """ A class representing a specific quantity of a `Solution`. In addition to the @@ -382,11 +387,6 @@ for _attr in dir(Solution): setattr(Quantity, _attr, _prop(_attr)) -# A pure-Python class to store weakrefs to -class _WeakrefProxy: - pass - - class SolutionArray(SolutionArrayBase): """ A class providing a convenient interface for representing many thermodynamic @@ -583,8 +583,6 @@ class SolutionArray(SolutionArrayBase): def __init__(self, phase, shape=(0,), states=None, extra=None, meta={}, init=True): self._phase = phase - self._weakref_proxy = _WeakrefProxy() - phase._references[self._weakref_proxy] = True if not init: return diff --git a/interfaces/cython/cantera/mixture.pxd b/interfaces/cython/cantera/mixture.pxd index 6b2ae611e..152194742 100644 --- a/interfaces/cython/cantera/mixture.pxd +++ b/interfaces/cython/cantera/mixture.pxd @@ -52,5 +52,4 @@ cdef extern from "cantera/equil/MultiPhase.h" namespace "Cantera": cdef class Mixture: cdef CxxMultiPhase* mix cdef list _phases - cdef object _weakref_proxy cpdef int element_index(self, element) except * diff --git a/interfaces/cython/cantera/mixture.pyx b/interfaces/cython/cantera/mixture.pyx index 35b8ab977..8263ae01e 100644 --- a/interfaces/cython/cantera/mixture.pyx +++ b/interfaces/cython/cantera/mixture.pyx @@ -8,10 +8,6 @@ from .solutionbase cimport * from .thermo cimport * from ._utils cimport * -# Need a pure-python class to store weakrefs to -class _WeakrefProxy: - pass - cdef class Mixture: """ @@ -44,7 +40,6 @@ cdef class Mixture: def __cinit__(self, phases): self.mix = new CxxMultiPhase() self._phases = [] - self._weakref_proxy = _WeakrefProxy() cdef _SolutionBase phase if isinstance(phases[0], _SolutionBase): @@ -53,7 +48,6 @@ cdef class Mixture: for phase,moles in phases: # Block species from being added to the phase as long as this object exists - phase._references[self._weakref_proxy] = True self.mix.addPhase(phase.thermo, moles) self._phases.append(phase) diff --git a/interfaces/cython/cantera/reactor.pxd b/interfaces/cython/cantera/reactor.pxd index 8f24e70b7..8bb589e53 100644 --- a/interfaces/cython/cantera/reactor.pxd +++ b/interfaces/cython/cantera/reactor.pxd @@ -223,7 +223,6 @@ cdef class ReactorBase: cdef list _outlets cdef list _walls cdef list _surfaces - cdef object _weakref_proxy cdef public dict node_attr """ A dictionary containing draw attributes for the representation of the reactor as a diff --git a/interfaces/cython/cantera/reactor.pyx b/interfaces/cython/cantera/reactor.pyx index d025645de..d2f383a59 100644 --- a/interfaces/cython/cantera/reactor.pyx +++ b/interfaces/cython/cantera/reactor.pyx @@ -14,10 +14,6 @@ from .drawnetwork import * _reactor_counts = _defaultdict(int) -# Need a pure-python class to store weakrefs to -class _WeakrefProxy: - pass - cdef class ReactorBase: """ Common base class for reactors and reservoirs. @@ -41,8 +37,6 @@ cdef class ReactorBase: def __init__(self, _SolutionBase contents=None, name=None, *, volume=None, node_attr=None): - - self._weakref_proxy = _WeakrefProxy() self._inlets = [] self._outlets = [] self._walls = [] @@ -61,8 +55,6 @@ cdef class ReactorBase: properties and kinetic rates for this reactor. """ self._thermo = solution - # Block species from being added to the phase as long as this object exists - self._thermo._references[self._weakref_proxy] = True self.rbase.setSolution(solution._base) property type: diff --git a/interfaces/cython/cantera/solutionbase.pxd b/interfaces/cython/cantera/solutionbase.pxd index 95e9e28ef..bb9f1666c 100644 --- a/interfaces/cython/cantera/solutionbase.pxd +++ b/interfaces/cython/cantera/solutionbase.pxd @@ -122,4 +122,3 @@ cdef class _SolutionBase: cdef class SolutionArrayBase: cdef shared_ptr[CxxSolutionArray] _base cdef CxxSolutionArray* base - cdef public object _weakref_proxy diff --git a/interfaces/cython/cantera/thermo.pyx b/interfaces/cython/cantera/thermo.pyx index 3f9179192..cd0a6deac 100644 --- a/interfaces/cython/cantera/thermo.pyx +++ b/interfaces/cython/cantera/thermo.pyx @@ -270,10 +270,9 @@ cdef class ThermoPhase(_SolutionBase): # In composite objects, the ThermoPhase constructor needs to be called first # to prevent instantiation of stand-alone 'Kinetics' or 'Transport' objects. # The following is used as a sentinel. After initialization, the _references - # object is used to track whether the ThermoPhase is being used by a `Reactor`, - # `Domain1D`, or `Mixture` object and to prevent species from being added to the - # ThermoPhase if so, since these objects require the number of species to remain - # constant. + # object is used to track whether the ThermoPhase is being used by other + # objects that require the number of species to remain constant and do not + # have C++ implementations (e.g. Quantity objects). self._references = weakref.WeakKeyDictionary() # validate plasma phase self._enable_plasma = False @@ -583,8 +582,7 @@ cdef class ThermoPhase(_SolutionBase): """ if self._references: raise CanteraError('Cannot add species to ThermoPhase object because it' - ' is being used by another object,\nsuch as a Reactor, Domain1D (flame),' - ' SolutionArray, Quantity, or Mixture object.') + ' is being used by a Quantity object.') self.thermo.addUndefinedElements() self.thermo.addSpecies(species._species) species._phase = self