Skip to content

Commit

Permalink
Trac #15983: Python 3 preparation: Change names of some function attr…
Browse files Browse the repository at this point in the history
…ibutes

Only the modern syntax like {{{f.__doc__}}} is accepted by Python 3.
[[br]]

Changes according to {{{lib2to3/fixes/fix_funcattrs.py}}}:
{{{
f.func_x -> f.__x__
for
('func_closure' | 'func_doc' | 'func_globals' | 'func_name' |
'func_defaults' | 'func_code' | 'func_dict')
}}}

This ticket is tracked as a dependency of meta-ticket ticket:15980.

URL: http://trac.sagemath.org/15983
Reported by: wluebbe
Ticket author(s): Wilfried Luebbe, R. Andrew Ohana
Reviewer(s): Frédéric Chapoton, R. Andrew Ohana
  • Loading branch information
Release Manager authored and vbraun committed Apr 4, 2014
2 parents 8c18f21 + c03d421 commit e95496e
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/sage/combinat/combinatorial_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ def __init__(self, f, order=None, name=None):
self._f = f
self._order = order
self._name = name
if hasattr(f, "func_doc"):
self.__doc__ = f.func_doc
if hasattr(f, "func_name"):
self.__name__ = f.func_name
if hasattr(f, "__doc__"):
self.__doc__ = f.__doc__
if hasattr(f, "__name__"):
self.__name__ = f.__name__
else:
self.__name__ = "..."
if hasattr(f, "__module__"):
Expand Down
8 changes: 4 additions & 4 deletions src/sage/misc/abstract_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ def __init__(self, f, optional = False):
assert isinstance(optional, bool)
self._f = f
self._optional = optional
if hasattr(f, "func_doc"):
self.__doc__ = f.func_doc
if hasattr(f, "func_name"):
self.__name__ = f.func_name
if hasattr(f, "__doc__"):
self.__doc__ = f.__doc__
if hasattr(f, "__name__"):
self.__name__ = f.__name__
else:
self.__name__ = "..."
if hasattr(f, "__module__"):
Expand Down
19 changes: 8 additions & 11 deletions src/sage/misc/cachefunc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ cdef class CachedFunction(object):
self.f = f
if name is not None:
self.__name__ = name
elif hasattr(f, "func_name"):
self.__name__ = f.func_name
elif hasattr(f, "__name__"):
self.__name__ = f.__name__
else:
self.__name__ = f.__name__
try:
Expand Down Expand Up @@ -649,13 +649,12 @@ cdef class CachedFunction(object):
available), or a toy implementation.
"""
from sage.misc.sageinspect import _extract_embedded_position
f = self.f
if hasattr(f, "func_doc"):
doc = f.__doc__ or ''
if _extract_embedded_position(doc.splitlines()[0]) is None:
try:
sourcelines = sage_getsourcelines(f)
except IOError:
sourcelines = None
if sourcelines is not None:
from sage.env import SAGE_SRC, SAGE_LIB
filename = sage_getfile(f)
# The following is a heuristics to get
Expand All @@ -666,11 +665,9 @@ cdef class CachedFunction(object):
elif filename.startswith(SAGE_LIB):
filename = filename[len(SAGE_LIB):]
file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1])
doc = file_info+(f.func_doc or '')
else:
doc = f.func_doc
else:
doc = f.__doc__
doc = file_info+doc
except IOError:
pass
return doc

def _sage_src_(self):
Expand Down
6 changes: 3 additions & 3 deletions src/sage/misc/fpickle.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def code_ctor(*args):
EXAMPLES:
This indirectly tests this function.
sage: def foo(a,b,c=10): return a+b+c
sage: sage.misc.fpickle.reduce_code(foo.func_code)
sage: sage.misc.fpickle.reduce_code(foo.__code__)
(<built-in function code_ctor>, ...)
sage: unpickle_function(pickle_function(foo))
<function foo at ...>
Expand All @@ -22,7 +22,7 @@ def reduce_code(co):
"""
EXAMPLES:
sage: def foo(N): return N+1
sage: sage.misc.fpickle.reduce_code(foo.func_code)
sage: sage.misc.fpickle.reduce_code(foo.__code__)
(<built-in function code_ctor>, ...)
"""
if co.co_freevars or co.co_cellvars:
Expand Down Expand Up @@ -57,7 +57,7 @@ def pickle_function(func):
sage: h(10)
11
"""
return cPickle.dumps(func.func_code)
return cPickle.dumps(func.__code__)

def unpickle_function(pickled):
"""
Expand Down
6 changes: 3 additions & 3 deletions src/sage/misc/func_persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def __init__(self, f, dir='func_persist'):
self.__dir = dir
sage_makedirs(dir)
self.__doc__ = '%s%s%s'%(\
f.func_name,
inspect.formatargspec(*inspect.getargs(f.func_code)),
f.__name__,
inspect.formatargspec(*inspect.getargs(f.__code__)),
f.__doc__)

def __call__(self, *args, **kwds):
key = (tuple(args), tuple(kwds.items()))
h = hash(key)
name = '%s/%s_%s.sobj'%(self.__dir, self.__func.func_name, h)
name = '%s/%s_%s.sobj'%(self.__dir, self.__func.__name__, h)

if os.path.exists(name):
key2, val = persist.load(name)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/function_mangling.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ cdef class ArgumentFixer:
else:
self._default_tuple = tuple(defaults)

#code = f.func_code
#code = f.__code__

self.f = f
self._ndefault = len(defaults)
Expand Down
8 changes: 4 additions & 4 deletions src/sage/misc/lazy_attribute.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,12 @@ class lazy_attribute(_lazy_attribute):
'__main__'
"""
self.f = f
if hasattr(f, "func_doc"):
self.__doc__ = f.func_doc
if hasattr(f, "__doc__"):
self.__doc__ = f.__doc__
elif hasattr(f, "__doc__"): # Needed to handle Cython methods
self.__doc__ = f.__doc__
if hasattr(f, "func_name"):
self.__name__ = f.func_name
if hasattr(f, "__name__"):
self.__name__ = f.__name__
elif hasattr(f, "__name__"): # Needed to handle Cython methods
self.__name__ = f.__name__
if hasattr(f, "__module__"):
Expand Down
8 changes: 4 additions & 4 deletions src/sage/misc/method_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def __init__(self, f):
<class 'sage.misc.method_decorator.MethodDecorator'>
"""
self.f = f
if hasattr(f, "func_doc"):
self.__doc__ = f.func_doc
if hasattr(f, "__doc__"):
self.__doc__ = f.__doc__
else:
self.__doc__ = f.__doc__
if hasattr(f, "func_name"):
self.__name__ = f.func_name
if hasattr(f, "__name__"):
self.__name__ = f.__name__
self.__module__ = f.__module__

def _sage_src_(self):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ def __call__(self, obj, _=None):
if obj is None:
return self
value = self._calculate(obj)
setattr(obj, self._calculate.func_name, value)
setattr(obj, self._calculate.__name__, value)
return value

def prop(f):
Expand Down
6 changes: 3 additions & 3 deletions src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ def foo(x, a='\')"', b={not (2+1==3):'bar'}): return

# Otherwise we're (hopefully!) plain Python, so use inspect
try:
args, varargs, varkw = inspect.getargs(func_obj.func_code)
args, varargs, varkw = inspect.getargs(func_obj.__code__)
except AttributeError:
try:
args, varargs, varkw = inspect.getargs(func_obj)
Expand All @@ -1333,7 +1333,7 @@ def foo(x, a='\')"', b={not (2+1==3):'bar'}): return
return inspect.ArgSpec(*_sage_getargspec_cython(sage_getsource(obj)))
#return _sage_getargspec_from_ast(sage_getsource(obj))
try:
defaults = func_obj.func_defaults
defaults = func_obj.__defaults__
except AttributeError:
defaults = tuple([])
return inspect.ArgSpec(args, varargs, varkw, defaults)
Expand Down Expand Up @@ -1640,7 +1640,7 @@ class ParentMethods:
if inspect.ismethod(object):
object = object.__func__
if inspect.isfunction(object):
object = object.func_code
object = object.__code__
if inspect.istraceback(object):
object = object.tb_frame
if inspect.isframe(object):
Expand Down
28 changes: 12 additions & 16 deletions src/sage/sets/set_from_iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,12 @@ def _sage_doc_(self):
IMPLEMENTATION: Calls the PARI "isprime" function.
<BLANKLINE>
"""
from sage.misc.sageinspect import sage_getsourcelines, sage_getfile
from sage.misc.sageinspect import sage_getsourcelines, sage_getfile, _extract_embedded_position
f = self.f
if hasattr(f, "func_doc"):
doc = f.__doc__ or ''
if _extract_embedded_position(doc.splitlines()[0]) is None:
try:
sourcelines = sage_getsourcelines(f)
except IOError:
sourcelines = None
if sourcelines is not None:
from sage.env import SAGE_LIB, SAGE_SRC
filename = sage_getfile(f)
# The following is a heuristics to get
Expand All @@ -459,11 +457,9 @@ def _sage_doc_(self):
elif filename.startswith(SAGE_LIB):
filename = filename[len(SAGE_LIB):]
file_info = "File: %s (starting at line %d)\n"%(filename,sourcelines[1])
doc = file_info+(f.func_doc or '')
else:
doc = f.func_doc
else:
doc = f.__doc__
doc = file_info+doc
except IOError:
pass
return doc

def _sage_src_(self):
Expand Down Expand Up @@ -638,8 +634,8 @@ def __init__(self, f=None, name=None, **options):
"""
if f is not None:
self.f = f
if hasattr(f, "func_name"):
self.__name__ = f.func_name
if hasattr(f, "__name__"):
self.__name__ = f.__name__
else:
self.__name__ = f.__name__
self.__module__ = f.__module__
Expand Down Expand Up @@ -737,8 +733,8 @@ def __init__(self, inst, f, name=None, **options):
self.inst = inst
self.f = f
self.af = ArgumentFixer(self.f)
if hasattr(f, "func_name"):
self.__name__ = f.func_name
if hasattr(f, "__name__"):
self.__name__ = f.__name__
else:
self.__name__ = f.__name__
self.__module__ = f.__module__
Expand Down Expand Up @@ -910,8 +906,8 @@ def __init__(self, f=None, **options):
if f is not None:
import types
self.f = f
if hasattr(f,"func_name"):
self.__name__ = f.func_name
if hasattr(f,"__name__"):
self.__name__ = f.__name__
self.__module__ = f.__module__

else:
Expand Down
4 changes: 2 additions & 2 deletions src/sage/structure/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3116,9 +3116,9 @@ cdef class NamedBinopMethod:
self._func = func
if name is None:
if isinstance(func, types.FunctionType):
name = func.func_name
name = func.__name__
if isinstance(func, types.UnboundMethodType):
name = func.im_func.func_name
name = func.__func__.__name__
else:
name = func.__name__
self._name = name
Expand Down
2 changes: 1 addition & 1 deletion src/sage/symbolic/function.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ cdef class SymbolicFunction(Function):
for fname in sfunctions_funcs:
real_fname = '_%s_'%fname
if hasattr(self, '%s'%real_fname):
slist.append(hash(getattr(self, real_fname).func_code))
slist.append(hash(getattr(self, real_fname).__code__))
else:
slist.append(' ')
self.__hcache = hash(tuple(slist))
Expand Down

0 comments on commit e95496e

Please sign in to comment.