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

Commit

Permalink
py3: fix a few doctests in this module for Python 3
Browse files Browse the repository at this point in the history
Many of these cases have unavoidably different output between Python 2 and Python 3,
due to the use of old-style classes in the tests on Python 2 (which I have kept, since
this is one case where it is very relevant that old-style classes are tested properly).

The tests do appear to work correctly with new-style classes on Python 3, but with
inevitably different results
  • Loading branch information
embray committed Jun 28, 2018
1 parent c4a9a45 commit fe444ef
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/sage/structure/dynamic_class.py
Expand Up @@ -199,13 +199,17 @@ def dynamic_class(name, bases, cls=None, reduction=None, doccls=None,
'__main__'
sage: Foo.__bases__
(<... 'object'>,)
sage: FooBar.__bases__
(<... 'object'>, <class __main__.Bar at ...>)
(<type 'object'>,)
sage: FooBar.__bases__ # py2
(<type 'object'>, <class __main__.Bar at ...>)
sage: FooBar.__bases__ # py3
(<class '__main__.Bar'>,)
sage: Foo.mro()
[<class '__main__.Foo'>, <... 'object'>]
sage: FooBar.mro()
[<class '__main__.FooBar'>, <... 'object'>, <class __main__.Bar at ...>]
[<class '__main__.Foo'>, <type 'object'>]
sage: FooBar.mro() # py2
[<class '__main__.FooBar'>, <type 'object'>, <class __main__.Bar at ...>]
sage: FooBar.mro() # py3
[<class '__main__.FooBar'>, <class '__main__.Bar'>, <class 'object'>]
If all the base classes are extension types, the dynamic class is
also considered to be an extension type (see :trac:`23435`)::
Expand All @@ -221,8 +225,10 @@ def dynamic_class(name, bases, cls=None, reduction=None, doccls=None,
unpickling, the class will be reconstructed by recalling
dynamic_class with the same arguments::
sage: type(FooBar).__reduce__(FooBar)
sage: type(FooBar).__reduce__(FooBar) # py2
(<function dynamic_class at ...>, ('FooBar', (<class __main__.Bar at ...>,), <class '__main__.Foo'>, None, None))
sage: type(FooBar).__reduce__(FooBar) # py3
(<function dynamic_class at ...>, ('FooBar', (<class '__main__.Bar'>,), <class '__main__.Foo'>, None, None))
Technically, this is achieved by using a metaclass, since the
Python pickling protocol for classes is to pickle by name::
Expand All @@ -235,7 +241,7 @@ def dynamic_class(name, bases, cls=None, reduction=None, doccls=None,
sage: BarFoo = dynamic_class("BarFoo", (Foo,), Bar, reduction = (str, (3,)))
sage: type(BarFoo).__reduce__(BarFoo)
(<... 'str'>, (3,))
(<type 'str'>, (3,))
sage: loads(dumps(BarFoo))
'3'
Expand Down Expand Up @@ -277,7 +283,7 @@ def dynamic_class(name, bases, cls=None, reduction=None, doccls=None,
first class::
sage: dynamic_class("BarFoo", (Foo,), Bar, reduction = (str, (2,)), cache="ignore_reduction")._reduction
(<... 'str'>, (3,))
(<type 'str'>, (3,))
.. WARNING::
Expand All @@ -296,8 +302,10 @@ def dynamic_class(name, bases, cls=None, reduction=None, doccls=None,
sage: x.__dict__ # Breaks without the __dict__ deletion in dynamic_class_internal
{'_x': 3}
sage: type(FooBar).__reduce__(FooBar)
sage: type(FooBar).__reduce__(FooBar) # py2
(<function dynamic_class at ...>, ('FooBar', (<class __main__.Bar at ...>,), <class '__main__.Foo'>, None, None))
sage: type(FooBar).__reduce__(FooBar) # py3
(<function dynamic_class at ...>, ('FooBar', (<class '__main__.Bar'>,), <class '__main__.Foo'>, None, None))
sage: from six.moves import cPickle
sage: cPickle.loads(cPickle.dumps(FooBar)) == FooBar
True
Expand Down Expand Up @@ -486,9 +494,12 @@ def __reduce__(self):
sage: class Foo: pass
sage: class DocClass: pass
sage: C = sage.structure.dynamic_class.dynamic_class_internal("bla", (object,), Foo, doccls = DocClass)
sage: type(C).__reduce__(C)
sage: type(C).__reduce__(C) # py2
(<function dynamic_class at ...>,
('bla', (<type 'object'>,), <class __main__.Foo at ...>, None, <class __main__.DocClass at ...>))
sage: type(C).__reduce__(C) # py3
(<function dynamic_class at ...>,
('bla', (<... 'object'>,), <class __main__.Foo at ...>, None, <class __main__.DocClass at ...>))
('bla', (<type 'object'>,), <class '__main__.Foo'>, None, <class '__main__.DocClass'>))
sage: C = sage.structure.dynamic_class.dynamic_class_internal("bla", (object,), Foo, doccls = DocClass, reduction = "blah")
sage: type(C).__reduce__(C)
'blah'
Expand Down

0 comments on commit fe444ef

Please sign in to comment.