Opened 6 years ago

Closed 22 months ago

#16076 closed enhancement (wontfix)

Python 3 preparation: Py3 has no more the special object-function "__nonzero__"

Reported by: wluebbe Owned by:
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: python3 Keywords: python3
Cc: embray Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by jdemeyer)

The tool 2to3 renames __nonzero__ to __bool__. But the code has to depend on the Python version!

Note that this does not affect Cython extension types because Cython supports __nonzero__ and __bool__ independently of the Python version.

There are 25 affected modules.

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

Change History (23)

comment:1 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:2 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:3 Changed 3 years ago by jdemeyer

  • Component changed from distribution to python3
  • Description modified (diff)

comment:4 Changed 3 years ago by chapoton

  • Milestone changed from sage-6.4 to sage-7.5

one step done (for the rings folder) in #21887

comment:5 Changed 3 years ago by chapoton

another step in #21898

comment:6 Changed 3 years ago by jdemeyer

  • Description modified (diff)

comment:7 Changed 3 years ago by jdemeyer

  • Description modified (diff)

comment:8 Changed 3 years ago by chapoton

  • Milestone changed from sage-7.5 to sage-7.6

comment:9 Changed 3 years ago by chapoton

  • Branch set to u/chapoton/16076
  • Commit set to fbf00d716c92c3f15ca9aed888817d5efa2b89b9

Here is a brute-force tentative

  • search and replace __nonzero__ by __bool__
  • remove the former lines that have become __bool__ = __bool__

Let us see what happens..


New commits:

5c0ed27substitute __nonzero__ by __bool__
fbf00d7cleanup : removal of former alias for __nonzero__

comment:10 Changed 3 years ago by chapoton

ok, this was too brutal.

comment:11 Changed 3 years ago by git

  • Commit changed from fbf00d716c92c3f15ca9aed888817d5efa2b89b9 to d3182db09b24647c2bde5ae5cf459afdbd58828d

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

d3182dbtrac 16076 trying to get rid of the last nonzero

comment:12 Changed 3 years ago by chapoton

ok, still not subtle enough..

comment:13 follow-up: Changed 3 years ago by tscrim

The way I've seen for this to be 2/3 compatible is to have __bool__ be an alias for __nonzero__ (or vice versa). The other way we could hack this is have in SageObject, as almost everything has this as a base class, we implement:

def __bool__(self):
    return self.__nonzero__()

comment:14 Changed 3 years ago by chapoton

Hello Travis,

if you feel like giving it a try, please do. There is an abstract method __nonzero__ somewhere, which is the last place where __nonzero__ is not an alias (in python files).

Last edited 3 years ago by chapoton (previous) (diff)

comment:15 Changed 3 years ago by tscrim

I think we should keep things as __nonzero__ for right now (up to having an explicit alias) for right now since we are in Python2 and to avoid the extra redirection. Although I guess to test how much this would cover things for Python3, it would have to be with a Python3 build.

comment:16 Changed 3 years ago by chapoton

  • Branch u/chapoton/16076 deleted
  • Commit d3182db09b24647c2bde5ae5cf459afdbd58828d deleted
  • Milestone changed from sage-7.6 to sage-8.0

comment:17 Changed 23 months ago by embray

  • Milestone changed from sage-8.0 to sage-pending

comment:18 Changed 23 months ago by chapoton

why sage-pending ?

comment:19 Changed 23 months ago by embray

I might be misusing the milestone but this seems to be a very general task ticket not tied to a specific release milestone...?

comment:20 follow-up: Changed 23 months ago by chapoton

  • Cc embray added
  • Milestone changed from sage-pending to sage-duplicate/invalid/wontfix
  • Status changed from new to needs_review

is this fixed by #24294?

comment:21 in reply to: ↑ 13 Changed 23 months ago by jdemeyer

  • Status changed from needs_review to positive_review

Replying to tscrim:

The way I've seen for this to be 2/3 compatible is to have __bool__ be an alias for __nonzero__ (or vice versa). The other way we could hack this is have in SageObject, as almost everything has this as a base class, we implement:

def __bool__(self):
    return self.__nonzero__()

I know that this reply is pointless is now, but I guess that something like that would have worked :-)

We are doing something similar for division in CategoryObject:

from __future__ import division

cdef class CategoryObject(SageObject):
    def __div__(self, other):
        return self / other

comment:22 in reply to: ↑ 20 Changed 23 months ago by embray

Replying to chapoton:

is this fixed by #24294?

I don't think so by itself--I believe I've found some other examples affected by this (in non-Cython modules) but I could be mistaken.

comment:23 Changed 22 months ago by embray

  • Resolution set to wontfix
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.