hassanakbar4 / tractive-test

0 stars 0 forks source link

Deprecation warning when running with python3 #340

Closed hassanakbar4 closed 3 years ago

hassanakbar4 commented 6 years ago

component_Version 2 cli resolution_fixed type_defect | by scott@kitterman.com


Running the tests with the current (and probably any) xml2rfc version on python3 yields the following warning:

test_header_footer (main.WriterDraftTest) ... /tmp/buildd/xml2rfc-2.8.4/xml2rfc/parser.py:440: DeprecationWarning: 'U' mode is deprecated self.text = open(self.source, "rUb").read()

It would be nice to see this resolved before it turns into an error. The fix is trivial:

diff --git a/xml2rfc/parser.py b/xml2rfc/parser.py
index 9bd0631..03b4a25 100644
--- a/xml2rfc/parser.py
+++ b/xml2rfc/parser.py
@@ -437,7 +437,7 @@ class XmlRfcParser:
         if six.PY2:
             self.text = open(self.source, "rU").read()
         else:
-            self.text = open(self.source, "rUb").read()
+            self.text = open(self.source, "rb").read()

         # Get an iterating parser object
         file = six.BytesIO(self.text)

Thanks for your work on xml2rfc,

Scott K


Issue migrated from trac:340 at 2021-10-20 18:25:35 +0500

hassanakbar4 commented 6 years ago

@{"email"=>"scott@kitterman.com", "name"=>nil, "username"=>nil} commented


Trac didn't handle my diff very well. To be clear, the fix is to delete the "U" from line 440.

Scott K

hassanakbar4 commented 6 years ago

@{"email"=>"henrik@levkowetz.com", "name"=>nil, "username"=>nil} edited the issue description

hassanakbar4 commented 6 years ago

@{"email"=>"henrik@levkowetz.com", "name"=>nil, "username"=>nil} commented


Thanks, Scott.

However, I'm not sure the fix is that simple -- there's a reason why the Universal newline flag was needed (even if I don't recall what prompted it, without going back in the commit history). Removing the 'U' flag would also mean investigating why it was needed, and probably adding code to handle newline normalization after reading the file.

hassanakbar4 commented 6 years ago

@{"email"=>"scott@kitterman.com", "name"=>nil, "username"=>nil} changed _comment0 which not transferred by tractive

hassanakbar4 commented 6 years ago

@{"email"=>"scott@kitterman.com", "name"=>nil, "username"=>nil} commented


I checked the documentation and whatever it was there for, it serves no purpose now. Here's what help(open) has to say about it in python3.4:

'U' mode is deprecated and will raise an exception in future versions
of Python.  It has no effect in Python 3.  Use newline to control
universal newlines mode.

Thanks for fixing the description.

Scott K

hassanakbar4 commented 6 years ago

@{"email"=>"henrik@levkowetz.com", "name"=>nil, "username"=>nil} commented


Sure, and thanks for the help text pointing at the newline= parameter.

Now running tests for the diff:

Index: xml2rfc/parser.py
===================================================================
--- xml2rfc/parser.py   (revision 2404)
+++ xml2rfc/parser.py   (working copy)
@@ -435,9 +435,11 @@
             xml2rfc.log.write('Parsing file', self.source)

         if six.PY2:
-            self.text = open(self.source, "rU").read()
+            with open(self.source, "rU") as f:
+                self.text = f.read()
         else:
-            self.text = open(self.source, "rUb").read()
+            with open(self.source, "rb", newline=None) as f:
+                self.text = f.read()

         # Get an iterating parser object
         file = six.BytesIO(self.text)

(Running tests under Py3.5, there was also a warning for the dangling open file created by the earlier code).

hassanakbar4 commented 6 years ago

@{"email"=>"henrik@levkowetz.com", "name"=>nil, "username"=>nil} changed status from new to closed

hassanakbar4 commented 6 years ago

@{"email"=>"henrik@levkowetz.com", "name"=>nil, "username"=>nil} changed resolution from ` tofixed`

hassanakbar4 commented 6 years ago

@{"email"=>"henrik@levkowetz.com", "name"=>nil, "username"=>nil} commented


Fixed in [2405]:

Changed a file open mode under python3 to use the newline= parameter to 
open() instead of the deprecated 'U' mode (thanks to spf2@kitterman.com for 
pointing that out).  Also changed the code to avoid a dangling open file 
handle.