Python Forum
Confusing output from 2to3 about what files need change
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Confusing output from 2to3 about what files need change
#1
Porting my 2.7 code to python 3.6.7

Two confusing issues:
1. 2to3 found changes on second run.

After running 2to3 on a dir so changes are made, tested, and committed, I run it again latter to check and it found new changes.

Now it wants to add 'list' to all previous changes of '.iteritems()' to 'items()'

I understand why from my research, but why did it not make the change the first time? I can reproduce this behavior every time.

e.g.

Quote:
- for k,v in self.all_devices.items():
+ for k,v in list(self.all_devices.items()):


2. output of 2to3 seems to say no changes needed but then gives list of files to be modified.

After running twice 2to3 on a dir so all needed changes are made, tested, and committed, I run it again latter to check and this output like this (note bold):

Quote:$ 2to3 *py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: No changes to DeviceMap.py
RefactoringTool: No changes to DeviceMapSnmp.py
RefactoringTool: No changes to SnmpSensor.py
RefactoringTool: No changes to SnmpSensors.py
RefactoringTool: Files that need to be modified:
RefactoringTool: DeviceMap.py
RefactoringTool: DeviceMapSnmp.py
RefactoringTool: SnmpSensor.py
RefactoringTool: SnmpSensors.py

If I run a third time with the -w option no changes are made. It seems this output is misleading, right?
Reply
#2
2to3 is supposed to transform python2 code into python3 code. If you give it python3 code as input and pretend it is python2 code, it may wrongly find new changes.

In your case, in python2, the_dict.iteritems() returns an iterator that yields the dictionary's items. The python3 code that does the same thing is the_dict.items(). If we now pretend that this is python2 code, the semantics changes because in python2, the_dict.items() returns a list of the dictionary's items. To achieve the same effect in python3, you need to convert to list with list(the_dict.items()). The difference is that if the code uses a list in the for statement, it is allowed to modify the dictionary's content during the for loop, while if the code uses an iterator, it is bad logic to modify the dictionary in the for loop.

To sum it up: use 2to3 as it is meant to be used!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output values change akbarza 3 527 Oct-18-2023, 12:30 PM
Last Post: deanhystad
  Find a specific keyword after another keyword and change the output sgtmcc 5 808 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  change directory of save of python files akbarza 3 875 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 957 Feb-15-2023, 05:34 PM
Last Post: zsousa
  merge two csv files into output.csv using Subprocess mg24 9 1,756 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Rain sensor output only on change Pete6 3 2,035 May-11-2022, 10:36 PM
Last Post: Pete6
  Confusing in [for loop] topic Sherine 11 3,495 Jul-31-2021, 02:53 PM
Last Post: deanhystad
  Increment text files output and limit contains Kaminsky 1 3,188 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  opening files and output of parsing leodavinci1990 4 2,528 Oct-12-2020, 06:52 AM
Last Post: bowlofred
  Confusing logic Blob 4 2,399 Nov-18-2019, 03:26 AM
Last Post: Blob

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020