rate.py: python3 syntax errors regarding handling of `next` with generators
Description of problem
I'm running into an issue with NDBins.randcoord()
under python3 under some conditions, getting the following
type of error:
AttributeError: 'generator' object has no attribute 'next'
Specifically, the issue is in this line: https://git.ligo.org/lscsoft/lalsuite/-/blob/ca9fba78af324499ea13dc133f3d50719954bece/lal/python/lal/rate.py#L1280
I would give a more thorough traceback, but wasn't able to dig it up. Hopefully the problem is clear, even though I'm not able to trigger it in a minimal case.
As an aside, this same handling of next
is also scattered in a few doctests throughout rate.py
, and can be invoked via pytest and may be worth tackling as part of this. Example:
/cvmfs/oasis.opensciencegrid.org/ligo/deploy/sw/conda/envs/igwn-py37-20200427/lib/python3.7/site-packages/lal/rate.py:2188: DocTestFailure
_________________________________________________________________________________ [doctest] lal.rate.Bins.randcoord __________________________________________________________________________________
205 adjusted to reflect this.
206
207 Example:
208
209 >>> import math
210 >>> # natural log of 1/10
211 >>> print("%.15g" % math.log(1./10))
212 -2.30258509299405
213 >>> # linear bins spanning [0, 10]
214 >>> x = LinearBins(0, 10, 5).randcoord().next
UNEXPECTED EXCEPTION: AttributeError("'generator' object has no attribute 'next'")
Traceback (most recent call last):
File "/cvmfs/oasis.opensciencegrid.org/ligo/sw/conda/envs/igwn-py37/lib/python3.7/doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest lal.rate.Bins.randcoord[2]>", line 1, in <module>
AttributeError: 'generator' object has no attribute 'next'
Anyways, I'm not sure how this should be best handled since I'm not sure whether python2 has officially been dropped for lalsuite at this point. If python3 only, it would be simple to switch to the new notation:
-
gen.next
->gen.__next__
-
gen.next()
->next(gen)
This would break python2 support though, so there may need more care. Either an alias or the usage of six
.
Expected behavior
Consistent behavior for python2 and python3, or just python3 if we're officially dropping support for python2 at this point.
Context/environment
System information
Python: 3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 23:03:20)
Operating system: Scientific Linux 7.8 Nitrogen
Hostname: ldas-pcdev6.ligo.caltech.edu
Conda info
$ conda info
active environment : igwn-py37
active env location : /cvmfs/oasis.opensciencegrid.org/ligo/sw/conda/envs/igwn-py37
shell level : 1
user config file : /home/patrick.godwin/.condarc
populated config files : /cvmfs/oasis.opensciencegrid.org/ligo/sw/conda/.condarc
conda version : 4.8.3
conda-build version : 3.18.11
python version : 3.7.6.final.0
virtual packages : __glibc=2.17
base environment : /cvmfs/oasis.opensciencegrid.org/ligo/sw/conda (read only)
channel URLs : https://conda.anaconda.org/conda-forge/linux-64
https://conda.anaconda.org/conda-forge/noarch
https://repo.anaconda.com/pkgs/main/linux-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/linux-64
https://repo.anaconda.com/pkgs/r/noarch
https://conda.anaconda.org/igwn/linux-64
https://conda.anaconda.org/igwn/noarch
package cache : /cvmfs/oasis.opensciencegrid.org/ligo/sw/conda/pkgs
/home/patrick.godwin/.conda/pkgs
envs directories : /home/patrick.godwin/.conda/envs
/cvmfs/oasis.opensciencegrid.org/ligo/sw/conda/envs
platform : linux-64
user-agent : conda/4.8.3 requests/2.22.0 CPython/3.7.6 Linux/3.10.0-1127.el7.x86_64 scientific/7.8 glibc/2.17
UID:GID : 44314:44314
netrc file : /home/patrick.godwin/.netrc
offline mode : False
Conda list
$ conda list
lalapps_version
$ lalapps_version --verbose