[Python] Remove most weakproxy sentinels

This commit is contained in:
Ingmar Schoegl 2024-03-31 10:56:16 -05:00
parent 4d3025587f
commit 3e036b7c84
9 changed files with 9 additions and 41 deletions

View File

@ -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):

View File

@ -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:
"""

View File

@ -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

View File

@ -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 *

View File

@ -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)

View File

@ -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

View File

@ -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:

View File

@ -122,4 +122,3 @@ cdef class _SolutionBase:
cdef class SolutionArrayBase:
cdef shared_ptr[CxxSolutionArray] _base
cdef CxxSolutionArray* base
cdef public object _weakref_proxy

View File

@ -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