interaction between locals, builtins and except clause
Porting to Py3K, I modified a function like the followin, using a trick for it working in Py2.x . def __iter__(self): if self == _mpi.INFO_NULL: return try: range = xrange except: pass nkeys = _mpi.info_get_nkeys(self) for nthkey in range(nkeys): yield _mpi.info_get_nthkey(self, nthkey) However, I've got in my unittests (running with py3k) ERROR: testPyMethods (__main__.TestInfo) ---------------------------------------------------------------------- Traceback (most recent call last): File "tests/unittest/test_info.py", line 123, in testPyMethods for key in INFO: File "/u/dalcinl/lib/python/mpi4py/MPI.py", line 937, in __iter__ for nthkey in range(nkeys): UnboundLocalError: local variable 'range' referenced before assignment I am not completelly sure if this is expected (it is, regarding implementation, but perhaps not regarding Python as a language), so I post this for your consideration. -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594
Lisandro Dalcin schrieb:
Porting to Py3K, I modified a function like the followin, using a trick for it working in Py2.x .
def __iter__(self): if self == _mpi.INFO_NULL: return try: range = xrange except: pass nkeys = _mpi.info_get_nkeys(self) for nthkey in range(nkeys): yield _mpi.info_get_nthkey(self, nthkey)
However, I've got in my unittests (running with py3k)
ERROR: testPyMethods (__main__.TestInfo) ---------------------------------------------------------------------- Traceback (most recent call last): File "tests/unittest/test_info.py", line 123, in testPyMethods for key in INFO: File "/u/dalcinl/lib/python/mpi4py/MPI.py", line 937, in __iter__ for nthkey in range(nkeys): UnboundLocalError: local variable 'range' referenced before assignment
I am not completelly sure if this is expected (it is, regarding implementation, but perhaps not regarding Python as a language), so I post this for your consideration.
Yes, this is expected. By an assignment to range anywhere in a function scope, the name is marked as a local and won't ever be looked up in the global namespace. I'd move the range = xrange part at the module top, or just bring the 2.x version in a state where one run of 2to3 produces a working 3.0 version. HTH, Georg -- Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are right out.
On 7/26/07, Lisandro Dalcin
Porting to Py3K, I modified a function like the followin, using a trick for it working in Py2.x .
def __iter__(self): if self == _mpi.INFO_NULL: return try: range = xrange except: pass nkeys = _mpi.info_get_nkeys(self) for nthkey in range(nkeys): yield _mpi.info_get_nthkey(self, nthkey)
However, I've got in my unittests (running with py3k)
ERROR: testPyMethods (__main__.TestInfo) ---------------------------------------------------------------------- Traceback (most recent call last): File "tests/unittest/test_info.py", line 123, in testPyMethods for key in INFO: File "/u/dalcinl/lib/python/mpi4py/MPI.py", line 937, in __iter__ for nthkey in range(nkeys): UnboundLocalError: local variable 'range' referenced before assignment
I am not completelly sure if this is expected (it is, regarding implementation, but perhaps not regarding Python as a language), so I post this for your consideration.
Python thinnks range is local, because you referenced it, even if an error ocurred. Use 'global range' at the top of the file. But, as I understand, you are trying to target both Python 2.x and 3 with the same code, using tricks like this one. I think that, even if you succeed, the resulting code will be quite unmaintainable. -- EduardoOPadoan (eopadoan->altavix::com) Bookmarks: http://del.icio.us/edcrypt
participants (3)
-
Eduardo "EdCrypt" O. Padoan
-
Georg Brandl
-
Lisandro Dalcin