Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Trac #31196: code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mjungmath committed Jan 7, 2021
1 parent 9d686f2 commit cc8f76b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/sage/structure/mutability.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Mutability -- Pyrex Implementation
cdef class Mutability:
cdef public bint _is_immutable
cdef _require_mutable_cdef(self)
cpdef bint is_mutable(self)
cpdef bint is_immutable(self)
23 changes: 13 additions & 10 deletions src/sage/structure/mutability.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ cdef class Mutability:
"""
self._is_immutable = 1

def is_immutable(self):
cpdef bint is_immutable(self):
"""
Return ``True`` if this object is immutable (cannot be changed)
and ``False`` if it is not.
Expand All @@ -66,7 +66,7 @@ cdef class Mutability:
"""
self._is_immutable

def is_mutable(self):
cpdef bint is_mutable(self):
return not self._is_immutable

##########################################################################
Expand Down Expand Up @@ -109,12 +109,15 @@ def require_mutable(f):
AUTHORS:
- Simon King <simon.king@uni-jena.de>
- Simon King <simon.king@uni-jena.de>: initial version
- Michael Jung <m.jung@vu.nl>: allowed ``_is_mutable`` attribute and
edited error message
"""
@sage_wraps(f)
def new_f(self, *args,**kwds):
if getattr(self, '_is_immutable', False):
raise ValueError("%s instance is immutable, %s must not be called" % (type(self), repr(f)))
def new_f(self, *args, **kwds):
if getattr(self, '_is_immutable', False) or not getattr(self, '_is_mutable', False):
raise ValueError("object is immutable; please use a mutable copy instead.")
return f(self, *args, **kwds)
return new_f

Expand Down Expand Up @@ -159,8 +162,8 @@ def require_immutable(f):
- Simon King <simon.king@uni-jena.de>
"""
@sage_wraps(f)
def new_f(self, *args,**kwds):
if not getattr(self,'_is_immutable',False):
raise ValueError("%s instance is mutable, %s must not be called" % (type(self), repr(f)))
return f(self, *args,**kwds)
def new_f(self, *args, **kwds):
if not getattr(self, '_is_immutable', False) or getattr(self, '_is_mutable', False):
raise ValueError("object is mutable; please make it immutable first.")
return f(self, *args, **kwds)
return new_f

0 comments on commit cc8f76b

Please sign in to comment.