用2to3将selenium.py转换为Python 3

3 投票
1 回答
2534 浏览
提问于 2025-04-16 14:48

我正在尝试把用Python 2写的selenium.py转换成其他版本。

正如你看到的,我把selenium.py复制到了C:\Python32\Tools\Scripts>这个文件夹里,然后执行了下面的命令:

2to3.py selenium.py

结果我发现文件没有变化。我到底做错了什么呢?或者我可以从哪里下载到转换好的文件呢?

C:\Python32\Tools\Scripts>2to3.py selenium.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored selenium.py
--- selenium.py (original)
+++ selenium.py (refactored)
@@ -18,8 +18,8 @@

 # This file has been automatically generated via XSL

-import httplib
-import urllib
+import http.client
+import urllib.request, urllib.parse, urllib.error
 import re

 class selenium:
@@ -186,34 +186,34 @@
         self.extensionJs = extensionJs

     def start(self):
-        print (self.browserURL)
+        print((self.browserURL))
         result = self.get_string("getNewBrowserSession", [self.browserStartComm
and, self.browserURL, self.extensionJs])
         try:
             self.sessionId = result
         except ValueError:
-            raise Exception, result
+            raise Exception(result)

     def stop(self):
         self.do_command("testComplete", [])
         self.sessionId = None

     def do_command(self, verb, args):
-        conn = httplib.HTTPConnection(self.host, self.port)
-        body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8'))
+        conn = http.client.HTTPConnection(self.host, self.port)
+        body = 'cmd=' + urllib.parse.quote_plus(str(verb).encode('utf-8'))
         for i in range(len(args)):
-            body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i
]).encode('utf-8'))
+            body += '&' + str(i+1) + '=' + urllib.parse.quote_plus(str(args[i])
.encode('utf-8'))
         if (None != self.sessionId):
-            body += "&sessionId=" + unicode(self.sessionId)
+            body += "&sessionId=" + str(self.sessionId)
         headers = {"Content-Type": "application/x-www-form-urlencoded; charset=
utf-8"}
         conn.request("POST", "/selenium-server/driver/", body, headers)

         response = conn.getresponse()
         #print response.status, response.reason
-        data = unicode(response.read(), "UTF-8")
+        data = str(response.read(), "UTF-8")
         result = response.reason
         #print "Selenium Result: " + repr(data) + "\n\n"
         if (not data.startswith('OK')):
-            raise Exception, data
+            raise Exception(data)
         return data

     def get_string(self, verb, args):
@@ -255,7 +255,7 @@
             return True
         if ("false" == boolstr):
             return False
-        raise ValueError, "result is neither 'true' nor 'false': " + boolstr
+        raise ValueError("result is neither 'true' nor 'false': " + boolstr)

     def get_boolean_array(self, verb, args):
         boolarr = self.get_string_array(verb, args)
@@ -266,7 +266,7 @@
             if ("false" == boolstr):
                 boolarr[i] = False
                 continue
-            raise ValueError, "result is neither 'true' nor 'false': " + boolar
r[i]
+            raise ValueError("result is neither 'true' nor 'false': " + boolarr
[i])
         return boolarr


RefactoringTool: Files that need to be modified:
RefactoringTool: selenium.py

C:\Python32\Tools\Scripts>2to3.py selenium.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored selenium.py
--- selenium.py (original)
+++ selenium.py (refactored)
@@ -18,8 +18,8 @@

 # This file has been automatically generated via XSL

-import httplib
-import urllib
+import http.client
+import urllib.request, urllib.parse, urllib.error
 import re

 class selenium:
@@ -186,34 +186,34 @@
         self.extensionJs = extensionJs

     def start(self):
-        print (self.browserURL)
+        print((self.browserURL))
         result = self.get_string("getNewBrowserSession", [self.browserStartComm
and, self.browserURL, self.extensionJs])
         try:
             self.sessionId = result
         except ValueError:
-            raise Exception, result
+            raise Exception(result)

     def stop(self):
         self.do_command("testComplete", [])
         self.sessionId = None

     def do_command(self, verb, args):
-        conn = httplib.HTTPConnection(self.host, self.port)
-        body = u'cmd=' + urllib.quote_plus(unicode(verb).encode('utf-8'))
+        conn = http.client.HTTPConnection(self.host, self.port)
+        body = 'cmd=' + urllib.parse.quote_plus(str(verb).encode('utf-8'))
         for i in range(len(args)):
-            body += '&' + unicode(i+1) + '=' + urllib.quote_plus(unicode(args[i
]).encode('utf-8'))
+            body += '&' + str(i+1) + '=' + urllib.parse.quote_plus(str(args[i])
.encode('utf-8'))
         if (None != self.sessionId):
-            body += "&sessionId=" + unicode(self.sessionId)
+            body += "&sessionId=" + str(self.sessionId)
         headers = {"Content-Type": "application/x-www-form-urlencoded; charset=
utf-8"}
         conn.request("POST", "/selenium-server/driver/", body, headers)

         response = conn.getresponse()
         #print response.status, response.reason
-        data = unicode(response.read(), "UTF-8")
+        data = str(response.read(), "UTF-8")
         result = response.reason
         #print "Selenium Result: " + repr(data) + "\n\n"
         if (not data.startswith('OK')):
-            raise Exception, data
+            raise Exception(data)
         return data

     def get_string(self, verb, args):
@@ -255,7 +255,7 @@
             return True
         if ("false" == boolstr):
             return False
-        raise ValueError, "result is neither 'true' nor 'false': " + boolstr
+        raise ValueError("result is neither 'true' nor 'false': " + boolstr)

     def get_boolean_array(self, verb, args):
         boolarr = self.get_string_array(verb, args)
@@ -266,7 +266,7 @@
             if ("false" == boolstr):
                 boolarr[i] = False
                 continue
-            raise ValueError, "result is neither 'true' nor 'false': " + boolar
r[i]
+            raise ValueError("result is neither 'true' nor 'false': " + boolarr
[i])
         return boolarr


RefactoringTool: Files that need to be modified:
RefactoringTool: selenium.py

1 个回答

5

这个内容在官方文档中有介绍,关于2to3工具的使用:http://docs.python.org/library/2to3.html

默认情况下,它只是显示文件之间的差异,供你查看(也可以选择保存为补丁)。

如果你想要直接覆盖原来的文件,就必须加上-w这个选项。

撰写回答