Skip to content

Commit

Permalink
make custom name attribute private to SageObject
Browse files Browse the repository at this point in the history
  • Loading branch information
infmagic2047 committed Aug 28, 2023
1 parent d819017 commit fd38365
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ http://docs.python.org/library/ for a complete list. ::
sage: el
bla
sage: el.__dict__
{'_custom_name': 'bla', 'value': 42}
{'_SageObject__custom_name': 'bla', 'value': 42}

Lots of Sage objects are not Python objects but compiled Cython
objects. Python sees them as builtin objects and you do not have
Expand Down
2 changes: 1 addition & 1 deletion src/sage/groups/perm_gps/permgroup_element.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cdef class PermutationGroupElement(MultiplicativeGroupElement):
cpdef PermutationGroupElement _generate_new_GAP(self, old)
cpdef _gap_list(self)
cpdef domain(self)
cdef public _custom_name
cdef public _SageObject__custom_name
cpdef list _act_on_list_on_position(self, list x)
cpdef ClonableIntArray _act_on_array_on_position(self, ClonableIntArray x)
cpdef ETuple _act_on_etuple_on_position(self, ETuple x)
7 changes: 2 additions & 5 deletions src/sage/interfaces/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,11 +1169,8 @@ def __repr__(self):
s = cr
else:
s = self._repr_()
if self._name in s:
try:
s = s.replace(self._name, getattr(self, '_custom_name'))
except AttributeError:
pass
if self._name in s and self.get_custom_name() is not None:
s = s.replace(self._name, self.get_custom_name())
if cr:
self._cached_repr = s
return s
Expand Down
4 changes: 2 additions & 2 deletions src/sage/interfaces/singular.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,8 +1400,8 @@ def _repr_(self):
"""
s = super(SingularElement, self)._repr_()
if self._name in s:
if (not hasattr(self, "_custom_name")) and self.type() == 'matrix':
s = self.parent().eval('pmat(%s,20)'%(self.name()))
if self.get_custom_name() is None and self.type() == 'matrix':
s = self.parent().eval('pmat(%s,20)' % (self.name()))
return s

def __copy__(self):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/libs/gap/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ cdef class GapElement(RingElement):
...
AttributeError: 'some_name' does not define a GAP function
"""
if name in ('__dict__', '_getAttributeNames', '_custom_name', 'keys'):
if name in ('__dict__', '_getAttributeNames', '_SageObject__custom_name', 'keys'):
raise AttributeError('Python special name, not a GAP function.')
try:
proxy = make_GapElement_MethodProxy\
Expand Down
6 changes: 3 additions & 3 deletions src/sage/matroids/basis_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
"""
N = BasisMatroid(M=self)
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo=None):
Expand All @@ -1201,7 +1201,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
if memo is None:
memo = {}
N = BasisMatroid(M=self)
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __reduce__(self):
Expand Down Expand Up @@ -1230,7 +1230,7 @@ cdef class BasisMatroid(BasisExchangeMatroid):
"""
import sage.matroids.unpickling
BB = bitset_pickle(self._bb)
data = (self._E, self._matroid_rank, getattr(self, '_custom_name'), BB)
data = (self._E, self._matroid_rank, self.get_custom_name(), BB)
version = 0
return sage.matroids.unpickling.unpickle_basis_matroid, (version, data)

Expand Down
8 changes: 3 additions & 5 deletions src/sage/matroids/circuit_closures_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,7 @@ cdef class CircuitClosuresMatroid(Matroid):
N._groundset = self._groundset
N._circuit_closures = self._circuit_closures
N._matroid_rank = self._matroid_rank
if getattr(self, '_custom_name') is not None: # because of name wrangling, this is not caught by the default copy
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo=None):
Expand All @@ -539,8 +538,7 @@ cdef class CircuitClosuresMatroid(Matroid):
from copy import deepcopy
# Since matroids are immutable, N cannot reference itself in correct code, so no need to worry about the recursion.
N = CircuitClosuresMatroid(groundset=deepcopy(self._groundset, memo), circuit_closures=deepcopy(self._circuit_closures, memo))
if getattr(self, '_custom_name') is not None: # because of name wrangling, this is not caught by the default deepcopy
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down Expand Up @@ -570,7 +568,7 @@ cdef class CircuitClosuresMatroid(Matroid):
4: {{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}}}
"""
import sage.matroids.unpickling
data = (self._groundset, self._circuit_closures, getattr(self, '_custom_name'))
data = (self._groundset, self._circuit_closures, self.get_custom_name())
version = 0
return sage.matroids.unpickling.unpickle_circuit_closures_matroid, (version, data)

Expand Down
11 changes: 3 additions & 8 deletions src/sage/matroids/dual_matroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,7 @@ def __copy__(self):
"""
N = DualMatroid(self._matroid)
if getattr(self, '_custom_name') is not None:
# because of name wrangling, this is not caught by the default copy
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo={}):
Expand All @@ -543,10 +541,7 @@ def __deepcopy__(self, memo={}):
"""
from copy import deepcopy
N = DualMatroid(deepcopy(self._matroid, memo))
if getattr(self, '_custom_name') is not None:
# because of name wrangling, this is not caught by the
# default deepcopy
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down Expand Up @@ -575,6 +570,6 @@ def __reduce__(self):
4: {{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}}}'
"""
import sage.matroids.unpickling
data = (self._matroid, getattr(self, '_custom_name'))
data = (self._matroid, self.get_custom_name())
version = 0
return sage.matroids.unpickling.unpickle_dual_matroid, (version, data)
8 changes: 3 additions & 5 deletions src/sage/matroids/graphic_matroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ def __copy__(self):
False
"""
N = GraphicMatroid(self._G)
if getattr(self, '_custom_name') is not None: # because of name wrangling, this is not caught by the default copy
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo={}):
Expand All @@ -502,8 +501,7 @@ def __deepcopy__(self, memo={}):
"""
# The only real difference between this and __copy__() is the memo
N = GraphicMatroid(deepcopy(self._G, memo))
if getattr(self, '_custom_name') is not None: # because of name wrangling, this is not caught by the default deepcopy
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand All @@ -519,7 +517,7 @@ def __reduce__(self):
Graphic matroid of rank 9 on 15 elements
"""
from .unpickling import unpickle_graphic_matroid
data = (self._G, getattr(self, '_custom_name'))
data = (self._G, self.get_custom_name())
version = 0
return unpickle_graphic_matroid, (version, data)

Expand Down
30 changes: 15 additions & 15 deletions src/sage/matroids/linear_matroid.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2909,7 +2909,7 @@ cdef class LinearMatroid(BasisExchangeMatroid):
else:
rows, cols = self._current_rows_cols()
N = LinearMatroid(groundset=rows + cols, reduced_matrix=self._A)
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo):
Expand All @@ -2930,7 +2930,7 @@ cdef class LinearMatroid(BasisExchangeMatroid):
else:
rows, cols = self._current_rows_cols()
N = LinearMatroid(groundset=deepcopy(rows + cols, memo), reduced_matrix=deepcopy(self._A, memo))
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down Expand Up @@ -2979,7 +2979,7 @@ cdef class LinearMatroid(BasisExchangeMatroid):
rows, cols = self._current_rows_cols()
gs = rows + cols
reduced = True
data = (A, gs, reduced, getattr(self, '_custom_name'))
data = (A, gs, reduced, self.get_custom_name())
return sage.matroids.unpickling.unpickle_linear_matroid, (version, data)

# Binary matroid
Expand Down Expand Up @@ -3964,7 +3964,7 @@ cdef class BinaryMatroid(LinearMatroid):
for e in self.basis():
basis[self._prow[self._idx[e]]] = e
N = BinaryMatroid(groundset=self._E, matrix=self._A, basis=basis)
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo):
Expand All @@ -3989,7 +3989,7 @@ cdef class BinaryMatroid(LinearMatroid):
for e in self.basis():
basis[self._prow[self._idx[e]]] = e
N = BinaryMatroid(groundset=deepcopy(self._E, memo), matrix=deepcopy(self._A, memo), basis=deepcopy(basis, memo))
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down Expand Up @@ -4047,7 +4047,7 @@ cdef class BinaryMatroid(LinearMatroid):
A = self._A
# current basis ordered so matrix cols form identity matrix:
basis = self._current_rows_cols()[0]
data = (A, gs, basis, getattr(self, '_custom_name'))
data = (A, gs, basis, self.get_custom_name())
return sage.matroids.unpickling.unpickle_binary_matroid, (version, data)

cdef class TernaryMatroid(LinearMatroid):
Expand Down Expand Up @@ -4858,7 +4858,7 @@ cdef class TernaryMatroid(LinearMatroid):
for e in self.basis():
basis[self._prow[self._idx[e]]] = e
N = TernaryMatroid(groundset=self._E, matrix=self._A, basis=basis)
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo):
Expand All @@ -4883,7 +4883,7 @@ cdef class TernaryMatroid(LinearMatroid):
for e in self.basis():
basis[self._prow[self._idx[e]]] = e
N = TernaryMatroid(groundset=deepcopy(self._E, memo), matrix=deepcopy(self._A, memo), basis=deepcopy(basis, memo))
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down Expand Up @@ -4945,7 +4945,7 @@ cdef class TernaryMatroid(LinearMatroid):
A = self._A
# current basis ordered so matrix cols form identity matrix:
basis = self._current_rows_cols()[0]
data = (A, gs, basis, getattr(self, '_custom_name'))
data = (A, gs, basis, self.get_custom_name())
return sage.matroids.unpickling.unpickle_ternary_matroid, (version, data)

# Quaternary Matroids
Expand Down Expand Up @@ -5589,7 +5589,7 @@ cdef class QuaternaryMatroid(LinearMatroid):
for e in self.basis():
basis[self._prow[self._idx[e]]] = e
N = QuaternaryMatroid(groundset=self._E, matrix=self._A, basis=basis)
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo):
Expand All @@ -5614,7 +5614,7 @@ cdef class QuaternaryMatroid(LinearMatroid):
for e in self.basis():
basis[self._prow[self._idx[e]]] = e
N = QuaternaryMatroid(groundset=deepcopy(self._E, memo), matrix=deepcopy(self._A, memo), basis=deepcopy(basis, memo))
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down Expand Up @@ -5672,7 +5672,7 @@ cdef class QuaternaryMatroid(LinearMatroid):
A = self._A
# current basis ordered so matrix cols form identity matrix:
basis = self._current_rows_cols()[0]
data = (A, gs, basis, getattr(self, '_custom_name'))
data = (A, gs, basis, self.get_custom_name())
return sage.matroids.unpickling.unpickle_quaternary_matroid, (version, data)

# Regular Matroids
Expand Down Expand Up @@ -6524,7 +6524,7 @@ cdef class RegularMatroid(LinearMatroid):
else:
rows, cols = self._current_rows_cols()
N = RegularMatroid(groundset=rows + cols, reduced_matrix=self._A)
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo):
Expand All @@ -6544,7 +6544,7 @@ cdef class RegularMatroid(LinearMatroid):
else:
rows, cols = self._current_rows_cols()
N = RegularMatroid(groundset=deepcopy(rows + cols, memo), reduced_matrix=deepcopy(self._A, memo))
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down Expand Up @@ -6593,5 +6593,5 @@ cdef class RegularMatroid(LinearMatroid):
rows, cols = self._current_rows_cols()
gs = rows + cols
reduced = True
data = (A, gs, reduced, getattr(self, '_custom_name'))
data = (A, gs, reduced, self.get_custom_name())
return sage.matroids.unpickling.unpickle_regular_matroid, (version, data)
2 changes: 1 addition & 1 deletion src/sage/matroids/matroid.pxd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sage.structure.sage_object cimport SageObject

cdef class Matroid(SageObject):
cdef public _custom_name
cdef public _SageObject__custom_name
cdef public _cached_info
cdef int _stored_full_rank
cdef int _stored_size
Expand Down
8 changes: 3 additions & 5 deletions src/sage/matroids/minor_matroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,7 @@ def __copy__(self):
True
"""
N = MinorMatroid(self._matroid, self._contractions, self._deletions)
if getattr(self, '_custom_name') is not None: # because of name wrangling, this is not caught by the default copy
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo={}):
Expand All @@ -512,8 +511,7 @@ def __deepcopy__(self, memo={}):
from copy import deepcopy
# Since matroids are immutable, N cannot reference itself in correct code, so no need to worry about the recursion.
N = MinorMatroid(deepcopy(self._matroid, memo), deepcopy(self._contractions, memo), deepcopy(self._deletions, memo))
if getattr(self, '_custom_name') is not None: # because of name wrangling, this is not caught by the default deepcopy
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand All @@ -534,6 +532,6 @@ def __reduce__(self):
4: {{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}}}
"""
import sage.matroids.unpickling
data = (self._matroid, self._contractions, self._deletions, getattr(self, '_custom_name'))
data = (self._matroid, self._contractions, self._deletions, self.get_custom_name())
version = 0
return sage.matroids.unpickling.unpickle_minor_matroid, (version, data)
7 changes: 2 additions & 5 deletions src/sage/matroids/rank_matroid.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,7 @@ def __copy__(self):
N = RankMatroid(groundset=[], rank_function=None)
N._groundset = self._groundset
N._rank_function = self._rank_function
if getattr(self, '_custom_name') is not None:
# because of name wrangling, this is not caught by the default copy
N.rename(getattr(self, '_custom_name'))
N.rename(self.get_custom_name())
return N

def __deepcopy__(self, memo={}):
Expand All @@ -293,8 +291,7 @@ def __deepcopy__(self, memo={}):
from copy import deepcopy
# Since matroids are immutable, N cannot reference itself in correct code, so no need to worry about the recursion.
N = RankMatroid(groundset=deepcopy(self._groundset), rank_function=deepcopy(self._rank_function))
if getattr(self, '_custom_name') is not None: # because of name wrangling, this is not caught by the default deepcopy
N.rename(deepcopy(getattr(self, '_custom_name'), memo))
N.rename(deepcopy(self.get_custom_name(), memo))
return N

def __reduce__(self):
Expand Down
5 changes: 1 addition & 4 deletions src/sage/quivers/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,10 +1297,7 @@ def copy(self):
sage: v.get_element(1)
(1, 0)
"""
if hasattr(self, '_custom_name'):
name = self._custom_name
else:
name = None
name = self.get_custom_name()
return self.parent()(self._elems.copy(), name)

####################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,8 @@ def _latex_(self):
\phi
sage: phi.reset_name()
"""
if hasattr(self, '_custom_name'):
return latex_variable_name(getattr(self, '_custom_name'))
if self.get_custom_name() is not None:
return latex_variable_name(self.get_custom_name())
else:
return f'\\phi: {latex(self._function_ring.gen())} \\mapsto ' \
f'{latex(self._gen)}'
Expand Down
Loading

0 comments on commit fd38365

Please sign in to comment.