PCART  v1.4
Automated Repair of Python API Parameter Compatibility Issues
extractCall.py
Go to the documentation of this file.
1 
10 
11 
12 
13 import ast
14 
15 
16 
17 
21 class Import(ast.NodeVisitor):
22 
24  def __init__(self):
25  self._md_name_md_name={}
26 
27 
30  def get_md_name(self):
31  return self._md_name_md_name
32 
33 
37  def visit_Import(self, node):
38  item=[nn.__dict__ for nn in node.names] #item中每个元素都是一个字典
39  for it in item:
40  if it["asname"] is None:
41  self._md_name_md_name[it["name"]]=it["name"]
42  else:
43  self._md_name_md_name[it["asname"]]=it["name"]
44 
45 
49  def visit_ImportFrom(self, node):
50  if node.module is not None:
51  item=[nn.__dict__ for nn in node.names]
52  for it in item:
53  if it["asname"] is None:
54  self._md_name_md_name[it["name"]]=node.module+'.'+it["name"]
55  else:
56  self._md_name_md_name[it["asname"]]=node.module+'.'+it["name"]
57 
58 
59 
60 
66 
68  def __init__(self):
69  self._func_call_func_call=[] #list中每个元素都是一个tuple
70 
71 
75  @property
76  def func_call(self):
77  return self._func_call_func_call
78 
79 
86  def dfsVisit(self,node):
87  #先递推再回归
88  for n in ast.iter_child_nodes(node):
89  self.dfsVisitdfsVisit(n)
90 
91  if isinstance(node,ast.Call):
92  callName=ast.unparse(node.func)
93  callState=ast.unparse(node) #还原之后的语句可能和项目中的语句存在差异,比如空格等
94  argLst=[]
95  for arg in node.args:
96  argLst.append(ast.unparse(arg))
97  for keyword in node.keywords:
98  argLst.append(ast.unparse(keyword))
99  parameters=','.join(argLst)
100  callInfo=(
101  callName,
102  parameters,
103  callState,
104  node.lineno,
105  node.col_offset,
106  getattr(node,'end_lineno',None),
107  getattr(node,'end_col_offset',None),
108  )
109  if callInfo not in self._func_call_func_call:
110  self._func_call_func_call.append(callInfo)
111  else:
112  pass
113  return
114 
115 
116 
117 
122 class WithVisitor(ast.NodeVisitor):
123 
127  def __init__(self):
128  self._withitemCall_withitemCall={}
129 
130 
133  def get_withitem_call(self):
134  return self._withitemCall_withitemCall
135 
136 
142  def visit_With(self, node):
143  self._visit_with_items_visit_with_items(node)
144  for stmt in node.body:
145  self.visit(stmt)
146 
147 
151  def visit_AsyncWith(self, node):
152  self._visit_with_items_visit_with_items(node)
153  for stmt in node.body:
154  self.visit(stmt)
155 
156 
160  def _visit_with_items(self, node):
161  for item in node.items:
162  self._record_withitem_record_withitem(item, node)
163 
164 
168  def visit_withitem(self, node):
169  self._record_withitem_record_withitem(node)
170 
171 
178  def _record_withitem(self, node, parent=None):
179  if isinstance(node.context_expr, ast.Call):
180  if node.optional_vars:
181  callName = ast.unparse(node.context_expr)
182  aliasName = ast.unparse(node.optional_vars)
183  # lineno/end_lineno用于后续按调用点选择当前作用域内的withitem
184  item = {
185  'callName': callName,
186  'lineno': getattr(parent, 'lineno', None),
187  'end_lineno': getattr(parent, 'end_lineno', None),
188  }
189  self._withitemCall_withitemCall.setdefault(aliasName, []).append(item)
Extract all call type nodes from a project source file 抽取项目源码中的所有call类型节点
Definition: extractCall.py:65
def func_call(self)
Return collected function call tuples 返回收集到的函数调用元组
Definition: extractCall.py:76
def dfsVisit(self, node)
Depth-first search visit that extracts Call nodes 深度优先遍历,提取Call节点
Definition: extractCall.py:86
def __init__(self)
Initialize the function call list 初始化函数调用列表
Definition: extractCall.py:68
Import and ImportFrom node visitor Import和ImportFrom节点遍历器
Definition: extractCall.py:21
def visit_Import(self, node)
Visit an Import node and record alias-to-name mappings 访问Import节点,记录别名到模块名的映射
Definition: extractCall.py:37
def get_md_name(self)
Return the module name dictionary 返回模块名字典
Definition: extractCall.py:30
def visit_ImportFrom(self, node)
Visit an ImportFrom node and record alias-to-qualified-name mappings 访问ImportFrom节点,记录别名到完整限定名的映射
Definition: extractCall.py:49
def __init__(self)
Initialize the module name dictionary 初始化模块名字典
Definition: extractCall.py:24
Get all with nodes from a project source file 项目源码with节点遍历器
Definition: extractCall.py:122
def visit_AsyncWith(self, node)
Visit an AsyncWith node with the same alias resolution rules as With 访问AsyncWith节点,与With保持相同的别名还原规则
Definition: extractCall.py:151
def get_withitem_call(self)
Return the withitem call dictionary 返回withitem调用字典
Definition: extractCall.py:133
def _visit_with_items(self, node)
Definition: extractCall.py:160
def visit_With(self, node)
Visit a With node and record withitem context expressions 访问With节点,记录withitem上下文表达式
Definition: extractCall.py:142
def _record_withitem(self, node, parent=None)
Definition: extractCall.py:178
def visit_withitem(self, node)
Visit a standalone withitem node (top-level visitor entry) 访问独立的withitem节点
Definition: extractCall.py:168
def __init__(self)
Initialize the withitem call dictionary 初始化withitem调用字典
Definition: extractCall.py:127