I would like to list all the functions and methods in the Python standard library that could throw an OSError
. Do you know of a way to find that out per python version?
I thought about running an AST on the C code, but I wonder if there is a simpler way (maybe dynamically).
CodePudding user response:
Enumerate the contents of the target module, use inspect
to filter for classes, and then test if OSError
is part of the base class list:
import inspect
module = globals()['__builtins__']
for name, value in module.__dict__.items():
if inspect.isclass(value) and OSError in value.__bases__:
print(name)
CodePudding user response:
I will tell you hard/Ugly process
Import 'library` that you want to analyze
use
inspect
library to get source code of the particularlibrary
likeimport re import inspect lines = inspect.getsource(re) print(lines)
runing
type(lines)
---> output: str
so we can apply some thing like regurlar expressions
? to hunt that keyword
aka os error
...
To list all the fun inside program
import re
re.findall('def (. ?)\s?\(', lines)
output
['__repr__',
'match',
'fullmatch',
'search',
'sub',
'subn',
'split',
'findall',
'finditer',
'compile',
'purge',
'template',
'escape',
'_compile',
'_compile_repl',
'_expand',
'_subx',
'filter',
'_pickle',
'__init__',
'scan']
to get all the errors inside the programs
re.findall('raise (. ?)\s?\(', lines)
output
['ValueError', 'TypeError']
still program is not finished...Next steps you need to implement is
using Regx
you need to find text between 2 keywords in our case def
& os error
(you have to do it)....It will spit out some text...
def name():
some code
some code
raise error()
return
After the extracting text between keywords output will be something like
#output str after applying `re`
name():
some code
some code
raise error(
From the output just print the text
before (
using regex
as program pattern gives function
name with particular error
you looking for