[issue43247] How to search for a __main__ module using pyclbr in Python3?

2021-02-17 Thread Aviral Srivastava


New submission from Aviral Srivastava :

I want to get all the functions and classes in module: `__main__` of the source 
code directory: `/tmp/rebound/rebound`. 
When I use the `pyclbr.readmodule_ex` API:
```
source_code_data = pyclbr.readmodule_ex(source_code_module, 
path=source_code_path)
```
I specify it the module and it's path:
```
DEBUG:root:Source code module: __main__, Source code path: 
['/tmp/rebound/rebound/rebound']
```
I then get this error:
```
File 
"/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/util.py",
 line 69, in _find_spec_from_path
raise ValueError('{}.__spec__ is None'.format(name))
ValueError: __main__.__spec__ is None
```
I then tried to use the function that is not supposed to be used by the public: 
`_readmodule`:
```
source_code_data = pyclbr._readmodule(source_code_module, source_code_path, )
```
But I could not decide what should be the value of the parameter: `inpackage`.

Upon tracing the code via debugger, I spotted a mistake:
```
def _find_spec_from_path(name, path=None):
"""Return the spec for the specified module.

First, sys.modules is checked to see if the module was already imported. If
so, then sys.modules[name].__spec__ is returned. If that happens to be
set to None, then ValueError is raised. If the module is not in
sys.modules, then sys.meta_path is searched for a suitable spec with the
value of 'path' given to the finders. None is returned if no spec could
be found.

Dotted names do not have their parent packages implicitly imported. You will
most likely need to explicitly import all parent packages in the proper
order for a submodule to get the correct spec.

"""
if name not in sys.modules:
return _find_spec(name, path)
else:
module = sys.modules[name]
if module is None:
return None
try:
spec = module.__spec__
except AttributeError:
raise ValueError('{}.__spec__ is not set'.format(name)) from None
else:
if spec is None:
raise ValueError('{}.__spec__ is None'.format(name))
return spec
```
This is the function in the module: `python3.8/importlib/util.py` and it 
evaluates `__main__` as a built-in module as it falls in the `else` block.

How do I differentiate `__main__` of my target source code to read from the 
built-in `__main__`? In other words, how do I read the module `__main__` of the 
codebase: rebound?

--
components: Library (Lib)
messages: 387200
nosy: kebab-mai-haddi
priority: normal
severity: normal
status: open
title: How to search for a __main__ module using pyclbr in Python3?
type: behavior
versions: Python 3.8

___
Python tracker 
<https://bugs.python.org/issue43247>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38307] Add .end_lineno attribute to pyclbr _Objects

2021-01-27 Thread Aviral Srivastava


Aviral Srivastava  added the comment:

Hey Terry, thanks for commenting. I have a few questions to ask you, please 
pardon my lack of awareness.

>This requieres end lines and having it be an attribute is easier and more 
>accurate than recalculating it.

How do you recalculate the end_lineno?


Since all the objects that _start_, have an _end_ too, would `end_lineno=None` 
make any sense?


I could not make anything out of your third point where you mention `readline` 
and `readline_ex`. Can you explain that point?


>I posted "What is the pyclbr public API" to pydev asking about this issue.

Can you share the link of your post?


As for my use case, I need the scope of a class and a function in the new tool 
that I am developing. My tool is to generate a new type of UML diagrams for a 
given codebase. Do check it out and leave your critical feedback: 
https://github.com/kebab-mai-haddi/gruml

I need to know about the scope (start and end) so that I can deduce whether the 
"used_at" attribute of an object is within a class. This will help me deduce 
whether a particular class (or a function) uses any imported object. This is to 
deduce the dependency of a class on other classes and functions.

Currently, I have to edit the pyclbr and make it custom, then, had to Dockerize 
the whole thing. So, want to contribute to `cpython` so that this feature is 
present at the source itself.

--

___
Python tracker 
<https://bugs.python.org/issue38307>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38307] Provide Class' end line in pyclbr module

2021-01-27 Thread Aviral Srivastava


Aviral Srivastava  added the comment:

I have made the changes and tested, my builds were successful. Please review 
it? 

CC @brandtbucher, @steven.daprano

--

___
Python tracker 
<https://bugs.python.org/issue38307>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38307] Provide Class' end line in pyclbr module

2021-01-26 Thread Aviral


Change by Aviral :


--
pull_requests: +23168
pull_request: https://github.com/python/cpython/pull/24348

___
Python tracker 
<https://bugs.python.org/issue38307>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38307] Provide Class' end line in pyclbr module

2021-01-25 Thread Aviral Srivastava


Aviral Srivastava  added the comment:

How do I generate the endline no? Initially, I could
do, stack[-1][0].end_lineno = start[0] - 1 but how do I this now given that the 
recent changes are operating on the AST instead of the token stream?

--
nosy: +kebab-mai-haddi

___
Python tracker 
<https://bugs.python.org/issue38307>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42920] How to add end_lineno in pyclbr?

2021-01-25 Thread Aviral Srivastava


Aviral Srivastava  added the comment:

But how do I generate the endline no? Initially, I could
do, stack[-1][0].end_lineno = start[0] - 1 but how do I this now?

Best,
Aviral Srivastava
LinkedIn
<https://mailtrack.io/trace/link/c4502b41561cd995471aa8291b55e07212663a68?url=https%3A%2F%2Fwww.linkedin.com%2Fin%2Fsriavi%2F=762683=7970b62394c71d67>
| Website
<https://mailtrack.io/trace/link/91745120231e6a5490b7cad9769cf43b37257fe4?url=https%3A%2F%2Faviralsrivastava.com%2F=762683=b0d6da90458944b7>

On Thu, Jan 14, 2021 at 11:12 AM Batuhan Taskaya 
wrote:

>
> Batuhan Taskaya  added the comment:
>
> It is actually much easier now, considering that we are operating on the
> AST instead of the token stream. You probably only have to add a new field
> to Function/Class classes for end_lineno and access .end_lineno attribute
> here just like how we do for .lineno
>
> https://github.com/python/cpython/blob/971235827754eee6c0d9f7d39b52fecdfd4cb7b4/Lib/pyclbr.py#L212-L214
> <https://mailtrack.io/trace/link/fcde5f4006527669633139919288094eb35d4671?url=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fblob%2F971235827754eee6c0d9f7d39b52fecdfd4cb7b4%2FLib%2Fpyclbr.py%23L212-L214=762683=3181a7c4de5a239b>
>
> --
>
> ___
> Python tracker 
> <https://bugs.python.org/issue42920
> <https://mailtrack.io/trace/link/a14e8d641bc2744efb70ac68058359d310914537?url=https%3A%2F%2Fbugs.python.org%2Fissue42920=762683=ef176fb5cb5491fd>
> >
> ___
>

--

___
Python tracker 
<https://bugs.python.org/issue42920>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue42920] How to add end_lineno in pyclbr?

2021-01-12 Thread Aviral Srivastava


New submission from Aviral Srivastava :

I created a PR a year ago, https://github.com/python/cpython/pull/16466/files, 
and now the source code is completely changed. How do I create another PR or if 
there is another way through which I can get an end line for each object (class 
and function) as stated 
here(https://stackoverflow.com/questions/58142456/how-to-get-the-scope-of-a-class-in-terms-of-starting-line-and-end-line-in-python).

--
components: Library (Lib)
messages: 385002
nosy: kebab-mai-haddi
priority: normal
severity: normal
status: open
title: How to add end_lineno in pyclbr?
type: enhancement
versions: Python 3.10

___
Python tracker 
<https://bugs.python.org/issue42920>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38307] Provide Class' end line in readmodule module

2019-10-08 Thread Aviral


Change by Aviral :


--
keywords: +patch
pull_requests: +16249
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/16466

___
Python tracker 
<https://bugs.python.org/issue38307>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38307] Provide Class' end line in readmodule module

2019-09-28 Thread Aviral


New submission from Aviral :

Currently, the `readmodule` returns the starting line of the classes but not 
the end line. This should be provided as well in order to get the scope of the 
class, mainly to help check what all imports were used in a class.

--
components: Library (Lib)
messages: 353467
nosy: aviral
priority: normal
severity: normal
status: open
title: Provide Class' end line in readmodule module
type: enhancement
versions: Python 3.6

___
Python tracker 
<https://bugs.python.org/issue38307>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com