Page 36 - 《软件学报》2025年第4期
P. 36
1442 软件学报 2025 年第 36 卷第 4 期
示例代码 2. 输入参数格式错误 (S1).
# 版本变更: pandas0.25.3 → pandas1.0.0
1 – def to_stata(self, fname, convert_dates=None, write_index=True, encoding=‘latin-1’, byteorder=None):
2 + def to_stata(self, path, convert_dates=None, write_index=True, byteorder=None):
3 ...
4 – writer = statawriter(fname, ...)
5 + writer = statawriter(path, ...)
6 writer.write_file()
# 受兼容性问题影响的上层应用代码
1 cls.to_stata(‘fname’, convert_dates=‘convert_dates’, write_index=‘write_index’, encoding=‘latin-1’,
byteorder=‘byteorder’)
2 cls.to_stata(fname=‘fname’)
43 个 API 的兼容性问题表现为非法输入参数值 (S2), 即上层调用 API 时输入的参数值不合要求, 如示例代
码 3. 此时, 抛出的异常类型由第三方库源代码中的 raise 语句决定. 其中, 90.70% 的 API 抛出异常 ValueError, 其
余 9.30% 的 API 抛出异常 AssertionError、IndexingError、TypeError 或 KeyError.
示例代码 3. 非法输入参数值 (S2).
# 版本变更: pandas1.4.4 → pandas1.5.0
1 def asof(self, where, subset=None):
2 – if not self.index.is_monotonic:
3 + if not self.index.is_monotonic_increasing:
4 raise ValueError(‘asof requires a sorted index’)
5 ... 版本
# 受兼容性问题影响的上层应用代码
1 cls.index.is_monotonic = None
2 cls.asof(‘where’)
3 个 API 的兼容性问题表现为非法输入参数类型 (S3), 即上层调用 API 时输入的参数类型不合要求. 此时, 抛
出的异常类型由第三方库源代码中的 raise 语句决定. 本文收集到的 API 所抛出异常均为 TypeError.
40 个 API 的兼容性问题表现为输出变量值错误 (S4), 即当上层调用 API 的输入不变时, API 返回了不同的输
出值.
1 个 API 的兼容性问题表现为输出变量类型错误 (S5), 即当上层调用 API 的输入不变时, API 返回了不同的
输出类型.
例 2: 示例代码 4 中, 若第 4 行的条件在 v1 pandas1.4.4 成立, 则 API 会返回 None. 然而在 v2 版本
pandas1.5.0 中, 同样的情况会返回一个 DataFrame 类型的对象.
6 个 API 的兼容性问题表现为新异常无法捕获错误 (S6), 即上层应用在 try-except 结构中调用 API 时, v2 版
本抛出的异常无法被正常捕获, 导致上层应用代码的执行路径发生更改.
示例代码 4. 输出变量类型错误 (S5).
# 版本变更: pandas1.4.4 → pandas1.5.0
1 def read(self, nrows=None):