Find the line number in a file where an object was defined. This is essentially a robust wrapper around `inspect.getsourcelines`. Returns None if no file can be found. Parameters ---------- obj : any Python object Returns ------- lineno : int The line number
(obj)
| 320 | |
| 321 | |
| 322 | def find_source_lines(obj): |
| 323 | """Find the line number in a file where an object was defined. |
| 324 | |
| 325 | This is essentially a robust wrapper around `inspect.getsourcelines`. |
| 326 | |
| 327 | Returns None if no file can be found. |
| 328 | |
| 329 | Parameters |
| 330 | ---------- |
| 331 | obj : any Python object |
| 332 | |
| 333 | Returns |
| 334 | ------- |
| 335 | lineno : int |
| 336 | The line number where the object definition starts. |
| 337 | """ |
| 338 | obj = _get_wrapped(obj) |
| 339 | |
| 340 | try: |
| 341 | try: |
| 342 | lineno = inspect.getsourcelines(obj)[1] |
| 343 | except TypeError: |
| 344 | # For instances, try the class object like getsource() does |
| 345 | if hasattr(obj, '__class__'): |
| 346 | lineno = inspect.getsourcelines(obj.__class__)[1] |
| 347 | else: |
| 348 | lineno = None |
| 349 | except: |
| 350 | return None |
| 351 | |
| 352 | return lineno |
| 353 | |
| 354 | class Inspector(Colorable): |
| 355 |
no test coverage detected