Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of the lib3to6 project 

2# https://github.com/mbarkhau/lib3to6 

3# 

4# Copyright (c) 2019-2021 Manuel Barkhau (mbarkhau@gmail.com) - MIT License 

5# SPDX-License-Identifier: MIT 

6 

7import ast 

8 

9from . import common 

10from . import fixer_base as fb 

11 

12 

13def _try_fallback(node: ast.stmt, fallback_node: ast.stmt) -> ast.Try: 

14 return ast.Try( 

15 body=[node], 

16 handlers=[ 

17 ast.ExceptHandler( 

18 type=ast.Name(id="ImportError", ctx=ast.Load()), name=None, body=[fallback_node] 

19 ) 

20 ], 

21 orelse=[], 

22 finalbody=[], 

23 ) 

24 

25 

26class ModuleImportFallbackFixerBase(fb.TransformerFixerBase): 

27 

28 new_name: str 

29 old_name: str 

30 

31 def visit_Import(self, node: ast.Import) -> ast.stmt: 

32 if len(node.names) != 1: 

33 return node 

34 

35 alias = node.names[0] 

36 if alias.name != self.new_name: 

37 return node 

38 

39 if alias.asname: 

40 asname = alias.asname 

41 elif "." in self.new_name: 

42 asname = self.new_name.replace(".", "_") 

43 msg = ( 

44 f"Prohibited use of 'import {self.new_name}', " 

45 f"use 'import {self.new_name} as {asname}' instead." 

46 ) 

47 raise common.CheckError(msg, node) 

48 else: 

49 asname = self.new_name 

50 

51 return _try_fallback(node, ast.Import(names=[ast.alias(name=self.old_name, asname=asname)])) 

52 

53 def visit_ImportFrom(self, node: ast.ImportFrom) -> ast.stmt: 

54 if node.module != self.new_name: 

55 return node 

56 

57 return _try_fallback( 

58 node, ast.ImportFrom(module=self.old_name, names=node.names, level=node.level) 

59 ) 

60 

61 

62class ConfigParserImportFallbackFixer(ModuleImportFallbackFixerBase): 

63 

64 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

65 new_name = "configparser" 

66 old_name = "ConfigParser" 

67 

68 

69class SocketServerImportFallbackFixer(ModuleImportFallbackFixerBase): 

70 

71 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

72 new_name = "socketserver" 

73 old_name = "SocketServer" 

74 

75 

76class BuiltinsImportFallbackFixer(ModuleImportFallbackFixerBase): 

77 

78 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

79 new_name = "builtins" 

80 old_name = "__builtin__" 

81 

82 

83class QueueImportFallbackFixer(ModuleImportFallbackFixerBase): 

84 

85 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

86 new_name = "queue" 

87 old_name = "Queue" 

88 

89 

90class CopyRegImportFallbackFixer(ModuleImportFallbackFixerBase): 

91 

92 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

93 new_name = "copyreg" 

94 old_name = "copy_reg" 

95 

96 

97class WinRegImportFallbackFixer(ModuleImportFallbackFixerBase): 

98 

99 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

100 new_name = "winreg" 

101 old_name = "_winreg" 

102 

103 

104class ReprLibImportFallbackFixer(ModuleImportFallbackFixerBase): 

105 

106 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

107 new_name = "reprlib" 

108 old_name = "repr" 

109 

110 

111class ThreadImportFallbackFixer(ModuleImportFallbackFixerBase): 

112 

113 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

114 new_name = "_thread" 

115 old_name = "thread" 

116 

117 

118class DummyThreadImportFallbackFixer(ModuleImportFallbackFixerBase): 

119 

120 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

121 new_name = "_dummy_thread" 

122 old_name = "dummy_thread" 

123 

124 

125# NOTE (mb 2018-09-01): Up to here are the simple cases. Below 

126# here, the fixes only work when using ast.FromImport, or when 

127# using asname. For everything else, we raise a CheckError. The 

128# only other option would be to scan whole tree and rewrite 

129# reverences 

130 

131 

132class HttpCookiejarImportFallbackFixer(ModuleImportFallbackFixerBase): 

133 

134 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

135 new_name = "http.cookiejar" 

136 old_name = "cookielib" 

137 

138 

139class UrllibParseImportFallbackFixer(ModuleImportFallbackFixerBase): 

140 

141 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

142 new_name = "urllib.parse" 

143 old_name = "urlparse" 

144 

145 

146class UrllibRequestImportFallbackFixer(ModuleImportFallbackFixerBase): 

147 

148 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

149 new_name = "urllib.request" 

150 old_name = "urllib2" 

151 

152 

153class UrllibErrorImportFallbackFixer(ModuleImportFallbackFixerBase): 

154 

155 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

156 new_name = "urllib.error" 

157 old_name = "urllib2" 

158 

159 

160class UrllibRobotParserImportFallbackFixer(ModuleImportFallbackFixerBase): 

161 

162 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

163 new_name = "urllib.robotparser" 

164 old_name = "robotparser" 

165 

166 

167class XMLRPCClientImportFallbackFixer(ModuleImportFallbackFixerBase): 

168 

169 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

170 new_name = "xmlrpc.client" 

171 old_name = "xmlrpclib" 

172 

173 

174class XmlrpcServerImportFallbackFixer(ModuleImportFallbackFixerBase): 

175 

176 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

177 new_name = "xmlrpc.server" 

178 old_name = "SimpleXMLRPCServer" 

179 

180 

181class HtmlParserImportFallbackFixer(ModuleImportFallbackFixerBase): 

182 

183 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

184 new_name = "html.parser" 

185 old_name = "HTMLParser" 

186 

187 

188class HttpClientImportFallbackFixer(ModuleImportFallbackFixerBase): 

189 

190 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

191 new_name = "http.client" 

192 old_name = "httplib" 

193 

194 

195class HttpCookiesImportFallbackFixer(ModuleImportFallbackFixerBase): 

196 

197 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

198 new_name = "http.cookies" 

199 old_name = "Cookie" 

200 

201 

202class PickleImportFallbackFixer(ModuleImportFallbackFixerBase): 

203 

204 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

205 new_name = "pickle" 

206 old_name = "cPickle" 

207 

208 

209class DbmGnuImportFallbackFixer(ModuleImportFallbackFixerBase): 

210 

211 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

212 new_name = "dbm.gnu" 

213 old_name = "gdbm" 

214 

215 

216class EmailMimeBaseImportFallbackFixer(ModuleImportFallbackFixerBase): 

217 

218 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

219 new_name = "email.mime.base" 

220 old_name = "email.MIMEBase" 

221 

222 

223class EmailMimeImageImportFallbackFixer(ModuleImportFallbackFixerBase): 

224 

225 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

226 new_name = "email.mime.image" 

227 old_name = "email.MIMEImage" 

228 

229 

230class EmailMimeMultipartImportFallbackFixer(ModuleImportFallbackFixerBase): 

231 

232 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

233 new_name = "email.mime.multipart" 

234 old_name = "email.MIMEMultipart" 

235 

236 

237class EmailMimeNonmultipartImportFallbackFixer(ModuleImportFallbackFixerBase): 

238 

239 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

240 new_name = "email.mime.nonmultipart" 

241 old_name = "email.MIMENonMultipart" 

242 

243 

244class EmailMimeTextImportFallbackFixer(ModuleImportFallbackFixerBase): 

245 

246 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

247 new_name = "email.mime.text" 

248 old_name = "email.MIMEText" 

249 

250 

251class TkinterImportFallbackFixer(ModuleImportFallbackFixerBase): 

252 

253 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

254 new_name = "tkinter" 

255 old_name = "Tkinter" 

256 

257 

258class TkinterDialogImportFallbackFixer(ModuleImportFallbackFixerBase): 

259 

260 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

261 new_name = "tkinter.dialog" 

262 old_name = "Dialog" 

263 

264 

265class TkinterScrolledTextImportFallbackFixer(ModuleImportFallbackFixerBase): 

266 

267 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

268 new_name = "tkinter.scrolledtext" 

269 old_name = "ScrolledText" 

270 

271 

272class TkinterTixImportFallbackFixer(ModuleImportFallbackFixerBase): 

273 

274 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

275 new_name = "tkinter.tix" 

276 old_name = "Tix" 

277 

278 

279class TkinterTtkImportFallbackFixer(ModuleImportFallbackFixerBase): 

280 

281 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

282 new_name = "tkinter.ttk" 

283 old_name = "ttk" 

284 

285 

286class TkinterConstantsImportFallbackFixer(ModuleImportFallbackFixerBase): 

287 

288 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

289 new_name = "tkinter.constants" 

290 old_name = "Tkconstants" 

291 

292 

293class TkinterDndImportFallbackFixer(ModuleImportFallbackFixerBase): 

294 

295 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

296 new_name = "tkinter.dnd" 

297 old_name = "Tkdnd" 

298 

299 

300class TkinterColorchooserImportFallbackFixer(ModuleImportFallbackFixerBase): 

301 

302 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

303 new_name = "tkinter.colorchooser" 

304 old_name = "tkColorChooser" 

305 

306 

307class TkinterCommonDialogImportFallbackFixer(ModuleImportFallbackFixerBase): 

308 

309 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

310 new_name = "tkinter.commondialog" 

311 old_name = "tkCommonDialog" 

312 

313 

314class TkinterFontImportFallbackFixer(ModuleImportFallbackFixerBase): 

315 

316 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

317 new_name = "tkinter.font" 

318 old_name = "tkFont" 

319 

320 

321class TkinterMessageboxImportFallbackFixer(ModuleImportFallbackFixerBase): 

322 

323 version_info = common.VersionInfo(apply_since="2.3", apply_until="2.7") 

324 new_name = "tkinter.messagebox" 

325 old_name = "tkMessageBox" 

326 

327 

328# TODO (mb 2018-09-02): Only ImportFrom is permitted for 

329# certain imports, so that we can determine which old 

330# module to import from 

331# 

332# new_name = "http.server" 

333# old_name = "BaseHTTPServer" 

334# old_name = "CGIHTTPServer" 

335# old_name = "SimpleHTTPServer" 

336# 

337# new_name = "tkinter.simpledialog" 

338# old_name = "tkSimpleDialog" 

339# old_name = "SimpleDialog" 

340# 

341# new_name = "tkinter.filedialog" 

342# old_name = "tkFileDialog" 

343# old_name = "FileDialog"