Issues

Issue #5 resolved

_PairContext causes deprecation warning in python2.6

John Porter
created an issue

_PairContext derives from tuple which is immutable. I think the class should use:

 def __new__ (self, (e1, e2)):
        return super(_PairContext, self).__new__(self, (e1,e2))

to create the tuple

Comments (3)

  1. Jan-Philip Gehrcke repo owner

    This was obviously just swallowed in the automatic tests. Can confirm:

    Python 2.6.5 (r265:79063, Oct  1 2012, 22:04:36)
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import gipc
    >>> r, w = gipc.pipe(duplex=True)
    gipc/gipc.py:691: DeprecationWarning: object.__init__() takes no parameters
      super(_PairContext, self).__init__((e1, e2))
    

    It's not a question of Python version, it's a question of whether deprecation warnings are enabled or not:

    $ python -Wd
    Python 2.7.3 (default, Aug 28 2012, 14:23:39)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import gipc
    >>> r, w = gipc.pipe()
    /projects/bioinfp_apps/Python-2.7.3/lib/python2.7/site-packages/gipc/gipc.py:691: DeprecationWarning: object.__init__() takes no parameters
      super(_PairContext, self).__init__((e1, e2))
    

    We're hitting a design decision from several years ago which is now enforced in Python 3.3: http://bugs.python.org/issue1683368

    Inheriting from an immutable type and initializing its content / value via __init__ should not work in principle, which is why the deprecation warning is emitted. The idea is pretty simple: in the moment the object is created (which happens earlier than __init__ is called), the object already is immutable, so why should __init__ be able to re-initialize it? __init__ simply is too late for setting an initial value. That's why the use of __new__ is preferred. I'm pretty sure my initial implementation was even based on __new__, but when I realized that there is a simpler way I took that one without realizing the violation of design principles of immutable types (and my standard interpreter does not emit deprecation warnings...).

  2. Log in to comment