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 numb
(obj)
| 350 | |
| 351 | |
| 352 | def find_source_lines(obj): |
| 353 | """Find the line number in a file where an object was defined. |
| 354 | |
| 355 | This is essentially a robust wrapper around `inspect.getsourcelines`. |
| 356 | |
| 357 | Returns None if no file can be found. |
| 358 | |
| 359 | Parameters |
| 360 | ---------- |
| 361 | obj : any Python object |
| 362 | |
| 363 | Returns |
| 364 | ------- |
| 365 | lineno : int |
| 366 | The line number where the object definition starts. |
| 367 | """ |
| 368 | obj = _get_wrapped(obj) |
| 369 | |
| 370 | try: |
| 371 | lineno = inspect.getsourcelines(obj)[1] |
| 372 | except TypeError: |
| 373 | # For instances, try the class object like getsource() does |
| 374 | try: |
| 375 | lineno = inspect.getsourcelines(obj.__class__)[1] |
| 376 | except (OSError, TypeError): |
| 377 | return None |
| 378 | except OSError: |
| 379 | return None |
| 380 | |
| 381 | return lineno |
| 382 | |
| 383 | |
| 384 | _sentinel = object() |
no test coverage detected
searching dependent graphs…