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 

13class BuiltinsRenameFixerBase(fb.FixerBase): 

14 

15 new_name: str 

16 old_name: str 

17 

18 def apply_fix(self, ctx: common.BuildContext, tree: ast.Module) -> ast.Module: 

19 for node in ast.walk(tree): 

20 is_access_to_builtin = ( 

21 isinstance(node, ast.Name) 

22 and isinstance(node.ctx, ast.Load) 

23 and node.id == self.new_name 

24 ) 

25 

26 if is_access_to_builtin: 

27 self.required_imports.add(common.ImportDecl("builtins", None, "__builtin__")) 

28 builtin_renmae_decl_str = f""" 

29 {self.new_name} = getattr(builtins, '{self.old_name}', {self.new_name}) 

30 """ 

31 self.module_declarations.add(builtin_renmae_decl_str.strip()) 

32 

33 return tree 

34 

35 

36class XrangeToRangeFixer(BuiltinsRenameFixerBase): 

37 

38 version_info = common.VersionInfo(apply_until="2.7") 

39 

40 new_name = "range" 

41 old_name = "xrange" 

42 

43 

44class UnicodeToStrFixer(BuiltinsRenameFixerBase): 

45 

46 version_info = common.VersionInfo(apply_until="2.7") 

47 

48 new_name = "str" 

49 old_name = "unicode" 

50 

51 

52class UnichrToChrFixer(BuiltinsRenameFixerBase): 

53 

54 version_info = common.VersionInfo(apply_until="2.7") 

55 

56 new_name = "chr" 

57 old_name = "unichr" 

58 

59 

60class RawInputToInputFixer(BuiltinsRenameFixerBase): 

61 

62 version_info = common.VersionInfo(apply_until="2.7") 

63 

64 new_name = "input" 

65 old_name = "raw_input"