getsources 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- getsources-0.0.1/LICENSE +21 -0
- getsources-0.0.1/PKG-INFO +100 -0
- getsources-0.0.1/README.md +70 -0
- getsources-0.0.1/getsources/__init__.py +2 -0
- getsources-0.0.1/getsources/base.py +11 -0
- getsources-0.0.1/getsources/clear.py +20 -0
- getsources-0.0.1/getsources/py.typed +0 -0
- getsources-0.0.1/getsources.egg-info/PKG-INFO +100 -0
- getsources-0.0.1/getsources.egg-info/SOURCES.txt +14 -0
- getsources-0.0.1/getsources.egg-info/dependency_links.txt +1 -0
- getsources-0.0.1/getsources.egg-info/requires.txt +1 -0
- getsources-0.0.1/getsources.egg-info/top_level.txt +1 -0
- getsources-0.0.1/pyproject.toml +52 -0
- getsources-0.0.1/setup.cfg +4 -0
- getsources-0.0.1/tests/test_base.py +136 -0
- getsources-0.0.1/tests/test_clear.py +174 -0
getsources-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The Mutating Company
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: getsources
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A way to get the source code of functions
|
|
5
|
+
Author-email: Evgeniy Blinov <zheni-b@yandex.ru>
|
|
6
|
+
Project-URL: Source, https://github.com/mutating/getsources
|
|
7
|
+
Project-URL: Tracker, https://github.com/mutating/getsources/issues
|
|
8
|
+
Keywords: inspect
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
11
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
12
|
+
Classifier: Operating System :: POSIX
|
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Programming Language :: Python :: Free Threading
|
|
23
|
+
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
|
|
24
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
25
|
+
Classifier: Intended Audience :: Developers
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
|
|
31
|
+
<details>
|
|
32
|
+
<summary>ⓘ</summary>
|
|
33
|
+
|
|
34
|
+
[](https://pepy.tech/project/getsources)
|
|
35
|
+
[](https://pepy.tech/project/getsources)
|
|
36
|
+
[](https://coveralls.io/github/mutating/getsources?branch=main)
|
|
37
|
+
[](https://github.com/boyter/scc/)
|
|
38
|
+
[](https://hitsofcode.com/github/mutating/getsources/view?branch=main)
|
|
39
|
+
[](https://github.com/mutating/getsources/actions/workflows/tests_and_coverage.yml)
|
|
40
|
+
[](https://pypi.python.org/pypi/getsources)
|
|
41
|
+
[](https://badge.fury.io/py/getsources)
|
|
42
|
+
[](http://mypy-lang.org/)
|
|
43
|
+
[](https://github.com/astral-sh/ruff)
|
|
44
|
+
[](https://deepwiki.com/mutating/getsources)
|
|
45
|
+
|
|
46
|
+
</details>
|
|
47
|
+
|
|
48
|
+

|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
This library is needed to obtain the source code of functions at runtime. It can be used, for example, as a basis for libraries that work with [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) on the fly. In fact, it is a thin layer built around [`inspect.getsource`](https://docs.python.org/3/library/inspect.html#inspect.getsource) and [`dill.source.getsource`](https://dill.readthedocs.io/en/latest/dill.html#dill.source.getsource).
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
You can install [`getsources`](https://pypi.python.org/pypi/getsources) using pip:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install getsources
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
You can also quickly try out this and other packages without having to install using [instld](https://github.com/pomponchik/instld).
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
The basic function of the library is `getsource`, which works similarly to the function of the same name from the standard library:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from getsources import getsource
|
|
71
|
+
|
|
72
|
+
def function():
|
|
73
|
+
...
|
|
74
|
+
|
|
75
|
+
print(getsource(function))
|
|
76
|
+
#> def function():
|
|
77
|
+
#> ...
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Unlike its counterpart from the standard library, this thing can also work:
|
|
81
|
+
|
|
82
|
+
- With lambda functions
|
|
83
|
+
- With functions defined inside REPL
|
|
84
|
+
|
|
85
|
+
We also often need to trim excess indentation from a function object to make it easier to further process the resulting code. To do this, use the `getclearsource` function:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from getsources import getclearsource
|
|
89
|
+
|
|
90
|
+
class SomeClass:
|
|
91
|
+
@staticmethod
|
|
92
|
+
def method():
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
print(getclearsource(SomeClass.method))
|
|
96
|
+
#> def method():
|
|
97
|
+
#> ...
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
As you can see, the resulting source code text has no extra indentation, but in all other respects this function is completely identical to the usual `getsource`.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
<details>
|
|
2
|
+
<summary>ⓘ</summary>
|
|
3
|
+
|
|
4
|
+
[](https://pepy.tech/project/getsources)
|
|
5
|
+
[](https://pepy.tech/project/getsources)
|
|
6
|
+
[](https://coveralls.io/github/mutating/getsources?branch=main)
|
|
7
|
+
[](https://github.com/boyter/scc/)
|
|
8
|
+
[](https://hitsofcode.com/github/mutating/getsources/view?branch=main)
|
|
9
|
+
[](https://github.com/mutating/getsources/actions/workflows/tests_and_coverage.yml)
|
|
10
|
+
[](https://pypi.python.org/pypi/getsources)
|
|
11
|
+
[](https://badge.fury.io/py/getsources)
|
|
12
|
+
[](http://mypy-lang.org/)
|
|
13
|
+
[](https://github.com/astral-sh/ruff)
|
|
14
|
+
[](https://deepwiki.com/mutating/getsources)
|
|
15
|
+
|
|
16
|
+
</details>
|
|
17
|
+
|
|
18
|
+

|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
This library is needed to obtain the source code of functions at runtime. It can be used, for example, as a basis for libraries that work with [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) on the fly. In fact, it is a thin layer built around [`inspect.getsource`](https://docs.python.org/3/library/inspect.html#inspect.getsource) and [`dill.source.getsource`](https://dill.readthedocs.io/en/latest/dill.html#dill.source.getsource).
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
You can install [`getsources`](https://pypi.python.org/pypi/getsources) using pip:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install getsources
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
You can also quickly try out this and other packages without having to install using [instld](https://github.com/pomponchik/instld).
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
The basic function of the library is `getsource`, which works similarly to the function of the same name from the standard library:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from getsources import getsource
|
|
41
|
+
|
|
42
|
+
def function():
|
|
43
|
+
...
|
|
44
|
+
|
|
45
|
+
print(getsource(function))
|
|
46
|
+
#> def function():
|
|
47
|
+
#> ...
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Unlike its counterpart from the standard library, this thing can also work:
|
|
51
|
+
|
|
52
|
+
- With lambda functions
|
|
53
|
+
- With functions defined inside REPL
|
|
54
|
+
|
|
55
|
+
We also often need to trim excess indentation from a function object to make it easier to further process the resulting code. To do this, use the `getclearsource` function:
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from getsources import getclearsource
|
|
59
|
+
|
|
60
|
+
class SomeClass:
|
|
61
|
+
@staticmethod
|
|
62
|
+
def method():
|
|
63
|
+
...
|
|
64
|
+
|
|
65
|
+
print(getclearsource(SomeClass.method))
|
|
66
|
+
#> def method():
|
|
67
|
+
#> ...
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
As you can see, the resulting source code text has no extra indentation, but in all other respects this function is completely identical to the usual `getsource`.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from inspect import getsource as original_getsource
|
|
2
|
+
from typing import Any, Callable
|
|
3
|
+
|
|
4
|
+
from dill.source import getsource as dill_getsource # type: ignore[import-untyped]
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def getsource(function: Callable[..., Any]) -> str:
|
|
8
|
+
try:
|
|
9
|
+
return original_getsource(function)
|
|
10
|
+
except OSError: # pragma: no cover
|
|
11
|
+
return dill_getsource(function) # type: ignore[no-any-return]
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from typing import Any, Callable
|
|
2
|
+
|
|
3
|
+
from getsources import getsource
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def getclearsource(function: Callable[..., Any]) -> str:
|
|
7
|
+
source_code = getsource(function)
|
|
8
|
+
|
|
9
|
+
splitted_source_code = source_code.split('\n')
|
|
10
|
+
|
|
11
|
+
indent = 0
|
|
12
|
+
for letter in splitted_source_code[0]: # pragma: no branch
|
|
13
|
+
if letter.isspace():
|
|
14
|
+
indent += 1
|
|
15
|
+
else:
|
|
16
|
+
break
|
|
17
|
+
|
|
18
|
+
new_splitted_source_code = [x[indent:] for x in splitted_source_code]
|
|
19
|
+
|
|
20
|
+
return '\n'.join(new_splitted_source_code).rstrip('\n')
|
|
File without changes
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: getsources
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A way to get the source code of functions
|
|
5
|
+
Author-email: Evgeniy Blinov <zheni-b@yandex.ru>
|
|
6
|
+
Project-URL: Source, https://github.com/mutating/getsources
|
|
7
|
+
Project-URL: Tracker, https://github.com/mutating/getsources/issues
|
|
8
|
+
Keywords: inspect
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
11
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
12
|
+
Classifier: Operating System :: POSIX
|
|
13
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Programming Language :: Python :: Free Threading
|
|
23
|
+
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
|
|
24
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
25
|
+
Classifier: Intended Audience :: Developers
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
|
|
31
|
+
<details>
|
|
32
|
+
<summary>ⓘ</summary>
|
|
33
|
+
|
|
34
|
+
[](https://pepy.tech/project/getsources)
|
|
35
|
+
[](https://pepy.tech/project/getsources)
|
|
36
|
+
[](https://coveralls.io/github/mutating/getsources?branch=main)
|
|
37
|
+
[](https://github.com/boyter/scc/)
|
|
38
|
+
[](https://hitsofcode.com/github/mutating/getsources/view?branch=main)
|
|
39
|
+
[](https://github.com/mutating/getsources/actions/workflows/tests_and_coverage.yml)
|
|
40
|
+
[](https://pypi.python.org/pypi/getsources)
|
|
41
|
+
[](https://badge.fury.io/py/getsources)
|
|
42
|
+
[](http://mypy-lang.org/)
|
|
43
|
+
[](https://github.com/astral-sh/ruff)
|
|
44
|
+
[](https://deepwiki.com/mutating/getsources)
|
|
45
|
+
|
|
46
|
+
</details>
|
|
47
|
+
|
|
48
|
+

|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
This library is needed to obtain the source code of functions at runtime. It can be used, for example, as a basis for libraries that work with [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) on the fly. In fact, it is a thin layer built around [`inspect.getsource`](https://docs.python.org/3/library/inspect.html#inspect.getsource) and [`dill.source.getsource`](https://dill.readthedocs.io/en/latest/dill.html#dill.source.getsource).
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
You can install [`getsources`](https://pypi.python.org/pypi/getsources) using pip:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install getsources
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
You can also quickly try out this and other packages without having to install using [instld](https://github.com/pomponchik/instld).
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
## Usage
|
|
66
|
+
|
|
67
|
+
The basic function of the library is `getsource`, which works similarly to the function of the same name from the standard library:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from getsources import getsource
|
|
71
|
+
|
|
72
|
+
def function():
|
|
73
|
+
...
|
|
74
|
+
|
|
75
|
+
print(getsource(function))
|
|
76
|
+
#> def function():
|
|
77
|
+
#> ...
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Unlike its counterpart from the standard library, this thing can also work:
|
|
81
|
+
|
|
82
|
+
- With lambda functions
|
|
83
|
+
- With functions defined inside REPL
|
|
84
|
+
|
|
85
|
+
We also often need to trim excess indentation from a function object to make it easier to further process the resulting code. To do this, use the `getclearsource` function:
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from getsources import getclearsource
|
|
89
|
+
|
|
90
|
+
class SomeClass:
|
|
91
|
+
@staticmethod
|
|
92
|
+
def method():
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
print(getclearsource(SomeClass.method))
|
|
96
|
+
#> def method():
|
|
97
|
+
#> ...
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
As you can see, the resulting source code text has no extra indentation, but in all other respects this function is completely identical to the usual `getsource`.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
getsources/__init__.py
|
|
5
|
+
getsources/base.py
|
|
6
|
+
getsources/clear.py
|
|
7
|
+
getsources/py.typed
|
|
8
|
+
getsources.egg-info/PKG-INFO
|
|
9
|
+
getsources.egg-info/SOURCES.txt
|
|
10
|
+
getsources.egg-info/dependency_links.txt
|
|
11
|
+
getsources.egg-info/requires.txt
|
|
12
|
+
getsources.egg-info/top_level.txt
|
|
13
|
+
tests/test_base.py
|
|
14
|
+
tests/test_clear.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dill==0.4.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
getsources
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools==68.0.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "getsources"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name="Evgeniy Blinov", email="zheni-b@yandex.ru" },
|
|
10
|
+
]
|
|
11
|
+
description = 'A way to get the source code of functions'
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.8"
|
|
14
|
+
dependencies = [
|
|
15
|
+
'dill==0.4.0',
|
|
16
|
+
]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
'Operating System :: MacOS :: MacOS X',
|
|
20
|
+
'Operating System :: Microsoft :: Windows',
|
|
21
|
+
'Operating System :: POSIX',
|
|
22
|
+
'Operating System :: POSIX :: Linux',
|
|
23
|
+
'Programming Language :: Python',
|
|
24
|
+
'Programming Language :: Python :: 3.8',
|
|
25
|
+
'Programming Language :: Python :: 3.9',
|
|
26
|
+
'Programming Language :: Python :: 3.10',
|
|
27
|
+
'Programming Language :: Python :: 3.11',
|
|
28
|
+
'Programming Language :: Python :: 3.12',
|
|
29
|
+
'Programming Language :: Python :: 3.13',
|
|
30
|
+
'Programming Language :: Python :: 3.14',
|
|
31
|
+
'Programming Language :: Python :: Free Threading',
|
|
32
|
+
'Programming Language :: Python :: Free Threading :: 3 - Stable',
|
|
33
|
+
'License :: OSI Approved :: MIT License',
|
|
34
|
+
'Intended Audience :: Developers',
|
|
35
|
+
'Topic :: Software Development :: Libraries',
|
|
36
|
+
]
|
|
37
|
+
keywords = ['inspect']
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.package-data]
|
|
40
|
+
"getsources" = ["py.typed"]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
markers = ["mypy_testing"]
|
|
44
|
+
|
|
45
|
+
[tool.ruff]
|
|
46
|
+
lint.ignore = ['E501', 'E712', 'PTH123', 'PTH118', 'PLR2004', 'PTH107', 'SIM105', 'SIM102', 'RET503', 'PLR0912', 'C901', 'E731']
|
|
47
|
+
lint.select = ["ERA001", "YTT", "ASYNC", "BLE", "B", "A", "COM", "INP", "PIE", "T20", "PT", "RSE", "RET", "SIM", "SLOT", "TID252", "ARG", "PTH", "I", "C90", "N", "E", "W", "D201", "D202", "D419", "F", "PL", "PLE", "PLR", "PLW", "RUF", "TRY201", "TRY400", "TRY401"]
|
|
48
|
+
format.quote-style = "single"
|
|
49
|
+
|
|
50
|
+
[project.urls]
|
|
51
|
+
'Source' = 'https://github.com/mutating/getsources'
|
|
52
|
+
'Tracker' = 'https://github.com/mutating/getsources/issues'
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from io import StringIO
|
|
3
|
+
from os import environ
|
|
4
|
+
from sys import platform
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from getsources import getsource
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_usual_functions():
|
|
12
|
+
def function_1():
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
def function_2(a, b):
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
assert getsource(function_1).splitlines() == [' def function_1():', ' pass']
|
|
19
|
+
assert getsource(function_2).splitlines() == [' def function_2(a, b):', ' pass']
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_lambda():
|
|
23
|
+
function = lambda x: x
|
|
24
|
+
|
|
25
|
+
assert getsource(function).strip() == 'function = lambda x: x'
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_usual_methods():
|
|
29
|
+
class A:
|
|
30
|
+
def method(self):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
class B:
|
|
34
|
+
def method(self, a, b):
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
assert getsource(A().method).splitlines() == [' def method(self):', ' pass']
|
|
38
|
+
assert getsource(B().method).splitlines() == [' def method(self, a, b):', ' pass']
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_usual_classmethods():
|
|
42
|
+
class A:
|
|
43
|
+
@classmethod
|
|
44
|
+
def method(cls):
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
class B:
|
|
48
|
+
@classmethod
|
|
49
|
+
def method(cls, a, b):
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
assert getsource(A().method).splitlines() == [' @classmethod', ' def method(cls):', ' pass']
|
|
53
|
+
assert getsource(B().method).splitlines() == [' @classmethod', ' def method(cls, a, b):', ' pass']
|
|
54
|
+
assert getsource(A.method).splitlines() == [' @classmethod', ' def method(cls):', ' pass']
|
|
55
|
+
assert getsource(B.method).splitlines() == [' @classmethod', ' def method(cls, a, b):', ' pass']
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def test_usual_staticmethods():
|
|
59
|
+
class A:
|
|
60
|
+
@staticmethod
|
|
61
|
+
def method():
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
class B:
|
|
65
|
+
@staticmethod
|
|
66
|
+
def method(a, b):
|
|
67
|
+
pass
|
|
68
|
+
|
|
69
|
+
assert getsource(A().method).splitlines() == [' @staticmethod', ' def method():', ' pass']
|
|
70
|
+
assert getsource(B().method).splitlines() == [' @staticmethod', ' def method(a, b):', ' pass']
|
|
71
|
+
assert getsource(A.method).splitlines() == [' @staticmethod', ' def method():', ' pass']
|
|
72
|
+
assert getsource(B.method).splitlines() == [' @staticmethod', ' def method(a, b):', ' pass']
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
@pytest.mark.skipif(platform == "win32", reason='I wait this: https://github.com/raczben/wexpect/issues/55')
|
|
76
|
+
def test_usual_functions_in_REPL(): # noqa: N802
|
|
77
|
+
from pexpect import spawn # type: ignore[import-untyped] # noqa: PLC0415
|
|
78
|
+
|
|
79
|
+
env = environ.copy()
|
|
80
|
+
env["PYTHON_COLORS"] = "0"
|
|
81
|
+
child = spawn('python3', ["-i"], encoding="utf-8", env=env, timeout=5)
|
|
82
|
+
|
|
83
|
+
buffer = StringIO()
|
|
84
|
+
child.logfile = buffer
|
|
85
|
+
|
|
86
|
+
child.expect(">>> ")
|
|
87
|
+
child.sendline('from getsources import getsource')
|
|
88
|
+
child.expect(">>> ")
|
|
89
|
+
child.sendline('def function(): ...')
|
|
90
|
+
child.sendline('')
|
|
91
|
+
child.expect(">>> ")
|
|
92
|
+
|
|
93
|
+
before = buffer.getvalue()
|
|
94
|
+
|
|
95
|
+
child.sendline("print(getsource(function), end='')")
|
|
96
|
+
child.expect(">>> ")
|
|
97
|
+
|
|
98
|
+
after = buffer.getvalue()
|
|
99
|
+
after = re.compile(r'(?:\x1B[@-_]|\x9B)[0-?]*[ -/]*[@-~]').sub('', after.lstrip(before))
|
|
100
|
+
after = ''.join(ch for ch in after if ch >= ' ' or ch in '\n\r\t')
|
|
101
|
+
after = after.splitlines()
|
|
102
|
+
|
|
103
|
+
child.sendline("exit()")
|
|
104
|
+
|
|
105
|
+
assert any('def function(): ...' in x for x in after)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
@pytest.mark.skipif(platform == "win32", reason='I wait this: https://github.com/raczben/wexpect/issues/55')
|
|
109
|
+
def test_lambda_in_REPL(): # noqa: N802
|
|
110
|
+
from pexpect import spawn # type: ignore[import-untyped] # noqa: PLC0415
|
|
111
|
+
|
|
112
|
+
env = environ.copy()
|
|
113
|
+
env["PYTHON_COLORS"] = "0"
|
|
114
|
+
child = spawn('python3', ["-i"], encoding="utf-8", env=env, timeout=5)
|
|
115
|
+
|
|
116
|
+
buffer = StringIO()
|
|
117
|
+
child.logfile = buffer
|
|
118
|
+
|
|
119
|
+
child.expect(">>> ")
|
|
120
|
+
child.sendline('from getsources import getsource')
|
|
121
|
+
child.expect(">>> ")
|
|
122
|
+
child.sendline('function = lambda x: x')
|
|
123
|
+
child.expect(">>> ")
|
|
124
|
+
|
|
125
|
+
before = buffer.getvalue()
|
|
126
|
+
|
|
127
|
+
child.sendline("print(getsource(function), end='')")
|
|
128
|
+
child.expect(">>> ")
|
|
129
|
+
|
|
130
|
+
after = buffer.getvalue()
|
|
131
|
+
after = after[len(before):]
|
|
132
|
+
after = after.splitlines()
|
|
133
|
+
|
|
134
|
+
child.sendline("exit()")
|
|
135
|
+
|
|
136
|
+
assert any('function = lambda x: x' in x for x in after)
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from io import StringIO
|
|
3
|
+
from os import environ
|
|
4
|
+
from sys import platform
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from getsources import getclearsource
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def global_function_1():
|
|
12
|
+
...
|
|
13
|
+
|
|
14
|
+
def global_function_2(a, b):
|
|
15
|
+
...
|
|
16
|
+
|
|
17
|
+
global_function_3 = lambda x: x
|
|
18
|
+
|
|
19
|
+
class GlobalClass:
|
|
20
|
+
def simple_method(self):
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
def method_with_parameters(self, a, b):
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
@classmethod
|
|
27
|
+
def class_method(cls, a, b):
|
|
28
|
+
pass
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def static_method(a, b):
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_usual_functions():
|
|
36
|
+
def function_1():
|
|
37
|
+
pass
|
|
38
|
+
|
|
39
|
+
def function_2(a, b):
|
|
40
|
+
pass
|
|
41
|
+
|
|
42
|
+
assert getclearsource(function_1).splitlines() == ['def function_1():', ' pass']
|
|
43
|
+
assert getclearsource(function_2).splitlines() == ['def function_2(a, b):', ' pass']
|
|
44
|
+
|
|
45
|
+
assert getclearsource(global_function_1).splitlines() == ['def global_function_1():', ' ...']
|
|
46
|
+
assert getclearsource(global_function_2).splitlines() == ['def global_function_2(a, b):', ' ...']
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_lambda():
|
|
50
|
+
function = lambda x: x
|
|
51
|
+
|
|
52
|
+
assert getclearsource(function) == 'function = lambda x: x'
|
|
53
|
+
assert getclearsource(global_function_3) == 'global_function_3 = lambda x: x'
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def test_usual_methods():
|
|
57
|
+
class A:
|
|
58
|
+
def method(self):
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
class B:
|
|
62
|
+
def method(self, a, b):
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
assert getclearsource(A().method).splitlines() == ['def method(self):', ' pass']
|
|
66
|
+
assert getclearsource(B().method).splitlines() == ['def method(self, a, b):', ' pass']
|
|
67
|
+
|
|
68
|
+
assert getclearsource(GlobalClass().simple_method).splitlines() == ['def simple_method(self):', ' pass']
|
|
69
|
+
assert getclearsource(GlobalClass().method_with_parameters).splitlines() == ['def method_with_parameters(self, a, b):', ' pass']
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_usual_classmethods():
|
|
73
|
+
class A:
|
|
74
|
+
@classmethod
|
|
75
|
+
def method(cls):
|
|
76
|
+
pass
|
|
77
|
+
|
|
78
|
+
class B:
|
|
79
|
+
@classmethod
|
|
80
|
+
def method(cls, a, b):
|
|
81
|
+
pass
|
|
82
|
+
|
|
83
|
+
assert getclearsource(A().method).splitlines() == ['@classmethod', 'def method(cls):', ' pass']
|
|
84
|
+
assert getclearsource(B().method).splitlines() == ['@classmethod', 'def method(cls, a, b):', ' pass']
|
|
85
|
+
assert getclearsource(A.method).splitlines() == ['@classmethod', 'def method(cls):', ' pass']
|
|
86
|
+
assert getclearsource(B.method).splitlines() == ['@classmethod', 'def method(cls, a, b):', ' pass']
|
|
87
|
+
|
|
88
|
+
assert getclearsource(GlobalClass().class_method).splitlines() == ['@classmethod', 'def class_method(cls, a, b):', ' pass']
|
|
89
|
+
assert getclearsource(GlobalClass.class_method).splitlines() == ['@classmethod', 'def class_method(cls, a, b):', ' pass']
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def test_usual_staticmethods():
|
|
93
|
+
class A:
|
|
94
|
+
@staticmethod
|
|
95
|
+
def method():
|
|
96
|
+
pass
|
|
97
|
+
|
|
98
|
+
class B:
|
|
99
|
+
@staticmethod
|
|
100
|
+
def method(a, b):
|
|
101
|
+
pass
|
|
102
|
+
|
|
103
|
+
assert getclearsource(A().method).splitlines() == ['@staticmethod', 'def method():', ' pass']
|
|
104
|
+
assert getclearsource(B().method).splitlines() == ['@staticmethod', 'def method(a, b):', ' pass']
|
|
105
|
+
|
|
106
|
+
assert getclearsource(A.method).splitlines() == ['@staticmethod', 'def method():', ' pass']
|
|
107
|
+
assert getclearsource(B.method).splitlines() == ['@staticmethod', 'def method(a, b):', ' pass']
|
|
108
|
+
|
|
109
|
+
assert getclearsource(GlobalClass().static_method).splitlines() == ['@staticmethod', 'def static_method(a, b):', ' pass']
|
|
110
|
+
assert getclearsource(GlobalClass.static_method).splitlines() == ['@staticmethod', 'def static_method(a, b):', ' pass']
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@pytest.mark.skipif(platform == "win32", reason='I wait this: https://github.com/raczben/wexpect/issues/55')
|
|
114
|
+
def test_usual_functions_in_REPL(): # noqa: N802
|
|
115
|
+
from pexpect import spawn # type: ignore[import-untyped] # noqa: PLC0415
|
|
116
|
+
|
|
117
|
+
env = environ.copy()
|
|
118
|
+
env["PYTHON_COLORS"] = "0"
|
|
119
|
+
child = spawn('python3', ["-i"], encoding="utf-8", env=env, timeout=5)
|
|
120
|
+
|
|
121
|
+
buffer = StringIO()
|
|
122
|
+
child.logfile = buffer
|
|
123
|
+
|
|
124
|
+
child.expect(">>> ")
|
|
125
|
+
child.sendline('from getsources import getclearsource')
|
|
126
|
+
child.expect(">>> ")
|
|
127
|
+
child.sendline('def function(): ...')
|
|
128
|
+
child.sendline('')
|
|
129
|
+
child.expect(">>> ")
|
|
130
|
+
|
|
131
|
+
before = buffer.getvalue()
|
|
132
|
+
|
|
133
|
+
child.sendline("print(getclearsource(function), end='')")
|
|
134
|
+
child.expect(">>> ")
|
|
135
|
+
|
|
136
|
+
after = buffer.getvalue()
|
|
137
|
+
after = re.compile(r'(?:\x1B[@-_]|\x9B)[0-?]*[ -/]*[@-~]').sub('', after.lstrip(before))
|
|
138
|
+
after = ''.join(ch for ch in after if ch >= ' ' or ch in '\n\r\t')
|
|
139
|
+
after = after.splitlines()
|
|
140
|
+
|
|
141
|
+
child.sendline("exit()")
|
|
142
|
+
|
|
143
|
+
assert any('def function(): ...' in x for x in after)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@pytest.mark.skipif(platform == "win32", reason='I wait this: https://github.com/raczben/wexpect/issues/55')
|
|
147
|
+
def test_lambda_in_REPL(): # noqa: N802
|
|
148
|
+
from pexpect import spawn # type: ignore[import-untyped] # noqa: PLC0415
|
|
149
|
+
|
|
150
|
+
env = environ.copy()
|
|
151
|
+
env["PYTHON_COLORS"] = "0"
|
|
152
|
+
child = spawn('python3', ["-i"], encoding="utf-8", env=env, timeout=5)
|
|
153
|
+
|
|
154
|
+
buffer = StringIO()
|
|
155
|
+
child.logfile = buffer
|
|
156
|
+
|
|
157
|
+
child.expect(">>> ")
|
|
158
|
+
child.sendline('from getsources import getclearsource')
|
|
159
|
+
child.expect(">>> ")
|
|
160
|
+
child.sendline('function = lambda x: x')
|
|
161
|
+
child.expect(">>> ")
|
|
162
|
+
|
|
163
|
+
before = buffer.getvalue()
|
|
164
|
+
|
|
165
|
+
child.sendline("print(getclearsource(function), end='')")
|
|
166
|
+
child.expect(">>> ")
|
|
167
|
+
|
|
168
|
+
after = buffer.getvalue()
|
|
169
|
+
after = after[len(before):]
|
|
170
|
+
after = after.splitlines()
|
|
171
|
+
|
|
172
|
+
child.sendline("exit()")
|
|
173
|
+
|
|
174
|
+
assert any('function = lambda x: x' in x for x in after)
|