[issue34978] check type of object in fix_dict.py in 2to3
Pranav Devarakonda added the comment: Thank you very much for pointing out some helpful things, Karthikeyan. >True if 1 in list(a.keys()) if type(a) == dict else a.keys() else False True that the fixer would return a syntax error in this case. I missed adding a pair of parenthesis to the whole if statement. I mean True if 1 in (list(a.keys()) if type(a) == dict else a.keys()) else False is valid code. So adding parenthesis would solve the problem as I did in the updated patch I uploaded. I don't see this updated fixer returning errors in other cases like nested if conditions or list comprehensions, since the fixer would convert the respective function calls of dict objects into separate statements, would not interfere with other if statements(or any other statements for that matter) and set the order of precedence appropriately. Please do point out if there any other cases I missed. I know this is a less pythonic but is more accurate :) Thanks. -- Added file: https://bugs.python.org/file47871/fix_dict.py ___ Python tracker <https://bugs.python.org/issue34978> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34978] check type of object in fix_dict.py in 2to3
New submission from Pranav Devarakonda : fix_dict.py applies fixes to every instance of keys(), items() or values() irrespective of the type of object. Since 2to3 cannot check the type of the object, we can at least add the check to the generated code like... d.keys() -> list(d.keys) if type(d) == dict else d.keys() and similarly d.viewkeys() -> d.keys() if type(d) == dict else d.viewkeys() PFA the tweaked fixer. -- components: 2to3 (2.x to 3.x conversion tool) files: fix_dict.py messages: 327682 nosy: benjamin.peterson, devarakondapranav priority: normal severity: normal status: open title: check type of object in fix_dict.py in 2to3 type: enhancement Added file: https://bugs.python.org/file47866/fix_dict.py ___ Python tracker <https://bugs.python.org/issue34978> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34893] Add 2to3 fixer to change send and recv methods of socket object.
Pranav Devarakonda added the comment: I have added a final condition that converts the arguments passed to bytes only if the type of object is socket and the method is send() in the generated code. All the other conversions still function as expected. -- Added file: https://bugs.python.org/file47864/fix_socket_send_recv_reupdated.py ___ Python tracker <https://bugs.python.org/issue34893> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34893] Add 2to3 fixer to change send and recv methods of socket object.
Pranav Devarakonda added the comment: I am sorry. I got you completely wrong with this > "One possibility is to add a type check to the generated code" I thought you were asking to check the type in the fixer itself. I get it now. So you meant to add the type check for the changed/generate code.I think that is a good idea. > but this has its own issues and isn't satisfying. Why do you think this way? Can you please elaborate? -- ___ Python tracker <https://bugs.python.org/issue34893> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34893] Add 2to3 fixer to change send and recv methods of socket object.
Pranav Devarakonda added the comment: > One possibility is to add a type check to the generated code, "send(x)" -> > send(x.encode() if type(x)==bytes else x)" That would have solved the problem. However we cannot check the type of the object while the parsing is going on. For example, printing out the type(x) for the above example in any of the fixers would print "lib2to3.pytree.Node" (or "lib2to3.pytree.Leaf" depending on the object) but not the expected type() of the object like str or bytes. I haven't found out any method that can actually find out the type of the object in the fixer. If that can be done, then writing the fixer is pretty much straight forward. The other fixers in lib2to3, for example fix_dict.py, would convert all instances of viewkeys() to keys() irrespective of the type of the object that has called the method. That is also the case with all other fixers as well. Would appreciate very much if somebody can suggest how to do this. But since that is not the case, the fixer code has to handle these cases individually and I expect the current fixer to do a good job for the same. -- ___ Python tracker <https://bugs.python.org/issue34893> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34893] Add 2to3 fixer to change send and recv methods of socket object.
Pranav Devarakonda added the comment: Thanks for pointing out these cases. I kept in mind the Python 2 documentation which says socket.send method expects a string and hence made that fixer. I have tweaked that fixer to handle the pointed cases and a few additional ones too. Please find attached the updated fixer. To summarize, the following send() cases would not be changed, data = "Data" s.send(str.encode(data)) s.send(data.encode('utf-8)) s.send(bytes(data, 'utf-8')) s.send(struct.pack('!I', x)) Similary, the following recv() cases would not be changed, data = s.recv(1024).decode('utf-8') q, w, e = struct.unpack('!IHQ', s.recv(4)) The remaining generic cases will be handled as expected. I know we cannot handle cases where the data has already been converted to bytes(since there is no way to find the 'type' of the object here) and then sent as an argument to socket.send(). But if we have to go by the documentation, then this is probably the right thing to do. -- Added file: https://bugs.python.org/file47854/fix_socket_send_recv_updated.py ___ Python tracker <https://bugs.python.org/issue34893> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34893] Add 2to3 fixer to change send and recv methods of socket object.
Pranav Devarakonda added the comment: Thanks Karthikeyan -- ___ Python tracker <https://bugs.python.org/issue34893> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34893] Add 2to3 fixer to change send and recv methods of socket object.
Pranav Devarakonda added the comment: Thanks for taking time and updating this, Karthikeyan Singaravelan. I do agree that there there is no proper way to find out if an object is of type socket or not. However other fixers in lib2to3 are not any different. For example the fix_dict.py changes every instance of viewkeys() method to keys() irrespective of the type of the object. And I guess that applies to all other fixers in lib2to3 as well. So that convinced me that a fix for this can also be accommodated. A compromise that we could foster, as I already mentioned is to make this fix explicit so users can use this only if they need it. Please find attached the fixer I wrote. I haven't made a PR yet. -- Added file: https://bugs.python.org/file47850/fix_socket_send_recv.py ___ Python tracker <https://bugs.python.org/issue34893> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34893] Add 2to3 fixer to change send and recv methods of socket object.
New submission from Pranav Devarakonda : The send() method of the the Socket object in Python 3.x requires the data to be sent to be first converted into bytes(object) which was not the case with Python 2.x. The 2to3 tool doesn't handle this case and hence an explicit fixer would help many beginners learning the socket module(most tutorials regarding this online are in Python 2.x) Similarly the fixer can change the recv() method by converting the returned bytes object back to a str object. For example, consider the following lines in Python 2.x, (for demonstration only) s.send("Foo") #where 's' is a socket object data = s.recv(1024) After the 2to3 fixer has been applied the above lines will change to, s.send(str.encode("Foo")) data = s.recv(1024).decode("utf-8") PS: I am a beginner in open source so any changes or suggestions are welcome. -- components: 2to3 (2.x to 3.x conversion tool) messages: 327053 nosy: benjamin.peterson, devarakondapranav priority: normal severity: normal status: open title: Add 2to3 fixer to change send and recv methods of socket object. type: enhancement ___ Python tracker <https://bugs.python.org/issue34893> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com