Upload folder using huggingface_hub
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +23 -0
- Cache/001.png/3ed52a7265db8c5d/meta.json +3 -0
- Cache/001.png/3ed52a7265db8c5d/output.png +3 -0
- Cache/001.png/fa661d22fd83f746/meta.json +3 -0
- Cache/001.png/fa661d22fd83f746/output.png +3 -0
- Cache/002.png/3ed52a7265db8c5d/meta.json +3 -0
- Cache/002.png/3ed52a7265db8c5d/output.png +3 -0
- Cache/002.png/fa661d22fd83f746/meta.json +3 -0
- Cache/002.png/fa661d22fd83f746/output.png +3 -0
- Cache/003.png/3ed52a7265db8c5d/meta.json +3 -0
- Cache/003.png/3ed52a7265db8c5d/output.png +3 -0
- Cache/003.png/fa661d22fd83f746/meta.json +3 -0
- Cache/003.png/fa661d22fd83f746/output.png +3 -0
- Cache/004.png/3ed52a7265db8c5d/meta.json +3 -0
- Cache/004.png/3ed52a7265db8c5d/output.png +3 -0
- Cache/004.png/fa661d22fd83f746/meta.json +3 -0
- Cache/004.png/fa661d22fd83f746/output.png +3 -0
- Cache/005.png/3ed52a7265db8c5d/meta.json +3 -0
- Cache/005.png/3ed52a7265db8c5d/output.png +3 -0
- Cache/005.png/fa661d22fd83f746/meta.json +3 -0
- Cache/005.png/fa661d22fd83f746/output.png +3 -0
- Cache/tmp/001.png_3ed52a7265db8c5d/out.png +3 -0
- Cache/tmp/001.png_fa661d22fd83f746/out.png +3 -0
- Cache/tmp/002.png_3ed52a7265db8c5d/out.png +3 -0
- Cache/tmp/002.png_fa661d22fd83f746/out.png +3 -0
- Cache/tmp/003.png_3ed52a7265db8c5d/out.png +3 -0
- Cache/tmp/003.png_fa661d22fd83f746/out.png +3 -0
- Cache/tmp/004.png_3ed52a7265db8c5d/out.png +3 -0
- Cache/tmp/004.png_fa661d22fd83f746/out.png +3 -0
- Cache/tmp/005.png_3ed52a7265db8c5d/out.png +3 -0
- Cache/tmp/005.png_fa661d22fd83f746/out.png +3 -0
- LQ/001.png +3 -0
- LQ/002.png +3 -0
- LQ/003.png +3 -0
- LQ/004.png +3 -0
- LQ/005.png +3 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/BuildExecutable.py +170 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Cythonize.py +255 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Dependencies.py +1380 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Distutils.py +1 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Inline.py +372 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/IpythonMagic.py +572 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestCyCache.py +119 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestCythonizeArgsParser.py +482 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestDependencies.py +142 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestInline.py +112 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestIpythonMagic.py +295 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestRecythonize.py +212 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestStripLiterals.py +56 -0
- Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/__init__.py +1 -0
.gitattributes
CHANGED
|
@@ -57,3 +57,26 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 57 |
# Video files - compressed
|
| 58 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 59 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
# Video files - compressed
|
| 58 |
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 59 |
*.webm filter=lfs diff=lfs merge=lfs -text
|
| 60 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Compiler/Code.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 61 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Compiler/FlowControl.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 62 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Compiler/FusedNode.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 63 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Compiler/Parsing.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 64 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Compiler/Scanning.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 65 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Compiler/Visitor.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 66 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Plex/DFA.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 67 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Plex/Machines.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 68 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Plex/Scanners.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 69 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Plex/Transitions.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 70 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Tempita/_tempita.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 71 |
+
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Utils.cpython-37m-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 72 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Compiler/Code.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 73 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Compiler/FlowControl.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 74 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Compiler/FusedNode.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 75 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Compiler/Parsing.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 76 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Compiler/Scanning.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 77 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Compiler/Visitor.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 78 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Plex/Machines.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 79 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Plex/Scanners.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 80 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Plex/Transitions.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 81 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Tempita/_tempita.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
| 82 |
+
Restormer/.eggs/cython-3.1.3-py3.9-linux-x86_64.egg/Cython/Utils.cpython-39-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
Cache/001.png/3ed52a7265db8c5d/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9fde94852a18b664034502dfc9b78af19e6c7c985cb8341995fdc2810cfa68e9
|
| 3 |
+
size 114
|
Cache/001.png/3ed52a7265db8c5d/output.png
ADDED
|
Git LFS Details
|
Cache/001.png/fa661d22fd83f746/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:890af9cd993f7e202d9e1b5f1f9ac406e24e86a003663d260e66325993e36f70
|
| 3 |
+
size 69
|
Cache/001.png/fa661d22fd83f746/output.png
ADDED
|
Git LFS Details
|
Cache/002.png/3ed52a7265db8c5d/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:17c0ef5832d0963908b8925402264c54c5841bd7e6af01204e5962de437a0e07
|
| 3 |
+
size 114
|
Cache/002.png/3ed52a7265db8c5d/output.png
ADDED
|
Git LFS Details
|
Cache/002.png/fa661d22fd83f746/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4249f054b0cf21346ea66843f55e120ec3a3c06ef541646cbe84475e84bba3c6
|
| 3 |
+
size 69
|
Cache/002.png/fa661d22fd83f746/output.png
ADDED
|
Git LFS Details
|
Cache/003.png/3ed52a7265db8c5d/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a95c786768bf8d6c5520ecacbb951e83a68644ed26e9a3efca84658e14644b7f
|
| 3 |
+
size 114
|
Cache/003.png/3ed52a7265db8c5d/output.png
ADDED
|
Git LFS Details
|
Cache/003.png/fa661d22fd83f746/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5609420158a43bd068e9018702655e23f40aee303b4e7c9cf38cda4c04851c72
|
| 3 |
+
size 69
|
Cache/003.png/fa661d22fd83f746/output.png
ADDED
|
Git LFS Details
|
Cache/004.png/3ed52a7265db8c5d/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e65435393ed67066d5e39faafc831ef4c7c0ea340fd9dcfef91454a450b6e426
|
| 3 |
+
size 114
|
Cache/004.png/3ed52a7265db8c5d/output.png
ADDED
|
Git LFS Details
|
Cache/004.png/fa661d22fd83f746/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:59df23f10027b311aed657d6cd66be59a2c9dc84b7014abc9ec583fdf989cea4
|
| 3 |
+
size 69
|
Cache/004.png/fa661d22fd83f746/output.png
ADDED
|
Git LFS Details
|
Cache/005.png/3ed52a7265db8c5d/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b91d9b9c88e897908dbbedfbc33ead4c3e241b2ef1ed32f7de1ebe3ab5d7d513
|
| 3 |
+
size 114
|
Cache/005.png/3ed52a7265db8c5d/output.png
ADDED
|
Git LFS Details
|
Cache/005.png/fa661d22fd83f746/meta.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a4d9f72e6fff0f22f5209e4577baa3102573af0280ad1c086e8b6a6077c045f8
|
| 3 |
+
size 69
|
Cache/005.png/fa661d22fd83f746/output.png
ADDED
|
Git LFS Details
|
Cache/tmp/001.png_3ed52a7265db8c5d/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/001.png_fa661d22fd83f746/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/002.png_3ed52a7265db8c5d/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/002.png_fa661d22fd83f746/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/003.png_3ed52a7265db8c5d/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/003.png_fa661d22fd83f746/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/004.png_3ed52a7265db8c5d/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/004.png_fa661d22fd83f746/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/005.png_3ed52a7265db8c5d/out.png
ADDED
|
Git LFS Details
|
Cache/tmp/005.png_fa661d22fd83f746/out.png
ADDED
|
Git LFS Details
|
LQ/001.png
ADDED
|
Git LFS Details
|
LQ/002.png
ADDED
|
Git LFS Details
|
LQ/003.png
ADDED
|
Git LFS Details
|
LQ/004.png
ADDED
|
Git LFS Details
|
LQ/005.png
ADDED
|
Git LFS Details
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/BuildExecutable.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Compile a Python script into an executable that embeds CPython.
|
| 3 |
+
Requires CPython to be built as a shared library ('libpythonX.Y').
|
| 4 |
+
|
| 5 |
+
Basic usage:
|
| 6 |
+
|
| 7 |
+
python -m Cython.Build.BuildExecutable [ARGS] somefile.py
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import absolute_import
|
| 11 |
+
|
| 12 |
+
DEBUG = True
|
| 13 |
+
|
| 14 |
+
import sys
|
| 15 |
+
import os
|
| 16 |
+
if sys.version_info < (3, 9):
|
| 17 |
+
from distutils import sysconfig as _sysconfig
|
| 18 |
+
|
| 19 |
+
class sysconfig(object):
|
| 20 |
+
|
| 21 |
+
@staticmethod
|
| 22 |
+
def get_path(name):
|
| 23 |
+
assert name == 'include'
|
| 24 |
+
return _sysconfig.get_python_inc()
|
| 25 |
+
|
| 26 |
+
get_config_var = staticmethod(_sysconfig.get_config_var)
|
| 27 |
+
else:
|
| 28 |
+
# sysconfig can be trusted from cpython >= 3.8.7
|
| 29 |
+
import sysconfig
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def get_config_var(name, default=''):
|
| 33 |
+
return sysconfig.get_config_var(name) or default
|
| 34 |
+
|
| 35 |
+
INCDIR = sysconfig.get_path('include')
|
| 36 |
+
LIBDIR1 = get_config_var('LIBDIR')
|
| 37 |
+
LIBDIR2 = get_config_var('LIBPL')
|
| 38 |
+
PYLIB = get_config_var('LIBRARY')
|
| 39 |
+
PYLIB_DYN = get_config_var('LDLIBRARY')
|
| 40 |
+
if PYLIB_DYN == PYLIB:
|
| 41 |
+
# no shared library
|
| 42 |
+
PYLIB_DYN = ''
|
| 43 |
+
else:
|
| 44 |
+
PYLIB_DYN = os.path.splitext(PYLIB_DYN[3:])[0] # 'lib(XYZ).so' -> XYZ
|
| 45 |
+
|
| 46 |
+
CC = get_config_var('CC', os.environ.get('CC', ''))
|
| 47 |
+
CFLAGS = get_config_var('CFLAGS') + ' ' + os.environ.get('CFLAGS', '')
|
| 48 |
+
LINKCC = get_config_var('LINKCC', os.environ.get('LINKCC', CC))
|
| 49 |
+
LINKFORSHARED = get_config_var('LINKFORSHARED')
|
| 50 |
+
LIBS = get_config_var('LIBS')
|
| 51 |
+
SYSLIBS = get_config_var('SYSLIBS')
|
| 52 |
+
EXE_EXT = sysconfig.get_config_var('EXE')
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _debug(msg, *args):
|
| 56 |
+
if DEBUG:
|
| 57 |
+
if args:
|
| 58 |
+
msg = msg % args
|
| 59 |
+
sys.stderr.write(msg + '\n')
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def dump_config():
|
| 63 |
+
_debug('INCDIR: %s', INCDIR)
|
| 64 |
+
_debug('LIBDIR1: %s', LIBDIR1)
|
| 65 |
+
_debug('LIBDIR2: %s', LIBDIR2)
|
| 66 |
+
_debug('PYLIB: %s', PYLIB)
|
| 67 |
+
_debug('PYLIB_DYN: %s', PYLIB_DYN)
|
| 68 |
+
_debug('CC: %s', CC)
|
| 69 |
+
_debug('CFLAGS: %s', CFLAGS)
|
| 70 |
+
_debug('LINKCC: %s', LINKCC)
|
| 71 |
+
_debug('LINKFORSHARED: %s', LINKFORSHARED)
|
| 72 |
+
_debug('LIBS: %s', LIBS)
|
| 73 |
+
_debug('SYSLIBS: %s', SYSLIBS)
|
| 74 |
+
_debug('EXE_EXT: %s', EXE_EXT)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def _parse_args(args):
|
| 78 |
+
cy_args = []
|
| 79 |
+
last_arg = None
|
| 80 |
+
for i, arg in enumerate(args):
|
| 81 |
+
if arg.startswith('-'):
|
| 82 |
+
cy_args.append(arg)
|
| 83 |
+
elif last_arg in ('-X', '--directive'):
|
| 84 |
+
cy_args.append(arg)
|
| 85 |
+
else:
|
| 86 |
+
input_file = arg
|
| 87 |
+
args = args[i+1:]
|
| 88 |
+
break
|
| 89 |
+
last_arg = arg
|
| 90 |
+
else:
|
| 91 |
+
raise ValueError('no input file provided')
|
| 92 |
+
|
| 93 |
+
return input_file, cy_args, args
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def runcmd(cmd, shell=True):
|
| 97 |
+
if shell:
|
| 98 |
+
cmd = ' '.join(cmd)
|
| 99 |
+
_debug(cmd)
|
| 100 |
+
else:
|
| 101 |
+
_debug(' '.join(cmd))
|
| 102 |
+
|
| 103 |
+
import subprocess
|
| 104 |
+
returncode = subprocess.call(cmd, shell=shell)
|
| 105 |
+
|
| 106 |
+
if returncode:
|
| 107 |
+
sys.exit(returncode)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def clink(basename):
|
| 111 |
+
runcmd([LINKCC, '-o', basename + EXE_EXT, basename+'.o', '-L'+LIBDIR1, '-L'+LIBDIR2]
|
| 112 |
+
+ [PYLIB_DYN and ('-l'+PYLIB_DYN) or os.path.join(LIBDIR1, PYLIB)]
|
| 113 |
+
+ LIBS.split() + SYSLIBS.split() + LINKFORSHARED.split())
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def ccompile(basename):
|
| 117 |
+
runcmd([CC, '-c', '-o', basename+'.o', basename+'.c', '-I' + INCDIR] + CFLAGS.split())
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def cycompile(input_file, options=()):
|
| 121 |
+
from ..Compiler import Version, CmdLine, Main
|
| 122 |
+
options, sources = CmdLine.parse_command_line(list(options or ()) + ['--embed', input_file])
|
| 123 |
+
_debug('Using Cython %s to compile %s', Version.version, input_file)
|
| 124 |
+
result = Main.compile(sources, options)
|
| 125 |
+
if result.num_errors > 0:
|
| 126 |
+
sys.exit(1)
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def exec_file(program_name, args=()):
|
| 130 |
+
runcmd([os.path.abspath(program_name)] + list(args), shell=False)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def build(input_file, compiler_args=(), force=False):
|
| 134 |
+
"""
|
| 135 |
+
Build an executable program from a Cython module.
|
| 136 |
+
|
| 137 |
+
Returns the name of the executable file.
|
| 138 |
+
"""
|
| 139 |
+
basename = os.path.splitext(input_file)[0]
|
| 140 |
+
exe_file = basename + EXE_EXT
|
| 141 |
+
if not force and os.path.abspath(exe_file) == os.path.abspath(input_file):
|
| 142 |
+
raise ValueError("Input and output file names are the same, refusing to overwrite")
|
| 143 |
+
if (not force and os.path.exists(exe_file) and os.path.exists(input_file)
|
| 144 |
+
and os.path.getmtime(input_file) <= os.path.getmtime(exe_file)):
|
| 145 |
+
_debug("File is up to date, not regenerating %s", exe_file)
|
| 146 |
+
return exe_file
|
| 147 |
+
cycompile(input_file, compiler_args)
|
| 148 |
+
ccompile(basename)
|
| 149 |
+
clink(basename)
|
| 150 |
+
return exe_file
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
def build_and_run(args):
|
| 154 |
+
"""
|
| 155 |
+
Build an executable program from a Cython module and run it.
|
| 156 |
+
|
| 157 |
+
Arguments after the module name will be passed verbatimly to the program.
|
| 158 |
+
"""
|
| 159 |
+
program_name, args = _build(args)
|
| 160 |
+
exec_file(program_name, args)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def _build(args):
|
| 164 |
+
input_file, cy_args, args = _parse_args(args)
|
| 165 |
+
program_name = build(input_file, cy_args)
|
| 166 |
+
return program_name, args
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
if __name__ == '__main__':
|
| 170 |
+
_build(sys.argv[1:])
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Cythonize.py
ADDED
|
@@ -0,0 +1,255 @@
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import absolute_import, print_function
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
from .Dependencies import cythonize, extended_iglob
|
| 8 |
+
from ..Utils import is_package_dir
|
| 9 |
+
from ..Compiler import Options
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
import multiprocessing
|
| 13 |
+
parallel_compiles = int(multiprocessing.cpu_count() * 1.5)
|
| 14 |
+
except ImportError:
|
| 15 |
+
multiprocessing = None
|
| 16 |
+
parallel_compiles = 0
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class _FakePool(object):
|
| 20 |
+
def map_async(self, func, args):
|
| 21 |
+
try:
|
| 22 |
+
from itertools import imap
|
| 23 |
+
except ImportError:
|
| 24 |
+
imap=map
|
| 25 |
+
for _ in imap(func, args):
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
def close(self):
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
def terminate(self):
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
def join(self):
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def find_package_base(path):
|
| 39 |
+
base_dir, package_path = os.path.split(path)
|
| 40 |
+
while is_package_dir(base_dir):
|
| 41 |
+
base_dir, parent = os.path.split(base_dir)
|
| 42 |
+
package_path = '%s/%s' % (parent, package_path)
|
| 43 |
+
return base_dir, package_path
|
| 44 |
+
|
| 45 |
+
def cython_compile(path_pattern, options):
|
| 46 |
+
all_paths = map(os.path.abspath, extended_iglob(path_pattern))
|
| 47 |
+
_cython_compile_files(all_paths, options)
|
| 48 |
+
|
| 49 |
+
def _cython_compile_files(all_paths, options):
|
| 50 |
+
pool = None
|
| 51 |
+
try:
|
| 52 |
+
for path in all_paths:
|
| 53 |
+
if options.build_inplace:
|
| 54 |
+
base_dir = path
|
| 55 |
+
while not os.path.isdir(base_dir) or is_package_dir(base_dir):
|
| 56 |
+
base_dir = os.path.dirname(base_dir)
|
| 57 |
+
else:
|
| 58 |
+
base_dir = None
|
| 59 |
+
|
| 60 |
+
if os.path.isdir(path):
|
| 61 |
+
# recursively compiling a package
|
| 62 |
+
paths = [os.path.join(path, '**', '*.{py,pyx}')]
|
| 63 |
+
else:
|
| 64 |
+
# assume it's a file(-like thing)
|
| 65 |
+
paths = [path]
|
| 66 |
+
|
| 67 |
+
ext_modules = cythonize(
|
| 68 |
+
paths,
|
| 69 |
+
nthreads=options.parallel,
|
| 70 |
+
exclude_failures=options.keep_going,
|
| 71 |
+
exclude=options.excludes,
|
| 72 |
+
compiler_directives=options.directives,
|
| 73 |
+
compile_time_env=options.compile_time_env,
|
| 74 |
+
force=options.force,
|
| 75 |
+
quiet=options.quiet,
|
| 76 |
+
depfile=options.depfile,
|
| 77 |
+
language=options.language,
|
| 78 |
+
**options.options)
|
| 79 |
+
|
| 80 |
+
if ext_modules and options.build:
|
| 81 |
+
if len(ext_modules) > 1 and options.parallel > 1:
|
| 82 |
+
if pool is None:
|
| 83 |
+
try:
|
| 84 |
+
pool = multiprocessing.Pool(options.parallel)
|
| 85 |
+
except OSError:
|
| 86 |
+
pool = _FakePool()
|
| 87 |
+
pool.map_async(run_distutils, [
|
| 88 |
+
(base_dir, [ext]) for ext in ext_modules])
|
| 89 |
+
else:
|
| 90 |
+
run_distutils((base_dir, ext_modules))
|
| 91 |
+
except:
|
| 92 |
+
if pool is not None:
|
| 93 |
+
pool.terminate()
|
| 94 |
+
raise
|
| 95 |
+
else:
|
| 96 |
+
if pool is not None:
|
| 97 |
+
pool.close()
|
| 98 |
+
pool.join()
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def run_distutils(args):
|
| 102 |
+
try:
|
| 103 |
+
from distutils.core import setup
|
| 104 |
+
except ImportError:
|
| 105 |
+
try:
|
| 106 |
+
from setuptools import setup
|
| 107 |
+
except ImportError:
|
| 108 |
+
raise ImportError("'distutils' is not available. Please install 'setuptools' for binary builds.")
|
| 109 |
+
|
| 110 |
+
base_dir, ext_modules = args
|
| 111 |
+
script_args = ['build_ext', '-i']
|
| 112 |
+
cwd = os.getcwd()
|
| 113 |
+
temp_dir = None
|
| 114 |
+
try:
|
| 115 |
+
if base_dir:
|
| 116 |
+
os.chdir(base_dir)
|
| 117 |
+
temp_dir = tempfile.mkdtemp(dir=base_dir)
|
| 118 |
+
script_args.extend(['--build-temp', temp_dir])
|
| 119 |
+
setup(
|
| 120 |
+
script_name='setup.py',
|
| 121 |
+
script_args=script_args,
|
| 122 |
+
ext_modules=ext_modules,
|
| 123 |
+
)
|
| 124 |
+
finally:
|
| 125 |
+
if base_dir:
|
| 126 |
+
os.chdir(cwd)
|
| 127 |
+
if temp_dir and os.path.isdir(temp_dir):
|
| 128 |
+
shutil.rmtree(temp_dir)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def create_args_parser():
|
| 132 |
+
from argparse import ArgumentParser, RawDescriptionHelpFormatter
|
| 133 |
+
from ..Compiler.CmdLine import ParseDirectivesAction, ParseOptionsAction, ParseCompileTimeEnvAction
|
| 134 |
+
|
| 135 |
+
parser = ArgumentParser(
|
| 136 |
+
formatter_class=RawDescriptionHelpFormatter,
|
| 137 |
+
epilog="""\
|
| 138 |
+
Environment variables:
|
| 139 |
+
CYTHON_FORCE_REGEN: if set to 1, forces cythonize to regenerate the output files regardless
|
| 140 |
+
of modification times and changes.
|
| 141 |
+
Environment variables accepted by setuptools are supported to configure the C compiler and build:
|
| 142 |
+
https://setuptools.pypa.io/en/latest/userguide/ext_modules.html#compiler-and-linker-options"""
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
parser.add_argument('-X', '--directive', metavar='NAME=VALUE,...',
|
| 146 |
+
dest='directives', default={}, type=str,
|
| 147 |
+
action=ParseDirectivesAction,
|
| 148 |
+
help='set a compiler directive')
|
| 149 |
+
parser.add_argument('-E', '--compile-time-env', metavar='NAME=VALUE,...',
|
| 150 |
+
dest='compile_time_env', default={}, type=str,
|
| 151 |
+
action=ParseCompileTimeEnvAction,
|
| 152 |
+
help='set a compile time environment variable')
|
| 153 |
+
parser.add_argument('-s', '--option', metavar='NAME=VALUE',
|
| 154 |
+
dest='options', default={}, type=str,
|
| 155 |
+
action=ParseOptionsAction,
|
| 156 |
+
help='set a cythonize option')
|
| 157 |
+
parser.add_argument('-2', dest='language_level', action='store_const', const=2, default=None,
|
| 158 |
+
help='use Python 2 syntax mode by default')
|
| 159 |
+
parser.add_argument('-3', dest='language_level', action='store_const', const=3,
|
| 160 |
+
help='use Python 3 syntax mode by default')
|
| 161 |
+
parser.add_argument('--3str', dest='language_level', action='store_const', const='3str',
|
| 162 |
+
help='use Python 3 syntax mode by default')
|
| 163 |
+
parser.add_argument('-+', '--cplus', dest='language', action='store_const', const='c++', default=None,
|
| 164 |
+
help='Compile as C++ rather than C')
|
| 165 |
+
parser.add_argument('-a', '--annotate', action='store_const', const='default', dest='annotate',
|
| 166 |
+
help='Produce a colorized HTML version of the source.')
|
| 167 |
+
parser.add_argument('--annotate-fullc', action='store_const', const='fullc', dest='annotate',
|
| 168 |
+
help='Produce a colorized HTML version of the source '
|
| 169 |
+
'which includes entire generated C/C++-code.')
|
| 170 |
+
parser.add_argument('-x', '--exclude', metavar='PATTERN', dest='excludes',
|
| 171 |
+
action='append', default=[],
|
| 172 |
+
help='exclude certain file patterns from the compilation')
|
| 173 |
+
|
| 174 |
+
parser.add_argument('-b', '--build', dest='build', action='store_true', default=None,
|
| 175 |
+
help='build extension modules using distutils/setuptools')
|
| 176 |
+
parser.add_argument('-i', '--inplace', dest='build_inplace', action='store_true', default=None,
|
| 177 |
+
help='build extension modules in place using distutils/setuptools (implies -b)')
|
| 178 |
+
parser.add_argument('-j', '--parallel', dest='parallel', metavar='N',
|
| 179 |
+
type=int, default=parallel_compiles,
|
| 180 |
+
help=('run builds in N parallel jobs (default: %d)' %
|
| 181 |
+
parallel_compiles or 1))
|
| 182 |
+
parser.add_argument('-f', '--force', dest='force', action='store_true', default=None,
|
| 183 |
+
help='force recompilation')
|
| 184 |
+
parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', default=None,
|
| 185 |
+
help='be less verbose during compilation')
|
| 186 |
+
|
| 187 |
+
parser.add_argument('--lenient', dest='lenient', action='store_true', default=None,
|
| 188 |
+
help='increase Python compatibility by ignoring some compile time errors')
|
| 189 |
+
parser.add_argument('-k', '--keep-going', dest='keep_going', action='store_true', default=None,
|
| 190 |
+
help='compile as much as possible, ignore compilation failures')
|
| 191 |
+
parser.add_argument('--no-docstrings', dest='no_docstrings', action='store_true', default=None,
|
| 192 |
+
help='strip docstrings')
|
| 193 |
+
parser.add_argument('-M', '--depfile', action='store_true', help='produce depfiles for the sources')
|
| 194 |
+
parser.add_argument('sources', nargs='*')
|
| 195 |
+
return parser
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
def parse_args_raw(parser, args):
|
| 199 |
+
options, unknown = parser.parse_known_args(args)
|
| 200 |
+
sources = options.sources
|
| 201 |
+
# if positional arguments were interspersed
|
| 202 |
+
# some of them are in unknown
|
| 203 |
+
for option in unknown:
|
| 204 |
+
if option.startswith('-'):
|
| 205 |
+
parser.error("unknown option "+option)
|
| 206 |
+
else:
|
| 207 |
+
sources.append(option)
|
| 208 |
+
del options.sources
|
| 209 |
+
return (options, sources)
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def parse_args(args):
|
| 213 |
+
parser = create_args_parser()
|
| 214 |
+
options, args = parse_args_raw(parser, args)
|
| 215 |
+
|
| 216 |
+
if not args:
|
| 217 |
+
parser.error("no source files provided")
|
| 218 |
+
if options.build_inplace:
|
| 219 |
+
options.build = True
|
| 220 |
+
if multiprocessing is None:
|
| 221 |
+
options.parallel = 0
|
| 222 |
+
if options.language_level:
|
| 223 |
+
assert options.language_level in (2, 3, '3str')
|
| 224 |
+
options.options['language_level'] = options.language_level
|
| 225 |
+
|
| 226 |
+
if options.lenient:
|
| 227 |
+
# increase Python compatibility by ignoring compile time errors
|
| 228 |
+
Options.error_on_unknown_names = False
|
| 229 |
+
Options.error_on_uninitialized = False
|
| 230 |
+
|
| 231 |
+
if options.annotate:
|
| 232 |
+
Options.annotate = options.annotate
|
| 233 |
+
|
| 234 |
+
if options.no_docstrings:
|
| 235 |
+
Options.docstrings = False
|
| 236 |
+
|
| 237 |
+
return options, args
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def main(args=None):
|
| 241 |
+
options, paths = parse_args(args)
|
| 242 |
+
|
| 243 |
+
all_paths = []
|
| 244 |
+
for path in paths:
|
| 245 |
+
expanded_path = [os.path.abspath(p) for p in extended_iglob(path)]
|
| 246 |
+
if not expanded_path:
|
| 247 |
+
import sys
|
| 248 |
+
print("{}: No such file or directory: '{}'".format(sys.argv[0], path), file=sys.stderr)
|
| 249 |
+
sys.exit(1)
|
| 250 |
+
all_paths.extend(expanded_path)
|
| 251 |
+
_cython_compile_files(all_paths, options)
|
| 252 |
+
|
| 253 |
+
|
| 254 |
+
if __name__ == '__main__':
|
| 255 |
+
main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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import absolute_import, print_function
|
| 2 |
+
|
| 3 |
+
import cython
|
| 4 |
+
from .. import __version__
|
| 5 |
+
|
| 6 |
+
import collections
|
| 7 |
+
import contextlib
|
| 8 |
+
import hashlib
|
| 9 |
+
import os
|
| 10 |
+
import shutil
|
| 11 |
+
import subprocess
|
| 12 |
+
import re, sys, time
|
| 13 |
+
from glob import iglob
|
| 14 |
+
from io import open as io_open
|
| 15 |
+
from os.path import relpath as _relpath
|
| 16 |
+
import zipfile
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
from collections.abc import Iterable
|
| 20 |
+
except ImportError:
|
| 21 |
+
from collections import Iterable
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
import gzip
|
| 25 |
+
gzip_open = gzip.open
|
| 26 |
+
gzip_ext = '.gz'
|
| 27 |
+
except ImportError:
|
| 28 |
+
gzip_open = open
|
| 29 |
+
gzip_ext = ''
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
import zlib
|
| 33 |
+
zipfile_compression_mode = zipfile.ZIP_DEFLATED
|
| 34 |
+
except ImportError:
|
| 35 |
+
zipfile_compression_mode = zipfile.ZIP_STORED
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
import pythran
|
| 39 |
+
except:
|
| 40 |
+
pythran = None
|
| 41 |
+
|
| 42 |
+
from .. import Utils
|
| 43 |
+
from ..Utils import (cached_function, cached_method, path_exists,
|
| 44 |
+
safe_makedirs, copy_file_to_dir_if_newer, is_package_dir, write_depfile)
|
| 45 |
+
from ..Compiler import Errors
|
| 46 |
+
from ..Compiler.Main import Context
|
| 47 |
+
from ..Compiler.Options import (CompilationOptions, default_options,
|
| 48 |
+
get_directive_defaults)
|
| 49 |
+
|
| 50 |
+
join_path = cached_function(os.path.join)
|
| 51 |
+
copy_once_if_newer = cached_function(copy_file_to_dir_if_newer)
|
| 52 |
+
safe_makedirs_once = cached_function(safe_makedirs)
|
| 53 |
+
|
| 54 |
+
if sys.version_info[0] < 3:
|
| 55 |
+
# stupid Py2 distutils enforces str type in list of sources
|
| 56 |
+
_fs_encoding = sys.getfilesystemencoding()
|
| 57 |
+
if _fs_encoding is None:
|
| 58 |
+
_fs_encoding = sys.getdefaultencoding()
|
| 59 |
+
def encode_filename_in_py2(filename):
|
| 60 |
+
if not isinstance(filename, bytes):
|
| 61 |
+
return filename.encode(_fs_encoding)
|
| 62 |
+
return filename
|
| 63 |
+
else:
|
| 64 |
+
def encode_filename_in_py2(filename):
|
| 65 |
+
return filename
|
| 66 |
+
basestring = str
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _make_relative(file_paths, base=None):
|
| 70 |
+
if not base:
|
| 71 |
+
base = os.getcwd()
|
| 72 |
+
if base[-1] != os.path.sep:
|
| 73 |
+
base += os.path.sep
|
| 74 |
+
return [_relpath(path, base) if path.startswith(base) else path
|
| 75 |
+
for path in file_paths]
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def extended_iglob(pattern):
|
| 79 |
+
if '{' in pattern:
|
| 80 |
+
m = re.match('(.*){([^}]+)}(.*)', pattern)
|
| 81 |
+
if m:
|
| 82 |
+
before, switch, after = m.groups()
|
| 83 |
+
for case in switch.split(','):
|
| 84 |
+
for path in extended_iglob(before + case + after):
|
| 85 |
+
yield path
|
| 86 |
+
return
|
| 87 |
+
|
| 88 |
+
# We always accept '/' and also '\' on Windows,
|
| 89 |
+
# because '/' is generally common for relative paths.
|
| 90 |
+
if '**/' in pattern or os.sep == '\\' and '**\\' in pattern:
|
| 91 |
+
seen = set()
|
| 92 |
+
first, rest = re.split(r'\*\*[%s]' % ('/\\\\' if os.sep == '\\' else '/'), pattern, 1)
|
| 93 |
+
if first:
|
| 94 |
+
first = iglob(first + os.sep)
|
| 95 |
+
else:
|
| 96 |
+
first = ['']
|
| 97 |
+
for root in first:
|
| 98 |
+
for path in extended_iglob(join_path(root, rest)):
|
| 99 |
+
if path not in seen:
|
| 100 |
+
seen.add(path)
|
| 101 |
+
yield path
|
| 102 |
+
for path in extended_iglob(join_path(root, '*', '**', rest)):
|
| 103 |
+
if path not in seen:
|
| 104 |
+
seen.add(path)
|
| 105 |
+
yield path
|
| 106 |
+
else:
|
| 107 |
+
for path in iglob(pattern):
|
| 108 |
+
yield path
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def nonempty(it, error_msg="expected non-empty iterator"):
|
| 112 |
+
empty = True
|
| 113 |
+
for value in it:
|
| 114 |
+
empty = False
|
| 115 |
+
yield value
|
| 116 |
+
if empty:
|
| 117 |
+
raise ValueError(error_msg)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
@cached_function
|
| 121 |
+
def file_hash(filename):
|
| 122 |
+
path = os.path.normpath(filename)
|
| 123 |
+
prefix = ('%d:%s' % (len(path), path)).encode("UTF-8")
|
| 124 |
+
m = hashlib.sha1(prefix)
|
| 125 |
+
with open(path, 'rb') as f:
|
| 126 |
+
data = f.read(65000)
|
| 127 |
+
while data:
|
| 128 |
+
m.update(data)
|
| 129 |
+
data = f.read(65000)
|
| 130 |
+
return m.hexdigest()
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def update_pythran_extension(ext):
|
| 134 |
+
if pythran is None:
|
| 135 |
+
raise RuntimeError("You first need to install Pythran to use the np_pythran directive.")
|
| 136 |
+
try:
|
| 137 |
+
pythran_ext = pythran.config.make_extension(python=True)
|
| 138 |
+
except TypeError: # older pythran version only
|
| 139 |
+
pythran_ext = pythran.config.make_extension()
|
| 140 |
+
|
| 141 |
+
ext.include_dirs.extend(pythran_ext['include_dirs'])
|
| 142 |
+
ext.extra_compile_args.extend(pythran_ext['extra_compile_args'])
|
| 143 |
+
ext.extra_link_args.extend(pythran_ext['extra_link_args'])
|
| 144 |
+
ext.define_macros.extend(pythran_ext['define_macros'])
|
| 145 |
+
ext.undef_macros.extend(pythran_ext['undef_macros'])
|
| 146 |
+
ext.library_dirs.extend(pythran_ext['library_dirs'])
|
| 147 |
+
ext.libraries.extend(pythran_ext['libraries'])
|
| 148 |
+
ext.language = 'c++'
|
| 149 |
+
|
| 150 |
+
# These options are not compatible with the way normal Cython extensions work
|
| 151 |
+
for bad_option in ["-fwhole-program", "-fvisibility=hidden"]:
|
| 152 |
+
try:
|
| 153 |
+
ext.extra_compile_args.remove(bad_option)
|
| 154 |
+
except ValueError:
|
| 155 |
+
pass
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def parse_list(s):
|
| 159 |
+
"""
|
| 160 |
+
>>> parse_list("")
|
| 161 |
+
[]
|
| 162 |
+
>>> parse_list("a")
|
| 163 |
+
['a']
|
| 164 |
+
>>> parse_list("a b c")
|
| 165 |
+
['a', 'b', 'c']
|
| 166 |
+
>>> parse_list("[a, b, c]")
|
| 167 |
+
['a', 'b', 'c']
|
| 168 |
+
>>> parse_list('a " " b')
|
| 169 |
+
['a', ' ', 'b']
|
| 170 |
+
>>> parse_list('[a, ",a", "a,", ",", ]')
|
| 171 |
+
['a', ',a', 'a,', ',']
|
| 172 |
+
"""
|
| 173 |
+
if len(s) >= 2 and s[0] == '[' and s[-1] == ']':
|
| 174 |
+
s = s[1:-1]
|
| 175 |
+
delimiter = ','
|
| 176 |
+
else:
|
| 177 |
+
delimiter = ' '
|
| 178 |
+
s, literals = strip_string_literals(s)
|
| 179 |
+
def unquote(literal):
|
| 180 |
+
literal = literal.strip()
|
| 181 |
+
if literal[0] in "'\"":
|
| 182 |
+
return literals[literal[1:-1]]
|
| 183 |
+
else:
|
| 184 |
+
return literal
|
| 185 |
+
return [unquote(item) for item in s.split(delimiter) if item.strip()]
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
transitive_str = object()
|
| 189 |
+
transitive_list = object()
|
| 190 |
+
bool_or = object()
|
| 191 |
+
|
| 192 |
+
distutils_settings = {
|
| 193 |
+
'name': str,
|
| 194 |
+
'sources': list,
|
| 195 |
+
'define_macros': list,
|
| 196 |
+
'undef_macros': list,
|
| 197 |
+
'libraries': transitive_list,
|
| 198 |
+
'library_dirs': transitive_list,
|
| 199 |
+
'runtime_library_dirs': transitive_list,
|
| 200 |
+
'include_dirs': transitive_list,
|
| 201 |
+
'extra_objects': list,
|
| 202 |
+
'extra_compile_args': transitive_list,
|
| 203 |
+
'extra_link_args': transitive_list,
|
| 204 |
+
'export_symbols': list,
|
| 205 |
+
'depends': transitive_list,
|
| 206 |
+
'language': transitive_str,
|
| 207 |
+
'np_pythran': bool_or
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def _legacy_strtobool(val):
|
| 212 |
+
# Used to be "distutils.util.strtobool", adapted for deprecation warnings.
|
| 213 |
+
if val == "True":
|
| 214 |
+
return True
|
| 215 |
+
elif val == "False":
|
| 216 |
+
return False
|
| 217 |
+
|
| 218 |
+
import warnings
|
| 219 |
+
warnings.warn("The 'np_python' option requires 'True' or 'False'", category=DeprecationWarning)
|
| 220 |
+
val = val.lower()
|
| 221 |
+
if val in ('y', 'yes', 't', 'true', 'on', '1'):
|
| 222 |
+
return True
|
| 223 |
+
elif val in ('n', 'no', 'f', 'false', 'off', '0'):
|
| 224 |
+
return False
|
| 225 |
+
else:
|
| 226 |
+
raise ValueError("invalid truth value %r" % (val,))
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
@cython.locals(start=cython.Py_ssize_t, end=cython.Py_ssize_t)
|
| 230 |
+
def line_iter(source):
|
| 231 |
+
if isinstance(source, basestring):
|
| 232 |
+
start = 0
|
| 233 |
+
while True:
|
| 234 |
+
end = source.find('\n', start)
|
| 235 |
+
if end == -1:
|
| 236 |
+
yield source[start:]
|
| 237 |
+
return
|
| 238 |
+
yield source[start:end]
|
| 239 |
+
start = end+1
|
| 240 |
+
else:
|
| 241 |
+
for line in source:
|
| 242 |
+
yield line
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
class DistutilsInfo(object):
|
| 246 |
+
|
| 247 |
+
def __init__(self, source=None, exn=None):
|
| 248 |
+
self.values = {}
|
| 249 |
+
if source is not None:
|
| 250 |
+
for line in line_iter(source):
|
| 251 |
+
line = line.lstrip()
|
| 252 |
+
if not line:
|
| 253 |
+
continue
|
| 254 |
+
if line[0] != '#':
|
| 255 |
+
break
|
| 256 |
+
line = line[1:].lstrip()
|
| 257 |
+
kind = next((k for k in ("distutils:","cython:") if line.startswith(k)), None)
|
| 258 |
+
if kind is not None:
|
| 259 |
+
key, _, value = [s.strip() for s in line[len(kind):].partition('=')]
|
| 260 |
+
type = distutils_settings.get(key, None)
|
| 261 |
+
if line.startswith("cython:") and type is None: continue
|
| 262 |
+
if type in (list, transitive_list):
|
| 263 |
+
value = parse_list(value)
|
| 264 |
+
if key == 'define_macros':
|
| 265 |
+
value = [tuple(macro.split('=', 1))
|
| 266 |
+
if '=' in macro else (macro, None)
|
| 267 |
+
for macro in value]
|
| 268 |
+
if type is bool_or:
|
| 269 |
+
value = _legacy_strtobool(value)
|
| 270 |
+
self.values[key] = value
|
| 271 |
+
elif exn is not None:
|
| 272 |
+
for key in distutils_settings:
|
| 273 |
+
if key in ('name', 'sources','np_pythran'):
|
| 274 |
+
continue
|
| 275 |
+
value = getattr(exn, key, None)
|
| 276 |
+
if value:
|
| 277 |
+
self.values[key] = value
|
| 278 |
+
|
| 279 |
+
def merge(self, other):
|
| 280 |
+
if other is None:
|
| 281 |
+
return self
|
| 282 |
+
for key, value in other.values.items():
|
| 283 |
+
type = distutils_settings[key]
|
| 284 |
+
if type is transitive_str and key not in self.values:
|
| 285 |
+
self.values[key] = value
|
| 286 |
+
elif type is transitive_list:
|
| 287 |
+
if key in self.values:
|
| 288 |
+
# Change a *copy* of the list (Trac #845)
|
| 289 |
+
all = self.values[key][:]
|
| 290 |
+
for v in value:
|
| 291 |
+
if v not in all:
|
| 292 |
+
all.append(v)
|
| 293 |
+
value = all
|
| 294 |
+
self.values[key] = value
|
| 295 |
+
elif type is bool_or:
|
| 296 |
+
self.values[key] = self.values.get(key, False) | value
|
| 297 |
+
return self
|
| 298 |
+
|
| 299 |
+
def subs(self, aliases):
|
| 300 |
+
if aliases is None:
|
| 301 |
+
return self
|
| 302 |
+
resolved = DistutilsInfo()
|
| 303 |
+
for key, value in self.values.items():
|
| 304 |
+
type = distutils_settings[key]
|
| 305 |
+
if type in [list, transitive_list]:
|
| 306 |
+
new_value_list = []
|
| 307 |
+
for v in value:
|
| 308 |
+
if v in aliases:
|
| 309 |
+
v = aliases[v]
|
| 310 |
+
if isinstance(v, list):
|
| 311 |
+
new_value_list += v
|
| 312 |
+
else:
|
| 313 |
+
new_value_list.append(v)
|
| 314 |
+
value = new_value_list
|
| 315 |
+
else:
|
| 316 |
+
if value in aliases:
|
| 317 |
+
value = aliases[value]
|
| 318 |
+
resolved.values[key] = value
|
| 319 |
+
return resolved
|
| 320 |
+
|
| 321 |
+
def apply(self, extension):
|
| 322 |
+
for key, value in self.values.items():
|
| 323 |
+
type = distutils_settings[key]
|
| 324 |
+
if type in [list, transitive_list]:
|
| 325 |
+
value = getattr(extension, key) + list(value)
|
| 326 |
+
setattr(extension, key, value)
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
@cython.locals(start=cython.Py_ssize_t, q=cython.Py_ssize_t,
|
| 330 |
+
single_q=cython.Py_ssize_t, double_q=cython.Py_ssize_t,
|
| 331 |
+
hash_mark=cython.Py_ssize_t, end=cython.Py_ssize_t,
|
| 332 |
+
k=cython.Py_ssize_t, counter=cython.Py_ssize_t, quote_len=cython.Py_ssize_t)
|
| 333 |
+
def strip_string_literals(code, prefix='__Pyx_L'):
|
| 334 |
+
"""
|
| 335 |
+
Normalizes every string literal to be of the form '__Pyx_Lxxx',
|
| 336 |
+
returning the normalized code and a mapping of labels to
|
| 337 |
+
string literals.
|
| 338 |
+
"""
|
| 339 |
+
new_code = []
|
| 340 |
+
literals = {}
|
| 341 |
+
counter = 0
|
| 342 |
+
start = q = 0
|
| 343 |
+
in_quote = False
|
| 344 |
+
hash_mark = single_q = double_q = -1
|
| 345 |
+
code_len = len(code)
|
| 346 |
+
quote_type = None
|
| 347 |
+
quote_len = -1
|
| 348 |
+
|
| 349 |
+
while True:
|
| 350 |
+
if hash_mark < q:
|
| 351 |
+
hash_mark = code.find('#', q)
|
| 352 |
+
if single_q < q:
|
| 353 |
+
single_q = code.find("'", q)
|
| 354 |
+
if double_q < q:
|
| 355 |
+
double_q = code.find('"', q)
|
| 356 |
+
q = min(single_q, double_q)
|
| 357 |
+
if q == -1:
|
| 358 |
+
q = max(single_q, double_q)
|
| 359 |
+
|
| 360 |
+
# We're done.
|
| 361 |
+
if q == -1 and hash_mark == -1:
|
| 362 |
+
new_code.append(code[start:])
|
| 363 |
+
break
|
| 364 |
+
|
| 365 |
+
# Try to close the quote.
|
| 366 |
+
elif in_quote:
|
| 367 |
+
if code[q-1] == u'\\':
|
| 368 |
+
k = 2
|
| 369 |
+
while q >= k and code[q-k] == u'\\':
|
| 370 |
+
k += 1
|
| 371 |
+
if k % 2 == 0:
|
| 372 |
+
q += 1
|
| 373 |
+
continue
|
| 374 |
+
if code[q] == quote_type and (
|
| 375 |
+
quote_len == 1 or (code_len > q + 2 and quote_type == code[q+1] == code[q+2])):
|
| 376 |
+
counter += 1
|
| 377 |
+
label = "%s%s_" % (prefix, counter)
|
| 378 |
+
literals[label] = code[start+quote_len:q]
|
| 379 |
+
full_quote = code[q:q+quote_len]
|
| 380 |
+
new_code.append(full_quote)
|
| 381 |
+
new_code.append(label)
|
| 382 |
+
new_code.append(full_quote)
|
| 383 |
+
q += quote_len
|
| 384 |
+
in_quote = False
|
| 385 |
+
start = q
|
| 386 |
+
else:
|
| 387 |
+
q += 1
|
| 388 |
+
|
| 389 |
+
# Process comment.
|
| 390 |
+
elif -1 != hash_mark and (hash_mark < q or q == -1):
|
| 391 |
+
new_code.append(code[start:hash_mark+1])
|
| 392 |
+
end = code.find('\n', hash_mark)
|
| 393 |
+
counter += 1
|
| 394 |
+
label = "%s%s_" % (prefix, counter)
|
| 395 |
+
if end == -1:
|
| 396 |
+
end_or_none = None
|
| 397 |
+
else:
|
| 398 |
+
end_or_none = end
|
| 399 |
+
literals[label] = code[hash_mark+1:end_or_none]
|
| 400 |
+
new_code.append(label)
|
| 401 |
+
if end == -1:
|
| 402 |
+
break
|
| 403 |
+
start = q = end
|
| 404 |
+
|
| 405 |
+
# Open the quote.
|
| 406 |
+
else:
|
| 407 |
+
if code_len >= q+3 and (code[q] == code[q+1] == code[q+2]):
|
| 408 |
+
quote_len = 3
|
| 409 |
+
else:
|
| 410 |
+
quote_len = 1
|
| 411 |
+
in_quote = True
|
| 412 |
+
quote_type = code[q]
|
| 413 |
+
new_code.append(code[start:q])
|
| 414 |
+
start = q
|
| 415 |
+
q += quote_len
|
| 416 |
+
|
| 417 |
+
return "".join(new_code), literals
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
# We need to allow spaces to allow for conditional compilation like
|
| 421 |
+
# IF ...:
|
| 422 |
+
# cimport ...
|
| 423 |
+
dependency_regex = re.compile(r"(?:^\s*from +([0-9a-zA-Z_.]+) +cimport)|"
|
| 424 |
+
r"(?:^\s*cimport +([0-9a-zA-Z_.]+(?: *, *[0-9a-zA-Z_.]+)*))|"
|
| 425 |
+
r"(?:^\s*cdef +extern +from +['\"]([^'\"]+)['\"])|"
|
| 426 |
+
r"(?:^\s*include +['\"]([^'\"]+)['\"])", re.M)
|
| 427 |
+
dependency_after_from_regex = re.compile(
|
| 428 |
+
r"(?:^\s+\(([0-9a-zA-Z_., ]*)\)[#\n])|"
|
| 429 |
+
r"(?:^\s+([0-9a-zA-Z_., ]*)[#\n])",
|
| 430 |
+
re.M)
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
def normalize_existing(base_path, rel_paths):
|
| 434 |
+
return normalize_existing0(os.path.dirname(base_path), tuple(set(rel_paths)))
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
@cached_function
|
| 438 |
+
def normalize_existing0(base_dir, rel_paths):
|
| 439 |
+
"""
|
| 440 |
+
Given some base directory ``base_dir`` and a list of path names
|
| 441 |
+
``rel_paths``, normalize each relative path name ``rel`` by
|
| 442 |
+
replacing it by ``os.path.join(base, rel)`` if that file exists.
|
| 443 |
+
|
| 444 |
+
Return a couple ``(normalized, needed_base)`` where ``normalized``
|
| 445 |
+
if the list of normalized file names and ``needed_base`` is
|
| 446 |
+
``base_dir`` if we actually needed ``base_dir``. If no paths were
|
| 447 |
+
changed (for example, if all paths were already absolute), then
|
| 448 |
+
``needed_base`` is ``None``.
|
| 449 |
+
"""
|
| 450 |
+
normalized = []
|
| 451 |
+
needed_base = None
|
| 452 |
+
for rel in rel_paths:
|
| 453 |
+
if os.path.isabs(rel):
|
| 454 |
+
normalized.append(rel)
|
| 455 |
+
continue
|
| 456 |
+
path = join_path(base_dir, rel)
|
| 457 |
+
if path_exists(path):
|
| 458 |
+
normalized.append(os.path.normpath(path))
|
| 459 |
+
needed_base = base_dir
|
| 460 |
+
else:
|
| 461 |
+
normalized.append(rel)
|
| 462 |
+
return (normalized, needed_base)
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def resolve_depends(depends, include_dirs):
|
| 466 |
+
include_dirs = tuple(include_dirs)
|
| 467 |
+
resolved = []
|
| 468 |
+
for depend in depends:
|
| 469 |
+
path = resolve_depend(depend, include_dirs)
|
| 470 |
+
if path is not None:
|
| 471 |
+
resolved.append(path)
|
| 472 |
+
return resolved
|
| 473 |
+
|
| 474 |
+
|
| 475 |
+
@cached_function
|
| 476 |
+
def resolve_depend(depend, include_dirs):
|
| 477 |
+
if depend[0] == '<' and depend[-1] == '>':
|
| 478 |
+
return None
|
| 479 |
+
for dir in include_dirs:
|
| 480 |
+
path = join_path(dir, depend)
|
| 481 |
+
if path_exists(path):
|
| 482 |
+
return os.path.normpath(path)
|
| 483 |
+
return None
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
@cached_function
|
| 487 |
+
def package(filename):
|
| 488 |
+
dir = os.path.dirname(os.path.abspath(str(filename)))
|
| 489 |
+
if dir != filename and is_package_dir(dir):
|
| 490 |
+
return package(dir) + (os.path.basename(dir),)
|
| 491 |
+
else:
|
| 492 |
+
return ()
|
| 493 |
+
|
| 494 |
+
|
| 495 |
+
@cached_function
|
| 496 |
+
def fully_qualified_name(filename):
|
| 497 |
+
module = os.path.splitext(os.path.basename(filename))[0]
|
| 498 |
+
return '.'.join(package(filename) + (module,))
|
| 499 |
+
|
| 500 |
+
|
| 501 |
+
@cached_function
|
| 502 |
+
def parse_dependencies(source_filename):
|
| 503 |
+
# Actual parsing is way too slow, so we use regular expressions.
|
| 504 |
+
# The only catch is that we must strip comments and string
|
| 505 |
+
# literals ahead of time.
|
| 506 |
+
with Utils.open_source_file(source_filename, error_handling='ignore') as fh:
|
| 507 |
+
source = fh.read()
|
| 508 |
+
distutils_info = DistutilsInfo(source)
|
| 509 |
+
source, literals = strip_string_literals(source)
|
| 510 |
+
source = source.replace('\\\n', ' ').replace('\t', ' ')
|
| 511 |
+
|
| 512 |
+
# TODO: pure mode
|
| 513 |
+
cimports = []
|
| 514 |
+
includes = []
|
| 515 |
+
externs = []
|
| 516 |
+
for m in dependency_regex.finditer(source):
|
| 517 |
+
cimport_from, cimport_list, extern, include = m.groups()
|
| 518 |
+
if cimport_from:
|
| 519 |
+
cimports.append(cimport_from)
|
| 520 |
+
m_after_from = dependency_after_from_regex.search(source, pos=m.end())
|
| 521 |
+
if m_after_from:
|
| 522 |
+
multiline, one_line = m_after_from.groups()
|
| 523 |
+
subimports = multiline or one_line
|
| 524 |
+
cimports.extend("{0}.{1}".format(cimport_from, s.strip())
|
| 525 |
+
for s in subimports.split(','))
|
| 526 |
+
|
| 527 |
+
elif cimport_list:
|
| 528 |
+
cimports.extend(x.strip() for x in cimport_list.split(","))
|
| 529 |
+
elif extern:
|
| 530 |
+
externs.append(literals[extern])
|
| 531 |
+
else:
|
| 532 |
+
includes.append(literals[include])
|
| 533 |
+
return cimports, includes, externs, distutils_info
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
class DependencyTree(object):
|
| 537 |
+
|
| 538 |
+
def __init__(self, context, quiet=False):
|
| 539 |
+
self.context = context
|
| 540 |
+
self.quiet = quiet
|
| 541 |
+
self._transitive_cache = {}
|
| 542 |
+
|
| 543 |
+
def parse_dependencies(self, source_filename):
|
| 544 |
+
if path_exists(source_filename):
|
| 545 |
+
source_filename = os.path.normpath(source_filename)
|
| 546 |
+
return parse_dependencies(source_filename)
|
| 547 |
+
|
| 548 |
+
@cached_method
|
| 549 |
+
def included_files(self, filename):
|
| 550 |
+
# This is messy because included files are textually included, resolving
|
| 551 |
+
# cimports (but not includes) relative to the including file.
|
| 552 |
+
all = set()
|
| 553 |
+
for include in self.parse_dependencies(filename)[1]:
|
| 554 |
+
include_path = join_path(os.path.dirname(filename), include)
|
| 555 |
+
if not path_exists(include_path):
|
| 556 |
+
include_path = self.context.find_include_file(include, source_file_path=filename)
|
| 557 |
+
if include_path:
|
| 558 |
+
if '.' + os.path.sep in include_path:
|
| 559 |
+
include_path = os.path.normpath(include_path)
|
| 560 |
+
all.add(include_path)
|
| 561 |
+
all.update(self.included_files(include_path))
|
| 562 |
+
elif not self.quiet:
|
| 563 |
+
print(u"Unable to locate '%s' referenced from '%s'" % (filename, include))
|
| 564 |
+
return all
|
| 565 |
+
|
| 566 |
+
@cached_method
|
| 567 |
+
def cimports_externs_incdirs(self, filename):
|
| 568 |
+
# This is really ugly. Nested cimports are resolved with respect to the
|
| 569 |
+
# includer, but includes are resolved with respect to the includee.
|
| 570 |
+
cimports, includes, externs = self.parse_dependencies(filename)[:3]
|
| 571 |
+
cimports = set(cimports)
|
| 572 |
+
externs = set(externs)
|
| 573 |
+
incdirs = set()
|
| 574 |
+
for include in self.included_files(filename):
|
| 575 |
+
included_cimports, included_externs, included_incdirs = self.cimports_externs_incdirs(include)
|
| 576 |
+
cimports.update(included_cimports)
|
| 577 |
+
externs.update(included_externs)
|
| 578 |
+
incdirs.update(included_incdirs)
|
| 579 |
+
externs, incdir = normalize_existing(filename, externs)
|
| 580 |
+
if incdir:
|
| 581 |
+
incdirs.add(incdir)
|
| 582 |
+
return tuple(cimports), externs, incdirs
|
| 583 |
+
|
| 584 |
+
def cimports(self, filename):
|
| 585 |
+
return self.cimports_externs_incdirs(filename)[0]
|
| 586 |
+
|
| 587 |
+
def package(self, filename):
|
| 588 |
+
return package(filename)
|
| 589 |
+
|
| 590 |
+
def fully_qualified_name(self, filename):
|
| 591 |
+
return fully_qualified_name(filename)
|
| 592 |
+
|
| 593 |
+
@cached_method
|
| 594 |
+
def find_pxd(self, module, filename=None):
|
| 595 |
+
is_relative = module[0] == '.'
|
| 596 |
+
if is_relative and not filename:
|
| 597 |
+
raise NotImplementedError("New relative imports.")
|
| 598 |
+
if filename is not None:
|
| 599 |
+
module_path = module.split('.')
|
| 600 |
+
if is_relative:
|
| 601 |
+
module_path.pop(0) # just explicitly relative
|
| 602 |
+
package_path = list(self.package(filename))
|
| 603 |
+
while module_path and not module_path[0]:
|
| 604 |
+
try:
|
| 605 |
+
package_path.pop()
|
| 606 |
+
except IndexError:
|
| 607 |
+
return None # FIXME: error?
|
| 608 |
+
module_path.pop(0)
|
| 609 |
+
relative = '.'.join(package_path + module_path)
|
| 610 |
+
pxd = self.context.find_pxd_file(relative, source_file_path=filename)
|
| 611 |
+
if pxd:
|
| 612 |
+
return pxd
|
| 613 |
+
if is_relative:
|
| 614 |
+
return None # FIXME: error?
|
| 615 |
+
return self.context.find_pxd_file(module, source_file_path=filename)
|
| 616 |
+
|
| 617 |
+
@cached_method
|
| 618 |
+
def cimported_files(self, filename):
|
| 619 |
+
filename_root, filename_ext = os.path.splitext(filename)
|
| 620 |
+
if filename_ext in ('.pyx', '.py') and path_exists(filename_root + '.pxd'):
|
| 621 |
+
pxd_list = [filename_root + '.pxd']
|
| 622 |
+
else:
|
| 623 |
+
pxd_list = []
|
| 624 |
+
# Cimports generates all possible combinations package.module
|
| 625 |
+
# when imported as from package cimport module.
|
| 626 |
+
for module in self.cimports(filename):
|
| 627 |
+
if module[:7] == 'cython.' or module == 'cython':
|
| 628 |
+
continue
|
| 629 |
+
pxd_file = self.find_pxd(module, filename)
|
| 630 |
+
if pxd_file is not None:
|
| 631 |
+
pxd_list.append(pxd_file)
|
| 632 |
+
return tuple(pxd_list)
|
| 633 |
+
|
| 634 |
+
@cached_method
|
| 635 |
+
def immediate_dependencies(self, filename):
|
| 636 |
+
all_deps = {filename}
|
| 637 |
+
all_deps.update(self.cimported_files(filename))
|
| 638 |
+
all_deps.update(self.included_files(filename))
|
| 639 |
+
return all_deps
|
| 640 |
+
|
| 641 |
+
def all_dependencies(self, filename):
|
| 642 |
+
return self.transitive_merge(filename, self.immediate_dependencies, set.union)
|
| 643 |
+
|
| 644 |
+
@cached_method
|
| 645 |
+
def timestamp(self, filename):
|
| 646 |
+
return os.path.getmtime(filename)
|
| 647 |
+
|
| 648 |
+
def extract_timestamp(self, filename):
|
| 649 |
+
return self.timestamp(filename), filename
|
| 650 |
+
|
| 651 |
+
def newest_dependency(self, filename):
|
| 652 |
+
return max([self.extract_timestamp(f) for f in self.all_dependencies(filename)])
|
| 653 |
+
|
| 654 |
+
def transitive_fingerprint(self, filename, module, compilation_options):
|
| 655 |
+
r"""
|
| 656 |
+
Return a fingerprint of a cython file that is about to be cythonized.
|
| 657 |
+
|
| 658 |
+
Fingerprints are looked up in future compilations. If the fingerprint
|
| 659 |
+
is found, the cythonization can be skipped. The fingerprint must
|
| 660 |
+
incorporate everything that has an influence on the generated code.
|
| 661 |
+
"""
|
| 662 |
+
try:
|
| 663 |
+
m = hashlib.sha1(__version__.encode('UTF-8'))
|
| 664 |
+
m.update(file_hash(filename).encode('UTF-8'))
|
| 665 |
+
for x in sorted(self.all_dependencies(filename)):
|
| 666 |
+
if os.path.splitext(x)[1] not in ('.c', '.cpp', '.h'):
|
| 667 |
+
m.update(file_hash(x).encode('UTF-8'))
|
| 668 |
+
# Include the module attributes that change the compilation result
|
| 669 |
+
# in the fingerprint. We do not iterate over module.__dict__ and
|
| 670 |
+
# include almost everything here as users might extend Extension
|
| 671 |
+
# with arbitrary (random) attributes that would lead to cache
|
| 672 |
+
# misses.
|
| 673 |
+
m.update(str((
|
| 674 |
+
module.language,
|
| 675 |
+
getattr(module, 'py_limited_api', False),
|
| 676 |
+
getattr(module, 'np_pythran', False)
|
| 677 |
+
)).encode('UTF-8'))
|
| 678 |
+
|
| 679 |
+
m.update(compilation_options.get_fingerprint().encode('UTF-8'))
|
| 680 |
+
return m.hexdigest()
|
| 681 |
+
except IOError:
|
| 682 |
+
return None
|
| 683 |
+
|
| 684 |
+
def distutils_info0(self, filename):
|
| 685 |
+
info = self.parse_dependencies(filename)[3]
|
| 686 |
+
kwds = info.values
|
| 687 |
+
cimports, externs, incdirs = self.cimports_externs_incdirs(filename)
|
| 688 |
+
basedir = os.getcwd()
|
| 689 |
+
# Add dependencies on "cdef extern from ..." files
|
| 690 |
+
if externs:
|
| 691 |
+
externs = _make_relative(externs, basedir)
|
| 692 |
+
if 'depends' in kwds:
|
| 693 |
+
kwds['depends'] = list(set(kwds['depends']).union(externs))
|
| 694 |
+
else:
|
| 695 |
+
kwds['depends'] = list(externs)
|
| 696 |
+
# Add include_dirs to ensure that the C compiler will find the
|
| 697 |
+
# "cdef extern from ..." files
|
| 698 |
+
if incdirs:
|
| 699 |
+
include_dirs = list(kwds.get('include_dirs', []))
|
| 700 |
+
for inc in _make_relative(incdirs, basedir):
|
| 701 |
+
if inc not in include_dirs:
|
| 702 |
+
include_dirs.append(inc)
|
| 703 |
+
kwds['include_dirs'] = include_dirs
|
| 704 |
+
return info
|
| 705 |
+
|
| 706 |
+
def distutils_info(self, filename, aliases=None, base=None):
|
| 707 |
+
return (self.transitive_merge(filename, self.distutils_info0, DistutilsInfo.merge)
|
| 708 |
+
.subs(aliases)
|
| 709 |
+
.merge(base))
|
| 710 |
+
|
| 711 |
+
def transitive_merge(self, node, extract, merge):
|
| 712 |
+
try:
|
| 713 |
+
seen = self._transitive_cache[extract, merge]
|
| 714 |
+
except KeyError:
|
| 715 |
+
seen = self._transitive_cache[extract, merge] = {}
|
| 716 |
+
return self.transitive_merge_helper(
|
| 717 |
+
node, extract, merge, seen, {}, self.cimported_files)[0]
|
| 718 |
+
|
| 719 |
+
def transitive_merge_helper(self, node, extract, merge, seen, stack, outgoing):
|
| 720 |
+
if node in seen:
|
| 721 |
+
return seen[node], None
|
| 722 |
+
deps = extract(node)
|
| 723 |
+
if node in stack:
|
| 724 |
+
return deps, node
|
| 725 |
+
try:
|
| 726 |
+
stack[node] = len(stack)
|
| 727 |
+
loop = None
|
| 728 |
+
for next in outgoing(node):
|
| 729 |
+
sub_deps, sub_loop = self.transitive_merge_helper(next, extract, merge, seen, stack, outgoing)
|
| 730 |
+
if sub_loop is not None:
|
| 731 |
+
if loop is not None and stack[loop] < stack[sub_loop]:
|
| 732 |
+
pass
|
| 733 |
+
else:
|
| 734 |
+
loop = sub_loop
|
| 735 |
+
deps = merge(deps, sub_deps)
|
| 736 |
+
if loop == node:
|
| 737 |
+
loop = None
|
| 738 |
+
if loop is None:
|
| 739 |
+
seen[node] = deps
|
| 740 |
+
return deps, loop
|
| 741 |
+
finally:
|
| 742 |
+
del stack[node]
|
| 743 |
+
|
| 744 |
+
|
| 745 |
+
_dep_tree = None
|
| 746 |
+
|
| 747 |
+
def create_dependency_tree(ctx=None, quiet=False):
|
| 748 |
+
global _dep_tree
|
| 749 |
+
if _dep_tree is None:
|
| 750 |
+
if ctx is None:
|
| 751 |
+
ctx = Context(["."], get_directive_defaults(),
|
| 752 |
+
options=CompilationOptions(default_options))
|
| 753 |
+
_dep_tree = DependencyTree(ctx, quiet=quiet)
|
| 754 |
+
return _dep_tree
|
| 755 |
+
|
| 756 |
+
|
| 757 |
+
# If this changes, change also docs/src/reference/compilation.rst
|
| 758 |
+
# which mentions this function
|
| 759 |
+
def default_create_extension(template, kwds):
|
| 760 |
+
if 'depends' in kwds:
|
| 761 |
+
include_dirs = kwds.get('include_dirs', []) + ["."]
|
| 762 |
+
depends = resolve_depends(kwds['depends'], include_dirs)
|
| 763 |
+
kwds['depends'] = sorted(set(depends + template.depends))
|
| 764 |
+
|
| 765 |
+
t = template.__class__
|
| 766 |
+
ext = t(**kwds)
|
| 767 |
+
metadata = dict(distutils=kwds, module_name=kwds['name'])
|
| 768 |
+
return (ext, metadata)
|
| 769 |
+
|
| 770 |
+
|
| 771 |
+
# This may be useful for advanced users?
|
| 772 |
+
def create_extension_list(patterns, exclude=None, ctx=None, aliases=None, quiet=False, language=None,
|
| 773 |
+
exclude_failures=False):
|
| 774 |
+
if language is not None:
|
| 775 |
+
print('Warning: passing language={0!r} to cythonize() is deprecated. '
|
| 776 |
+
'Instead, put "# distutils: language={0}" in your .pyx or .pxd file(s)'.format(language))
|
| 777 |
+
if exclude is None:
|
| 778 |
+
exclude = []
|
| 779 |
+
if patterns is None:
|
| 780 |
+
return [], {}
|
| 781 |
+
elif isinstance(patterns, basestring) or not isinstance(patterns, Iterable):
|
| 782 |
+
patterns = [patterns]
|
| 783 |
+
|
| 784 |
+
from distutils.extension import Extension
|
| 785 |
+
if 'setuptools' in sys.modules:
|
| 786 |
+
# Support setuptools Extension instances as well.
|
| 787 |
+
extension_classes = (
|
| 788 |
+
Extension, # should normally be the same as 'setuptools.extension._Extension'
|
| 789 |
+
sys.modules['setuptools.extension']._Extension,
|
| 790 |
+
sys.modules['setuptools'].Extension,
|
| 791 |
+
)
|
| 792 |
+
else:
|
| 793 |
+
extension_classes = (Extension,)
|
| 794 |
+
|
| 795 |
+
explicit_modules = {m.name for m in patterns if isinstance(m, extension_classes)}
|
| 796 |
+
deps = create_dependency_tree(ctx, quiet=quiet)
|
| 797 |
+
|
| 798 |
+
to_exclude = set()
|
| 799 |
+
if not isinstance(exclude, list):
|
| 800 |
+
exclude = [exclude]
|
| 801 |
+
for pattern in exclude:
|
| 802 |
+
to_exclude.update(map(os.path.abspath, extended_iglob(pattern)))
|
| 803 |
+
|
| 804 |
+
module_list = []
|
| 805 |
+
module_metadata = {}
|
| 806 |
+
|
| 807 |
+
# if no create_extension() function is defined, use a simple
|
| 808 |
+
# default function.
|
| 809 |
+
create_extension = ctx.options.create_extension or default_create_extension
|
| 810 |
+
|
| 811 |
+
seen = set()
|
| 812 |
+
for pattern in patterns:
|
| 813 |
+
if not isinstance(pattern, extension_classes):
|
| 814 |
+
pattern = encode_filename_in_py2(pattern)
|
| 815 |
+
if isinstance(pattern, str):
|
| 816 |
+
filepattern = pattern
|
| 817 |
+
template = Extension(pattern, []) # Fake Extension without sources
|
| 818 |
+
name = '*'
|
| 819 |
+
base = None
|
| 820 |
+
ext_language = language
|
| 821 |
+
elif isinstance(pattern, extension_classes):
|
| 822 |
+
cython_sources = [s for s in pattern.sources
|
| 823 |
+
if os.path.splitext(s)[1] in ('.py', '.pyx')]
|
| 824 |
+
if cython_sources:
|
| 825 |
+
filepattern = cython_sources[0]
|
| 826 |
+
if len(cython_sources) > 1:
|
| 827 |
+
print(u"Warning: Multiple cython sources found for extension '%s': %s\n"
|
| 828 |
+
u"See https://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html "
|
| 829 |
+
u"for sharing declarations among Cython files." % (pattern.name, cython_sources))
|
| 830 |
+
else:
|
| 831 |
+
# ignore non-cython modules
|
| 832 |
+
module_list.append(pattern)
|
| 833 |
+
continue
|
| 834 |
+
template = pattern
|
| 835 |
+
name = template.name
|
| 836 |
+
base = DistutilsInfo(exn=template)
|
| 837 |
+
ext_language = None # do not override whatever the Extension says
|
| 838 |
+
else:
|
| 839 |
+
msg = str("pattern is not of type str nor subclass of Extension (%s)"
|
| 840 |
+
" but of type %s and class %s" % (repr(Extension),
|
| 841 |
+
type(pattern),
|
| 842 |
+
pattern.__class__))
|
| 843 |
+
raise TypeError(msg)
|
| 844 |
+
|
| 845 |
+
for file in nonempty(sorted(extended_iglob(filepattern)), "'%s' doesn't match any files" % filepattern):
|
| 846 |
+
if os.path.abspath(file) in to_exclude:
|
| 847 |
+
continue
|
| 848 |
+
module_name = deps.fully_qualified_name(file)
|
| 849 |
+
if '*' in name:
|
| 850 |
+
if module_name in explicit_modules:
|
| 851 |
+
continue
|
| 852 |
+
elif name:
|
| 853 |
+
module_name = name
|
| 854 |
+
|
| 855 |
+
Utils.raise_error_if_module_name_forbidden(module_name)
|
| 856 |
+
|
| 857 |
+
if module_name not in seen:
|
| 858 |
+
try:
|
| 859 |
+
kwds = deps.distutils_info(file, aliases, base).values
|
| 860 |
+
except Exception:
|
| 861 |
+
if exclude_failures:
|
| 862 |
+
continue
|
| 863 |
+
raise
|
| 864 |
+
if base is not None:
|
| 865 |
+
for key, value in base.values.items():
|
| 866 |
+
if key not in kwds:
|
| 867 |
+
kwds[key] = value
|
| 868 |
+
|
| 869 |
+
kwds['name'] = module_name
|
| 870 |
+
|
| 871 |
+
sources = [file] + [m for m in template.sources if m != filepattern]
|
| 872 |
+
if 'sources' in kwds:
|
| 873 |
+
# allow users to add .c files etc.
|
| 874 |
+
for source in kwds['sources']:
|
| 875 |
+
source = encode_filename_in_py2(source)
|
| 876 |
+
if source not in sources:
|
| 877 |
+
sources.append(source)
|
| 878 |
+
kwds['sources'] = sources
|
| 879 |
+
|
| 880 |
+
if ext_language and 'language' not in kwds:
|
| 881 |
+
kwds['language'] = ext_language
|
| 882 |
+
|
| 883 |
+
np_pythran = kwds.pop('np_pythran', False)
|
| 884 |
+
|
| 885 |
+
# Create the new extension
|
| 886 |
+
m, metadata = create_extension(template, kwds)
|
| 887 |
+
m.np_pythran = np_pythran or getattr(m, 'np_pythran', False)
|
| 888 |
+
if m.np_pythran:
|
| 889 |
+
update_pythran_extension(m)
|
| 890 |
+
module_list.append(m)
|
| 891 |
+
|
| 892 |
+
# Store metadata (this will be written as JSON in the
|
| 893 |
+
# generated C file but otherwise has no purpose)
|
| 894 |
+
module_metadata[module_name] = metadata
|
| 895 |
+
|
| 896 |
+
if file not in m.sources:
|
| 897 |
+
# Old setuptools unconditionally replaces .pyx with .c/.cpp
|
| 898 |
+
target_file = os.path.splitext(file)[0] + ('.cpp' if m.language == 'c++' else '.c')
|
| 899 |
+
try:
|
| 900 |
+
m.sources.remove(target_file)
|
| 901 |
+
except ValueError:
|
| 902 |
+
# never seen this in the wild, but probably better to warn about this unexpected case
|
| 903 |
+
print(u"Warning: Cython source file not found in sources list, adding %s" % file)
|
| 904 |
+
m.sources.insert(0, file)
|
| 905 |
+
seen.add(name)
|
| 906 |
+
return module_list, module_metadata
|
| 907 |
+
|
| 908 |
+
|
| 909 |
+
# This is the user-exposed entry point.
|
| 910 |
+
def cythonize(module_list, exclude=None, nthreads=0, aliases=None, quiet=False, force=None, language=None,
|
| 911 |
+
exclude_failures=False, show_all_warnings=False, **options):
|
| 912 |
+
"""
|
| 913 |
+
Compile a set of source modules into C/C++ files and return a list of distutils
|
| 914 |
+
Extension objects for them.
|
| 915 |
+
|
| 916 |
+
:param module_list: As module list, pass either a glob pattern, a list of glob
|
| 917 |
+
patterns or a list of Extension objects. The latter
|
| 918 |
+
allows you to configure the extensions separately
|
| 919 |
+
through the normal distutils options.
|
| 920 |
+
You can also pass Extension objects that have
|
| 921 |
+
glob patterns as their sources. Then, cythonize
|
| 922 |
+
will resolve the pattern and create a
|
| 923 |
+
copy of the Extension for every matching file.
|
| 924 |
+
|
| 925 |
+
:param exclude: When passing glob patterns as ``module_list``, you can exclude certain
|
| 926 |
+
module names explicitly by passing them into the ``exclude`` option.
|
| 927 |
+
|
| 928 |
+
:param nthreads: The number of concurrent builds for parallel compilation
|
| 929 |
+
(requires the ``multiprocessing`` module).
|
| 930 |
+
|
| 931 |
+
:param aliases: If you want to use compiler directives like ``# distutils: ...`` but
|
| 932 |
+
can only know at compile time (when running the ``setup.py``) which values
|
| 933 |
+
to use, you can use aliases and pass a dictionary mapping those aliases
|
| 934 |
+
to Python strings when calling :func:`cythonize`. As an example, say you
|
| 935 |
+
want to use the compiler
|
| 936 |
+
directive ``# distutils: include_dirs = ../static_libs/include/``
|
| 937 |
+
but this path isn't always fixed and you want to find it when running
|
| 938 |
+
the ``setup.py``. You can then do ``# distutils: include_dirs = MY_HEADERS``,
|
| 939 |
+
find the value of ``MY_HEADERS`` in the ``setup.py``, put it in a python
|
| 940 |
+
variable called ``foo`` as a string, and then call
|
| 941 |
+
``cythonize(..., aliases={'MY_HEADERS': foo})``.
|
| 942 |
+
|
| 943 |
+
:param quiet: If True, Cython won't print error, warning, or status messages during the
|
| 944 |
+
compilation.
|
| 945 |
+
|
| 946 |
+
:param force: Forces the recompilation of the Cython modules, even if the timestamps
|
| 947 |
+
don't indicate that a recompilation is necessary.
|
| 948 |
+
|
| 949 |
+
:param language: To globally enable C++ mode, you can pass ``language='c++'``. Otherwise, this
|
| 950 |
+
will be determined at a per-file level based on compiler directives. This
|
| 951 |
+
affects only modules found based on file names. Extension instances passed
|
| 952 |
+
into :func:`cythonize` will not be changed. It is recommended to rather
|
| 953 |
+
use the compiler directive ``# distutils: language = c++`` than this option.
|
| 954 |
+
|
| 955 |
+
:param exclude_failures: For a broad 'try to compile' mode that ignores compilation
|
| 956 |
+
failures and simply excludes the failed extensions,
|
| 957 |
+
pass ``exclude_failures=True``. Note that this only
|
| 958 |
+
really makes sense for compiling ``.py`` files which can also
|
| 959 |
+
be used without compilation.
|
| 960 |
+
|
| 961 |
+
:param show_all_warnings: By default, not all Cython warnings are printed.
|
| 962 |
+
Set to true to show all warnings.
|
| 963 |
+
|
| 964 |
+
:param annotate: If ``True``, will produce a HTML file for each of the ``.pyx`` or ``.py``
|
| 965 |
+
files compiled. The HTML file gives an indication
|
| 966 |
+
of how much Python interaction there is in
|
| 967 |
+
each of the source code lines, compared to plain C code.
|
| 968 |
+
It also allows you to see the C/C++ code
|
| 969 |
+
generated for each line of Cython code. This report is invaluable when
|
| 970 |
+
optimizing a function for speed,
|
| 971 |
+
and for determining when to :ref:`release the GIL <nogil>`:
|
| 972 |
+
in general, a ``nogil`` block may contain only "white" code.
|
| 973 |
+
See examples in :ref:`determining_where_to_add_types` or
|
| 974 |
+
:ref:`primes`.
|
| 975 |
+
|
| 976 |
+
|
| 977 |
+
:param annotate-fullc: If ``True`` will produce a colorized HTML version of
|
| 978 |
+
the source which includes entire generated C/C++-code.
|
| 979 |
+
|
| 980 |
+
|
| 981 |
+
:param compiler_directives: Allow to set compiler directives in the ``setup.py`` like this:
|
| 982 |
+
``compiler_directives={'embedsignature': True}``.
|
| 983 |
+
See :ref:`compiler-directives`.
|
| 984 |
+
|
| 985 |
+
:param depfile: produce depfiles for the sources if True.
|
| 986 |
+
"""
|
| 987 |
+
if exclude is None:
|
| 988 |
+
exclude = []
|
| 989 |
+
if 'include_path' not in options:
|
| 990 |
+
options['include_path'] = ['.']
|
| 991 |
+
if 'common_utility_include_dir' in options:
|
| 992 |
+
safe_makedirs(options['common_utility_include_dir'])
|
| 993 |
+
|
| 994 |
+
depfile = options.pop('depfile', None)
|
| 995 |
+
|
| 996 |
+
if pythran is None:
|
| 997 |
+
pythran_options = None
|
| 998 |
+
else:
|
| 999 |
+
pythran_options = CompilationOptions(**options)
|
| 1000 |
+
pythran_options.cplus = True
|
| 1001 |
+
pythran_options.np_pythran = True
|
| 1002 |
+
|
| 1003 |
+
if force is None:
|
| 1004 |
+
force = os.environ.get("CYTHON_FORCE_REGEN") == "1" # allow global overrides for build systems
|
| 1005 |
+
|
| 1006 |
+
c_options = CompilationOptions(**options)
|
| 1007 |
+
cpp_options = CompilationOptions(**options); cpp_options.cplus = True
|
| 1008 |
+
ctx = Context.from_options(c_options)
|
| 1009 |
+
options = c_options
|
| 1010 |
+
module_list, module_metadata = create_extension_list(
|
| 1011 |
+
module_list,
|
| 1012 |
+
exclude=exclude,
|
| 1013 |
+
ctx=ctx,
|
| 1014 |
+
quiet=quiet,
|
| 1015 |
+
exclude_failures=exclude_failures,
|
| 1016 |
+
language=language,
|
| 1017 |
+
aliases=aliases)
|
| 1018 |
+
|
| 1019 |
+
fix_windows_unicode_modules(module_list)
|
| 1020 |
+
|
| 1021 |
+
deps = create_dependency_tree(ctx, quiet=quiet)
|
| 1022 |
+
build_dir = getattr(options, 'build_dir', None)
|
| 1023 |
+
|
| 1024 |
+
def copy_to_build_dir(filepath, root=os.getcwd()):
|
| 1025 |
+
filepath_abs = os.path.abspath(filepath)
|
| 1026 |
+
if os.path.isabs(filepath):
|
| 1027 |
+
filepath = filepath_abs
|
| 1028 |
+
if filepath_abs.startswith(root):
|
| 1029 |
+
# distutil extension depends are relative to cwd
|
| 1030 |
+
mod_dir = join_path(build_dir,
|
| 1031 |
+
os.path.dirname(_relpath(filepath, root)))
|
| 1032 |
+
copy_once_if_newer(filepath_abs, mod_dir)
|
| 1033 |
+
|
| 1034 |
+
modules_by_cfile = collections.defaultdict(list)
|
| 1035 |
+
to_compile = []
|
| 1036 |
+
for m in module_list:
|
| 1037 |
+
if build_dir:
|
| 1038 |
+
for dep in m.depends:
|
| 1039 |
+
copy_to_build_dir(dep)
|
| 1040 |
+
|
| 1041 |
+
cy_sources = [
|
| 1042 |
+
source for source in m.sources
|
| 1043 |
+
if os.path.splitext(source)[1] in ('.pyx', '.py')]
|
| 1044 |
+
if len(cy_sources) == 1:
|
| 1045 |
+
# normal "special" case: believe the Extension module name to allow user overrides
|
| 1046 |
+
full_module_name = m.name
|
| 1047 |
+
else:
|
| 1048 |
+
# infer FQMN from source files
|
| 1049 |
+
full_module_name = None
|
| 1050 |
+
|
| 1051 |
+
new_sources = []
|
| 1052 |
+
for source in m.sources:
|
| 1053 |
+
base, ext = os.path.splitext(source)
|
| 1054 |
+
if ext in ('.pyx', '.py'):
|
| 1055 |
+
if m.np_pythran:
|
| 1056 |
+
c_file = base + '.cpp'
|
| 1057 |
+
options = pythran_options
|
| 1058 |
+
elif m.language == 'c++':
|
| 1059 |
+
c_file = base + '.cpp'
|
| 1060 |
+
options = cpp_options
|
| 1061 |
+
else:
|
| 1062 |
+
c_file = base + '.c'
|
| 1063 |
+
options = c_options
|
| 1064 |
+
|
| 1065 |
+
# setup for out of place build directory if enabled
|
| 1066 |
+
if build_dir:
|
| 1067 |
+
if os.path.isabs(c_file):
|
| 1068 |
+
c_file = os.path.splitdrive(c_file)[1]
|
| 1069 |
+
c_file = c_file.split(os.sep, 1)[1]
|
| 1070 |
+
c_file = os.path.join(build_dir, c_file)
|
| 1071 |
+
dir = os.path.dirname(c_file)
|
| 1072 |
+
safe_makedirs_once(dir)
|
| 1073 |
+
|
| 1074 |
+
# write out the depfile, if requested
|
| 1075 |
+
if depfile:
|
| 1076 |
+
dependencies = deps.all_dependencies(source)
|
| 1077 |
+
write_depfile(c_file, source, dependencies)
|
| 1078 |
+
|
| 1079 |
+
# Missing files and those generated by other Cython versions should always be recreated.
|
| 1080 |
+
if Utils.file_generated_by_this_cython(c_file):
|
| 1081 |
+
c_timestamp = os.path.getmtime(c_file)
|
| 1082 |
+
else:
|
| 1083 |
+
c_timestamp = -1
|
| 1084 |
+
|
| 1085 |
+
# Priority goes first to modified files, second to direct
|
| 1086 |
+
# dependents, and finally to indirect dependents.
|
| 1087 |
+
if c_timestamp < deps.timestamp(source):
|
| 1088 |
+
dep_timestamp, dep = deps.timestamp(source), source
|
| 1089 |
+
priority = 0
|
| 1090 |
+
else:
|
| 1091 |
+
dep_timestamp, dep = deps.newest_dependency(source)
|
| 1092 |
+
priority = 2 - (dep in deps.immediate_dependencies(source))
|
| 1093 |
+
if force or c_timestamp < dep_timestamp:
|
| 1094 |
+
if not quiet and not force:
|
| 1095 |
+
if source == dep:
|
| 1096 |
+
print(u"Compiling %s because it changed." % Utils.decode_filename(source))
|
| 1097 |
+
else:
|
| 1098 |
+
print(u"Compiling %s because it depends on %s." % (
|
| 1099 |
+
Utils.decode_filename(source),
|
| 1100 |
+
Utils.decode_filename(dep),
|
| 1101 |
+
))
|
| 1102 |
+
if not force and options.cache:
|
| 1103 |
+
fingerprint = deps.transitive_fingerprint(source, m, options)
|
| 1104 |
+
else:
|
| 1105 |
+
fingerprint = None
|
| 1106 |
+
to_compile.append((
|
| 1107 |
+
priority, source, c_file, fingerprint, quiet,
|
| 1108 |
+
options, not exclude_failures, module_metadata.get(m.name),
|
| 1109 |
+
full_module_name, show_all_warnings))
|
| 1110 |
+
new_sources.append(c_file)
|
| 1111 |
+
modules_by_cfile[c_file].append(m)
|
| 1112 |
+
else:
|
| 1113 |
+
new_sources.append(source)
|
| 1114 |
+
if build_dir:
|
| 1115 |
+
copy_to_build_dir(source)
|
| 1116 |
+
m.sources = new_sources
|
| 1117 |
+
|
| 1118 |
+
if options.cache:
|
| 1119 |
+
if not os.path.exists(options.cache):
|
| 1120 |
+
os.makedirs(options.cache)
|
| 1121 |
+
to_compile.sort()
|
| 1122 |
+
# Drop "priority" component of "to_compile" entries and add a
|
| 1123 |
+
# simple progress indicator.
|
| 1124 |
+
N = len(to_compile)
|
| 1125 |
+
progress_fmt = "[{0:%d}/{1}] " % len(str(N))
|
| 1126 |
+
for i in range(N):
|
| 1127 |
+
progress = progress_fmt.format(i+1, N)
|
| 1128 |
+
to_compile[i] = to_compile[i][1:] + (progress,)
|
| 1129 |
+
|
| 1130 |
+
if N <= 1:
|
| 1131 |
+
nthreads = 0
|
| 1132 |
+
if nthreads:
|
| 1133 |
+
import multiprocessing
|
| 1134 |
+
pool = multiprocessing.Pool(
|
| 1135 |
+
nthreads, initializer=_init_multiprocessing_helper)
|
| 1136 |
+
# This is a bit more involved than it should be, because KeyboardInterrupts
|
| 1137 |
+
# break the multiprocessing workers when using a normal pool.map().
|
| 1138 |
+
# See, for example:
|
| 1139 |
+
# https://noswap.com/blog/python-multiprocessing-keyboardinterrupt
|
| 1140 |
+
try:
|
| 1141 |
+
result = pool.map_async(cythonize_one_helper, to_compile, chunksize=1)
|
| 1142 |
+
pool.close()
|
| 1143 |
+
while not result.ready():
|
| 1144 |
+
try:
|
| 1145 |
+
result.get(99999) # seconds
|
| 1146 |
+
except multiprocessing.TimeoutError:
|
| 1147 |
+
pass
|
| 1148 |
+
except KeyboardInterrupt:
|
| 1149 |
+
pool.terminate()
|
| 1150 |
+
raise
|
| 1151 |
+
pool.join()
|
| 1152 |
+
else:
|
| 1153 |
+
for args in to_compile:
|
| 1154 |
+
cythonize_one(*args)
|
| 1155 |
+
|
| 1156 |
+
if exclude_failures:
|
| 1157 |
+
failed_modules = set()
|
| 1158 |
+
for c_file, modules in modules_by_cfile.items():
|
| 1159 |
+
if not os.path.exists(c_file):
|
| 1160 |
+
failed_modules.update(modules)
|
| 1161 |
+
elif os.path.getsize(c_file) < 200:
|
| 1162 |
+
f = io_open(c_file, 'r', encoding='iso8859-1')
|
| 1163 |
+
try:
|
| 1164 |
+
if f.read(len('#error ')) == '#error ':
|
| 1165 |
+
# dead compilation result
|
| 1166 |
+
failed_modules.update(modules)
|
| 1167 |
+
finally:
|
| 1168 |
+
f.close()
|
| 1169 |
+
if failed_modules:
|
| 1170 |
+
for module in failed_modules:
|
| 1171 |
+
module_list.remove(module)
|
| 1172 |
+
print(u"Failed compilations: %s" % ', '.join(sorted([
|
| 1173 |
+
module.name for module in failed_modules])))
|
| 1174 |
+
|
| 1175 |
+
if options.cache:
|
| 1176 |
+
cleanup_cache(options.cache, getattr(options, 'cache_size', 1024 * 1024 * 100))
|
| 1177 |
+
# cythonize() is often followed by the (non-Python-buffered)
|
| 1178 |
+
# compiler output, flush now to avoid interleaving output.
|
| 1179 |
+
sys.stdout.flush()
|
| 1180 |
+
return module_list
|
| 1181 |
+
|
| 1182 |
+
|
| 1183 |
+
def fix_windows_unicode_modules(module_list):
|
| 1184 |
+
# Hack around a distutils 3.[5678] bug on Windows for unicode module names.
|
| 1185 |
+
# https://bugs.python.org/issue39432
|
| 1186 |
+
if sys.platform != "win32":
|
| 1187 |
+
return
|
| 1188 |
+
if sys.version_info < (3, 5) or sys.version_info >= (3, 8, 2):
|
| 1189 |
+
return
|
| 1190 |
+
|
| 1191 |
+
def make_filtered_list(ignored_symbol, old_entries):
|
| 1192 |
+
class FilteredExportSymbols(list):
|
| 1193 |
+
# export_symbols for unicode filename cause link errors on Windows
|
| 1194 |
+
# Cython doesn't need them (it already defines PyInit with the correct linkage)
|
| 1195 |
+
# so use this class as a temporary fix to stop them from being generated
|
| 1196 |
+
def __contains__(self, val):
|
| 1197 |
+
# so distutils doesn't "helpfully" add PyInit_<name>
|
| 1198 |
+
return val == ignored_symbol or list.__contains__(self, val)
|
| 1199 |
+
|
| 1200 |
+
filtered_list = FilteredExportSymbols(old_entries)
|
| 1201 |
+
if old_entries:
|
| 1202 |
+
filtered_list.extend(name for name in old_entries if name != ignored_symbol)
|
| 1203 |
+
return filtered_list
|
| 1204 |
+
|
| 1205 |
+
for m in module_list:
|
| 1206 |
+
# TODO: use m.name.isascii() in Py3.7+
|
| 1207 |
+
try:
|
| 1208 |
+
m.name.encode("ascii")
|
| 1209 |
+
continue
|
| 1210 |
+
except UnicodeEncodeError:
|
| 1211 |
+
pass
|
| 1212 |
+
m.export_symbols = make_filtered_list(
|
| 1213 |
+
"PyInit_" + m.name.rsplit(".", 1)[-1],
|
| 1214 |
+
m.export_symbols,
|
| 1215 |
+
)
|
| 1216 |
+
|
| 1217 |
+
|
| 1218 |
+
if os.environ.get('XML_RESULTS'):
|
| 1219 |
+
compile_result_dir = os.environ['XML_RESULTS']
|
| 1220 |
+
def record_results(func):
|
| 1221 |
+
def with_record(*args):
|
| 1222 |
+
t = time.time()
|
| 1223 |
+
success = True
|
| 1224 |
+
try:
|
| 1225 |
+
try:
|
| 1226 |
+
func(*args)
|
| 1227 |
+
except:
|
| 1228 |
+
success = False
|
| 1229 |
+
finally:
|
| 1230 |
+
t = time.time() - t
|
| 1231 |
+
module = fully_qualified_name(args[0])
|
| 1232 |
+
name = "cythonize." + module
|
| 1233 |
+
failures = 1 - success
|
| 1234 |
+
if success:
|
| 1235 |
+
failure_item = ""
|
| 1236 |
+
else:
|
| 1237 |
+
failure_item = "failure"
|
| 1238 |
+
output = open(os.path.join(compile_result_dir, name + ".xml"), "w")
|
| 1239 |
+
output.write("""
|
| 1240 |
+
<?xml version="1.0" ?>
|
| 1241 |
+
<testsuite name="%(name)s" errors="0" failures="%(failures)s" tests="1" time="%(t)s">
|
| 1242 |
+
<testcase classname="%(name)s" name="cythonize">
|
| 1243 |
+
%(failure_item)s
|
| 1244 |
+
</testcase>
|
| 1245 |
+
</testsuite>
|
| 1246 |
+
""".strip() % locals())
|
| 1247 |
+
output.close()
|
| 1248 |
+
return with_record
|
| 1249 |
+
else:
|
| 1250 |
+
def record_results(func):
|
| 1251 |
+
return func
|
| 1252 |
+
|
| 1253 |
+
|
| 1254 |
+
# TODO: Share context? Issue: pyx processing leaks into pxd module
|
| 1255 |
+
@record_results
|
| 1256 |
+
def cythonize_one(pyx_file, c_file, fingerprint, quiet, options=None,
|
| 1257 |
+
raise_on_failure=True, embedded_metadata=None,
|
| 1258 |
+
full_module_name=None, show_all_warnings=False,
|
| 1259 |
+
progress=""):
|
| 1260 |
+
from ..Compiler.Main import compile_single, default_options
|
| 1261 |
+
from ..Compiler.Errors import CompileError, PyrexError
|
| 1262 |
+
|
| 1263 |
+
if fingerprint:
|
| 1264 |
+
if not os.path.exists(options.cache):
|
| 1265 |
+
safe_makedirs(options.cache)
|
| 1266 |
+
# Cython-generated c files are highly compressible.
|
| 1267 |
+
# (E.g. a compression ratio of about 10 for Sage).
|
| 1268 |
+
fingerprint_file_base = join_path(
|
| 1269 |
+
options.cache, "%s-%s" % (os.path.basename(c_file), fingerprint))
|
| 1270 |
+
gz_fingerprint_file = fingerprint_file_base + gzip_ext
|
| 1271 |
+
zip_fingerprint_file = fingerprint_file_base + '.zip'
|
| 1272 |
+
if os.path.exists(gz_fingerprint_file) or os.path.exists(zip_fingerprint_file):
|
| 1273 |
+
if not quiet:
|
| 1274 |
+
print(u"%sFound compiled %s in cache" % (progress, pyx_file))
|
| 1275 |
+
if os.path.exists(gz_fingerprint_file):
|
| 1276 |
+
os.utime(gz_fingerprint_file, None)
|
| 1277 |
+
with contextlib.closing(gzip_open(gz_fingerprint_file, 'rb')) as g:
|
| 1278 |
+
with contextlib.closing(open(c_file, 'wb')) as f:
|
| 1279 |
+
shutil.copyfileobj(g, f)
|
| 1280 |
+
else:
|
| 1281 |
+
os.utime(zip_fingerprint_file, None)
|
| 1282 |
+
dirname = os.path.dirname(c_file)
|
| 1283 |
+
with contextlib.closing(zipfile.ZipFile(zip_fingerprint_file)) as z:
|
| 1284 |
+
for artifact in z.namelist():
|
| 1285 |
+
z.extract(artifact, os.path.join(dirname, artifact))
|
| 1286 |
+
return
|
| 1287 |
+
if not quiet:
|
| 1288 |
+
print(u"%sCythonizing %s" % (progress, Utils.decode_filename(pyx_file)))
|
| 1289 |
+
if options is None:
|
| 1290 |
+
options = CompilationOptions(default_options)
|
| 1291 |
+
options.output_file = c_file
|
| 1292 |
+
options.embedded_metadata = embedded_metadata
|
| 1293 |
+
|
| 1294 |
+
old_warning_level = Errors.LEVEL
|
| 1295 |
+
if show_all_warnings:
|
| 1296 |
+
Errors.LEVEL = 0
|
| 1297 |
+
|
| 1298 |
+
any_failures = 0
|
| 1299 |
+
try:
|
| 1300 |
+
result = compile_single(pyx_file, options, full_module_name=full_module_name)
|
| 1301 |
+
if result.num_errors > 0:
|
| 1302 |
+
any_failures = 1
|
| 1303 |
+
except (EnvironmentError, PyrexError) as e:
|
| 1304 |
+
sys.stderr.write('%s\n' % e)
|
| 1305 |
+
any_failures = 1
|
| 1306 |
+
# XXX
|
| 1307 |
+
import traceback
|
| 1308 |
+
traceback.print_exc()
|
| 1309 |
+
except Exception:
|
| 1310 |
+
if raise_on_failure:
|
| 1311 |
+
raise
|
| 1312 |
+
import traceback
|
| 1313 |
+
traceback.print_exc()
|
| 1314 |
+
any_failures = 1
|
| 1315 |
+
finally:
|
| 1316 |
+
if show_all_warnings:
|
| 1317 |
+
Errors.LEVEL = old_warning_level
|
| 1318 |
+
|
| 1319 |
+
if any_failures:
|
| 1320 |
+
if raise_on_failure:
|
| 1321 |
+
raise CompileError(None, pyx_file)
|
| 1322 |
+
elif os.path.exists(c_file):
|
| 1323 |
+
os.remove(c_file)
|
| 1324 |
+
elif fingerprint:
|
| 1325 |
+
artifacts = list(filter(None, [
|
| 1326 |
+
getattr(result, attr, None)
|
| 1327 |
+
for attr in ('c_file', 'h_file', 'api_file', 'i_file')]))
|
| 1328 |
+
if len(artifacts) == 1:
|
| 1329 |
+
fingerprint_file = gz_fingerprint_file
|
| 1330 |
+
with contextlib.closing(open(c_file, 'rb')) as f:
|
| 1331 |
+
with contextlib.closing(gzip_open(fingerprint_file + '.tmp', 'wb')) as g:
|
| 1332 |
+
shutil.copyfileobj(f, g)
|
| 1333 |
+
else:
|
| 1334 |
+
fingerprint_file = zip_fingerprint_file
|
| 1335 |
+
with contextlib.closing(zipfile.ZipFile(
|
| 1336 |
+
fingerprint_file + '.tmp', 'w', zipfile_compression_mode)) as zip:
|
| 1337 |
+
for artifact in artifacts:
|
| 1338 |
+
zip.write(artifact, os.path.basename(artifact))
|
| 1339 |
+
os.rename(fingerprint_file + '.tmp', fingerprint_file)
|
| 1340 |
+
|
| 1341 |
+
|
| 1342 |
+
def cythonize_one_helper(m):
|
| 1343 |
+
import traceback
|
| 1344 |
+
try:
|
| 1345 |
+
return cythonize_one(*m)
|
| 1346 |
+
except Exception:
|
| 1347 |
+
traceback.print_exc()
|
| 1348 |
+
raise
|
| 1349 |
+
|
| 1350 |
+
|
| 1351 |
+
def _init_multiprocessing_helper():
|
| 1352 |
+
# KeyboardInterrupt kills workers, so don't let them get it
|
| 1353 |
+
import signal
|
| 1354 |
+
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
| 1355 |
+
|
| 1356 |
+
|
| 1357 |
+
def cleanup_cache(cache, target_size, ratio=.85):
|
| 1358 |
+
try:
|
| 1359 |
+
p = subprocess.Popen(['du', '-s', '-k', os.path.abspath(cache)], stdout=subprocess.PIPE)
|
| 1360 |
+
stdout, _ = p.communicate()
|
| 1361 |
+
res = p.wait()
|
| 1362 |
+
if res == 0:
|
| 1363 |
+
total_size = 1024 * int(stdout.strip().split()[0])
|
| 1364 |
+
if total_size < target_size:
|
| 1365 |
+
return
|
| 1366 |
+
except (OSError, ValueError):
|
| 1367 |
+
pass
|
| 1368 |
+
total_size = 0
|
| 1369 |
+
all = []
|
| 1370 |
+
for file in os.listdir(cache):
|
| 1371 |
+
path = join_path(cache, file)
|
| 1372 |
+
s = os.stat(path)
|
| 1373 |
+
total_size += s.st_size
|
| 1374 |
+
all.append((s.st_atime, s.st_size, path))
|
| 1375 |
+
if total_size > target_size:
|
| 1376 |
+
for time, size, file in reversed(sorted(all)):
|
| 1377 |
+
os.unlink(file)
|
| 1378 |
+
total_size -= size
|
| 1379 |
+
if total_size < target_size * ratio:
|
| 1380 |
+
break
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Distutils.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from Cython.Distutils.build_ext import build_ext
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Inline.py
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import absolute_import
|
| 2 |
+
|
| 3 |
+
import hashlib
|
| 4 |
+
import inspect
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
import sys
|
| 8 |
+
|
| 9 |
+
from distutils.core import Distribution, Extension
|
| 10 |
+
from distutils.command.build_ext import build_ext
|
| 11 |
+
|
| 12 |
+
import Cython
|
| 13 |
+
from ..Compiler.Main import Context
|
| 14 |
+
from ..Compiler.Options import (default_options, CompilationOptions,
|
| 15 |
+
get_directive_defaults)
|
| 16 |
+
|
| 17 |
+
from ..Compiler.Visitor import CythonTransform, EnvTransform
|
| 18 |
+
from ..Compiler.ParseTreeTransforms import SkipDeclarations
|
| 19 |
+
from ..Compiler.TreeFragment import parse_from_strings
|
| 20 |
+
from ..Compiler.StringEncoding import _unicode
|
| 21 |
+
from .Dependencies import strip_string_literals, cythonize, cached_function
|
| 22 |
+
from ..Compiler import Pipeline
|
| 23 |
+
from ..Utils import get_cython_cache_dir
|
| 24 |
+
import cython as cython_module
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
IS_PY3 = sys.version_info >= (3,)
|
| 28 |
+
|
| 29 |
+
# A utility function to convert user-supplied ASCII strings to unicode.
|
| 30 |
+
if not IS_PY3:
|
| 31 |
+
def to_unicode(s):
|
| 32 |
+
if isinstance(s, bytes):
|
| 33 |
+
return s.decode('ascii')
|
| 34 |
+
else:
|
| 35 |
+
return s
|
| 36 |
+
else:
|
| 37 |
+
to_unicode = lambda x: x
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if sys.version_info < (3, 5):
|
| 41 |
+
import imp
|
| 42 |
+
def load_dynamic(name, module_path):
|
| 43 |
+
return imp.load_dynamic(name, module_path)
|
| 44 |
+
else:
|
| 45 |
+
import importlib.util
|
| 46 |
+
from importlib.machinery import ExtensionFileLoader
|
| 47 |
+
|
| 48 |
+
def load_dynamic(name, path):
|
| 49 |
+
spec = importlib.util.spec_from_file_location(name, loader=ExtensionFileLoader(name, path))
|
| 50 |
+
module = importlib.util.module_from_spec(spec)
|
| 51 |
+
spec.loader.exec_module(module)
|
| 52 |
+
return module
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
class UnboundSymbols(EnvTransform, SkipDeclarations):
|
| 56 |
+
def __init__(self):
|
| 57 |
+
super(EnvTransform, self).__init__(context=None)
|
| 58 |
+
self.unbound = set()
|
| 59 |
+
def visit_NameNode(self, node):
|
| 60 |
+
if not self.current_env().lookup(node.name):
|
| 61 |
+
self.unbound.add(node.name)
|
| 62 |
+
return node
|
| 63 |
+
def __call__(self, node):
|
| 64 |
+
super(UnboundSymbols, self).__call__(node)
|
| 65 |
+
return self.unbound
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
@cached_function
|
| 69 |
+
def unbound_symbols(code, context=None):
|
| 70 |
+
code = to_unicode(code)
|
| 71 |
+
if context is None:
|
| 72 |
+
context = Context([], get_directive_defaults(),
|
| 73 |
+
options=CompilationOptions(default_options))
|
| 74 |
+
from ..Compiler.ParseTreeTransforms import AnalyseDeclarationsTransform
|
| 75 |
+
tree = parse_from_strings('(tree fragment)', code)
|
| 76 |
+
for phase in Pipeline.create_pipeline(context, 'pyx'):
|
| 77 |
+
if phase is None:
|
| 78 |
+
continue
|
| 79 |
+
tree = phase(tree)
|
| 80 |
+
if isinstance(phase, AnalyseDeclarationsTransform):
|
| 81 |
+
break
|
| 82 |
+
try:
|
| 83 |
+
import builtins
|
| 84 |
+
except ImportError:
|
| 85 |
+
import __builtin__ as builtins
|
| 86 |
+
return tuple(UnboundSymbols()(tree) - set(dir(builtins)))
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def unsafe_type(arg, context=None):
|
| 90 |
+
py_type = type(arg)
|
| 91 |
+
if py_type is int:
|
| 92 |
+
return 'long'
|
| 93 |
+
else:
|
| 94 |
+
return safe_type(arg, context)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def safe_type(arg, context=None):
|
| 98 |
+
py_type = type(arg)
|
| 99 |
+
if py_type in (list, tuple, dict, str):
|
| 100 |
+
return py_type.__name__
|
| 101 |
+
elif py_type is complex:
|
| 102 |
+
return 'double complex'
|
| 103 |
+
elif py_type is float:
|
| 104 |
+
return 'double'
|
| 105 |
+
elif py_type is bool:
|
| 106 |
+
return 'bint'
|
| 107 |
+
elif 'numpy' in sys.modules and isinstance(arg, sys.modules['numpy'].ndarray):
|
| 108 |
+
return 'numpy.ndarray[numpy.%s_t, ndim=%s]' % (arg.dtype.name, arg.ndim)
|
| 109 |
+
else:
|
| 110 |
+
for base_type in py_type.__mro__:
|
| 111 |
+
if base_type.__module__ in ('__builtin__', 'builtins'):
|
| 112 |
+
return 'object'
|
| 113 |
+
module = context.find_module(base_type.__module__, need_pxd=False)
|
| 114 |
+
if module:
|
| 115 |
+
entry = module.lookup(base_type.__name__)
|
| 116 |
+
if entry.is_type:
|
| 117 |
+
return '%s.%s' % (base_type.__module__, base_type.__name__)
|
| 118 |
+
return 'object'
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def _get_build_extension():
|
| 122 |
+
dist = Distribution()
|
| 123 |
+
# Ensure the build respects distutils configuration by parsing
|
| 124 |
+
# the configuration files
|
| 125 |
+
config_files = dist.find_config_files()
|
| 126 |
+
dist.parse_config_files(config_files)
|
| 127 |
+
build_extension = build_ext(dist)
|
| 128 |
+
build_extension.finalize_options()
|
| 129 |
+
return build_extension
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
@cached_function
|
| 133 |
+
def _create_context(cython_include_dirs):
|
| 134 |
+
return Context(
|
| 135 |
+
list(cython_include_dirs),
|
| 136 |
+
get_directive_defaults(),
|
| 137 |
+
options=CompilationOptions(default_options)
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
_cython_inline_cache = {}
|
| 142 |
+
_cython_inline_default_context = _create_context(('.',))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def _populate_unbound(kwds, unbound_symbols, locals=None, globals=None):
|
| 146 |
+
for symbol in unbound_symbols:
|
| 147 |
+
if symbol not in kwds:
|
| 148 |
+
if locals is None or globals is None:
|
| 149 |
+
calling_frame = inspect.currentframe().f_back.f_back.f_back
|
| 150 |
+
if locals is None:
|
| 151 |
+
locals = calling_frame.f_locals
|
| 152 |
+
if globals is None:
|
| 153 |
+
globals = calling_frame.f_globals
|
| 154 |
+
if not isinstance(locals, dict):
|
| 155 |
+
# FrameLocalsProxy is stricter than dict on how it looks up keys
|
| 156 |
+
# and this means our "EncodedStrings" don't match the keys in locals.
|
| 157 |
+
# Therefore copy to a dict.
|
| 158 |
+
locals = dict(locals)
|
| 159 |
+
if symbol in locals:
|
| 160 |
+
kwds[symbol] = locals[symbol]
|
| 161 |
+
elif symbol in globals:
|
| 162 |
+
kwds[symbol] = globals[symbol]
|
| 163 |
+
else:
|
| 164 |
+
print("Couldn't find %r" % symbol)
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
def _inline_key(orig_code, arg_sigs, language_level):
|
| 168 |
+
key = orig_code, arg_sigs, sys.version_info, sys.executable, language_level, Cython.__version__
|
| 169 |
+
return hashlib.sha1(_unicode(key).encode('utf-8')).hexdigest()
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def cython_inline(code, get_type=unsafe_type,
|
| 173 |
+
lib_dir=os.path.join(get_cython_cache_dir(), 'inline'),
|
| 174 |
+
cython_include_dirs=None, cython_compiler_directives=None,
|
| 175 |
+
force=False, quiet=False, locals=None, globals=None, language_level=None, **kwds):
|
| 176 |
+
|
| 177 |
+
if get_type is None:
|
| 178 |
+
get_type = lambda x: 'object'
|
| 179 |
+
ctx = _create_context(tuple(cython_include_dirs)) if cython_include_dirs else _cython_inline_default_context
|
| 180 |
+
|
| 181 |
+
cython_compiler_directives = dict(cython_compiler_directives) if cython_compiler_directives else {}
|
| 182 |
+
if language_level is None and 'language_level' not in cython_compiler_directives:
|
| 183 |
+
language_level = '3str'
|
| 184 |
+
if language_level is not None:
|
| 185 |
+
cython_compiler_directives['language_level'] = language_level
|
| 186 |
+
|
| 187 |
+
key_hash = None
|
| 188 |
+
|
| 189 |
+
# Fast path if this has been called in this session.
|
| 190 |
+
_unbound_symbols = _cython_inline_cache.get(code)
|
| 191 |
+
if _unbound_symbols is not None:
|
| 192 |
+
_populate_unbound(kwds, _unbound_symbols, locals, globals)
|
| 193 |
+
args = sorted(kwds.items())
|
| 194 |
+
arg_sigs = tuple([(get_type(value, ctx), arg) for arg, value in args])
|
| 195 |
+
key_hash = _inline_key(code, arg_sigs, language_level)
|
| 196 |
+
invoke = _cython_inline_cache.get((code, arg_sigs, key_hash))
|
| 197 |
+
if invoke is not None:
|
| 198 |
+
arg_list = [arg[1] for arg in args]
|
| 199 |
+
return invoke(*arg_list)
|
| 200 |
+
|
| 201 |
+
orig_code = code
|
| 202 |
+
code = to_unicode(code)
|
| 203 |
+
code, literals = strip_string_literals(code)
|
| 204 |
+
code = strip_common_indent(code)
|
| 205 |
+
if locals is None:
|
| 206 |
+
locals = inspect.currentframe().f_back.f_back.f_locals
|
| 207 |
+
if globals is None:
|
| 208 |
+
globals = inspect.currentframe().f_back.f_back.f_globals
|
| 209 |
+
try:
|
| 210 |
+
_cython_inline_cache[orig_code] = _unbound_symbols = unbound_symbols(code)
|
| 211 |
+
_populate_unbound(kwds, _unbound_symbols, locals, globals)
|
| 212 |
+
except AssertionError:
|
| 213 |
+
if not quiet:
|
| 214 |
+
# Parsing from strings not fully supported (e.g. cimports).
|
| 215 |
+
print("Could not parse code as a string (to extract unbound symbols).")
|
| 216 |
+
|
| 217 |
+
cimports = []
|
| 218 |
+
for name, arg in list(kwds.items()):
|
| 219 |
+
if arg is cython_module:
|
| 220 |
+
cimports.append('\ncimport cython as %s' % name)
|
| 221 |
+
del kwds[name]
|
| 222 |
+
arg_names = sorted(kwds)
|
| 223 |
+
arg_sigs = tuple([(get_type(kwds[arg], ctx), arg) for arg in arg_names])
|
| 224 |
+
if key_hash is None:
|
| 225 |
+
key_hash = _inline_key(orig_code, arg_sigs, language_level)
|
| 226 |
+
module_name = "_cython_inline_" + key_hash
|
| 227 |
+
|
| 228 |
+
if module_name in sys.modules:
|
| 229 |
+
module = sys.modules[module_name]
|
| 230 |
+
|
| 231 |
+
else:
|
| 232 |
+
build_extension = None
|
| 233 |
+
if cython_inline.so_ext is None:
|
| 234 |
+
# Figure out and cache current extension suffix
|
| 235 |
+
build_extension = _get_build_extension()
|
| 236 |
+
cython_inline.so_ext = build_extension.get_ext_filename('')
|
| 237 |
+
|
| 238 |
+
lib_dir = os.path.abspath(lib_dir)
|
| 239 |
+
module_path = os.path.join(lib_dir, module_name + cython_inline.so_ext)
|
| 240 |
+
|
| 241 |
+
if not os.path.exists(lib_dir):
|
| 242 |
+
os.makedirs(lib_dir)
|
| 243 |
+
if force or not os.path.isfile(module_path):
|
| 244 |
+
cflags = []
|
| 245 |
+
define_macros = []
|
| 246 |
+
c_include_dirs = []
|
| 247 |
+
qualified = re.compile(r'([.\w]+)[.]')
|
| 248 |
+
for type, _ in arg_sigs:
|
| 249 |
+
m = qualified.match(type)
|
| 250 |
+
if m:
|
| 251 |
+
cimports.append('\ncimport %s' % m.groups()[0])
|
| 252 |
+
# one special case
|
| 253 |
+
if m.groups()[0] == 'numpy':
|
| 254 |
+
import numpy
|
| 255 |
+
c_include_dirs.append(numpy.get_include())
|
| 256 |
+
define_macros.append(("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION"))
|
| 257 |
+
# cflags.append('-Wno-unused')
|
| 258 |
+
module_body, func_body = extract_func_code(code)
|
| 259 |
+
params = ', '.join(['%s %s' % a for a in arg_sigs])
|
| 260 |
+
module_code = """
|
| 261 |
+
%(module_body)s
|
| 262 |
+
%(cimports)s
|
| 263 |
+
def __invoke(%(params)s):
|
| 264 |
+
%(func_body)s
|
| 265 |
+
return locals()
|
| 266 |
+
""" % {'cimports': '\n'.join(cimports),
|
| 267 |
+
'module_body': module_body,
|
| 268 |
+
'params': params,
|
| 269 |
+
'func_body': func_body }
|
| 270 |
+
for key, value in literals.items():
|
| 271 |
+
module_code = module_code.replace(key, value)
|
| 272 |
+
pyx_file = os.path.join(lib_dir, module_name + '.pyx')
|
| 273 |
+
fh = open(pyx_file, 'w')
|
| 274 |
+
try:
|
| 275 |
+
fh.write(module_code)
|
| 276 |
+
finally:
|
| 277 |
+
fh.close()
|
| 278 |
+
extension = Extension(
|
| 279 |
+
name=module_name,
|
| 280 |
+
sources=[pyx_file],
|
| 281 |
+
include_dirs=c_include_dirs or None,
|
| 282 |
+
extra_compile_args=cflags or None,
|
| 283 |
+
define_macros=define_macros or None,
|
| 284 |
+
)
|
| 285 |
+
if build_extension is None:
|
| 286 |
+
build_extension = _get_build_extension()
|
| 287 |
+
build_extension.extensions = cythonize(
|
| 288 |
+
[extension],
|
| 289 |
+
include_path=cython_include_dirs or ['.'],
|
| 290 |
+
compiler_directives=cython_compiler_directives,
|
| 291 |
+
quiet=quiet)
|
| 292 |
+
build_extension.build_temp = os.path.dirname(pyx_file)
|
| 293 |
+
build_extension.build_lib = lib_dir
|
| 294 |
+
build_extension.run()
|
| 295 |
+
|
| 296 |
+
if sys.platform == 'win32' and sys.version_info >= (3, 8):
|
| 297 |
+
with os.add_dll_directory(os.path.abspath(lib_dir)):
|
| 298 |
+
module = load_dynamic(module_name, module_path)
|
| 299 |
+
else:
|
| 300 |
+
module = load_dynamic(module_name, module_path)
|
| 301 |
+
|
| 302 |
+
_cython_inline_cache[orig_code, arg_sigs, key_hash] = module.__invoke
|
| 303 |
+
arg_list = [kwds[arg] for arg in arg_names]
|
| 304 |
+
return module.__invoke(*arg_list)
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
# Cached suffix used by cython_inline above. None should get
|
| 308 |
+
# overridden with actual value upon the first cython_inline invocation
|
| 309 |
+
cython_inline.so_ext = None
|
| 310 |
+
|
| 311 |
+
_find_non_space = re.compile('[^ ]').search
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
def strip_common_indent(code):
|
| 315 |
+
min_indent = None
|
| 316 |
+
lines = code.splitlines()
|
| 317 |
+
for line in lines:
|
| 318 |
+
match = _find_non_space(line)
|
| 319 |
+
if not match:
|
| 320 |
+
continue # blank
|
| 321 |
+
indent = match.start()
|
| 322 |
+
if line[indent] == '#':
|
| 323 |
+
continue # comment
|
| 324 |
+
if min_indent is None or min_indent > indent:
|
| 325 |
+
min_indent = indent
|
| 326 |
+
for ix, line in enumerate(lines):
|
| 327 |
+
match = _find_non_space(line)
|
| 328 |
+
if not match or not line or line[indent:indent+1] == '#':
|
| 329 |
+
continue
|
| 330 |
+
lines[ix] = line[min_indent:]
|
| 331 |
+
return '\n'.join(lines)
|
| 332 |
+
|
| 333 |
+
|
| 334 |
+
module_statement = re.compile(r'^((cdef +(extern|class))|cimport|(from .+ cimport)|(from .+ import +[*]))')
|
| 335 |
+
def extract_func_code(code):
|
| 336 |
+
module = []
|
| 337 |
+
function = []
|
| 338 |
+
current = function
|
| 339 |
+
code = code.replace('\t', ' ')
|
| 340 |
+
lines = code.split('\n')
|
| 341 |
+
for line in lines:
|
| 342 |
+
if not line.startswith(' '):
|
| 343 |
+
if module_statement.match(line):
|
| 344 |
+
current = module
|
| 345 |
+
else:
|
| 346 |
+
current = function
|
| 347 |
+
current.append(line)
|
| 348 |
+
return '\n'.join(module), ' ' + '\n '.join(function)
|
| 349 |
+
|
| 350 |
+
|
| 351 |
+
def get_body(source):
|
| 352 |
+
ix = source.index(':')
|
| 353 |
+
if source[:5] == 'lambda':
|
| 354 |
+
return "return %s" % source[ix+1:]
|
| 355 |
+
else:
|
| 356 |
+
return source[ix+1:]
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
# Lots to be done here... It would be especially cool if compiled functions
|
| 360 |
+
# could invoke each other quickly.
|
| 361 |
+
class RuntimeCompiledFunction(object):
|
| 362 |
+
|
| 363 |
+
def __init__(self, f):
|
| 364 |
+
self._f = f
|
| 365 |
+
self._body = get_body(inspect.getsource(f))
|
| 366 |
+
|
| 367 |
+
def __call__(self, *args, **kwds):
|
| 368 |
+
all = inspect.getcallargs(self._f, *args, **kwds)
|
| 369 |
+
if IS_PY3:
|
| 370 |
+
return cython_inline(self._body, locals=self._f.__globals__, globals=self._f.__globals__, **all)
|
| 371 |
+
else:
|
| 372 |
+
return cython_inline(self._body, locals=self._f.func_globals, globals=self._f.func_globals, **all)
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/IpythonMagic.py
ADDED
|
@@ -0,0 +1,572 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
=====================
|
| 4 |
+
Cython related magics
|
| 5 |
+
=====================
|
| 6 |
+
|
| 7 |
+
Magic command interface for interactive work with Cython
|
| 8 |
+
|
| 9 |
+
.. note::
|
| 10 |
+
|
| 11 |
+
The ``Cython`` package needs to be installed separately. It
|
| 12 |
+
can be obtained using ``easy_install`` or ``pip``.
|
| 13 |
+
|
| 14 |
+
Usage
|
| 15 |
+
=====
|
| 16 |
+
|
| 17 |
+
To enable the magics below, execute ``%load_ext cython``.
|
| 18 |
+
|
| 19 |
+
``%%cython``
|
| 20 |
+
|
| 21 |
+
{CYTHON_DOC}
|
| 22 |
+
|
| 23 |
+
``%%cython_inline``
|
| 24 |
+
|
| 25 |
+
{CYTHON_INLINE_DOC}
|
| 26 |
+
|
| 27 |
+
``%%cython_pyximport``
|
| 28 |
+
|
| 29 |
+
{CYTHON_PYXIMPORT_DOC}
|
| 30 |
+
|
| 31 |
+
Author:
|
| 32 |
+
* Brian Granger
|
| 33 |
+
|
| 34 |
+
Code moved from IPython and adapted by:
|
| 35 |
+
* Martín Gaitán
|
| 36 |
+
|
| 37 |
+
Parts of this code were taken from Cython.inline.
|
| 38 |
+
"""
|
| 39 |
+
#-----------------------------------------------------------------------------
|
| 40 |
+
# Copyright (C) 2010-2011, IPython Development Team.
|
| 41 |
+
#
|
| 42 |
+
# Distributed under the terms of the Modified BSD License.
|
| 43 |
+
#
|
| 44 |
+
# The full license is in the file ipython-COPYING.rst, distributed with this software.
|
| 45 |
+
#-----------------------------------------------------------------------------
|
| 46 |
+
|
| 47 |
+
from __future__ import absolute_import, print_function
|
| 48 |
+
|
| 49 |
+
import io
|
| 50 |
+
import os
|
| 51 |
+
import re
|
| 52 |
+
import sys
|
| 53 |
+
import time
|
| 54 |
+
import copy
|
| 55 |
+
import distutils.log
|
| 56 |
+
import textwrap
|
| 57 |
+
|
| 58 |
+
IO_ENCODING = sys.getfilesystemencoding()
|
| 59 |
+
IS_PY2 = sys.version_info[0] < 3
|
| 60 |
+
|
| 61 |
+
import hashlib
|
| 62 |
+
from distutils.core import Distribution, Extension
|
| 63 |
+
from distutils.command.build_ext import build_ext
|
| 64 |
+
|
| 65 |
+
from IPython.core import display
|
| 66 |
+
from IPython.core import magic_arguments
|
| 67 |
+
from IPython.core.magic import Magics, magics_class, cell_magic
|
| 68 |
+
try:
|
| 69 |
+
from IPython.paths import get_ipython_cache_dir
|
| 70 |
+
except ImportError:
|
| 71 |
+
# older IPython version
|
| 72 |
+
from IPython.utils.path import get_ipython_cache_dir
|
| 73 |
+
from IPython.utils.text import dedent
|
| 74 |
+
|
| 75 |
+
from ..Shadow import __version__ as cython_version
|
| 76 |
+
from ..Compiler.Errors import CompileError
|
| 77 |
+
from .Inline import cython_inline, load_dynamic
|
| 78 |
+
from .Dependencies import cythonize
|
| 79 |
+
from ..Utils import captured_fd, print_captured
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
PGO_CONFIG = {
|
| 83 |
+
'gcc': {
|
| 84 |
+
'gen': ['-fprofile-generate', '-fprofile-dir={TEMPDIR}'],
|
| 85 |
+
'use': ['-fprofile-use', '-fprofile-correction', '-fprofile-dir={TEMPDIR}'],
|
| 86 |
+
},
|
| 87 |
+
# blind copy from 'configure' script in CPython 3.7
|
| 88 |
+
'icc': {
|
| 89 |
+
'gen': ['-prof-gen'],
|
| 90 |
+
'use': ['-prof-use'],
|
| 91 |
+
}
|
| 92 |
+
}
|
| 93 |
+
PGO_CONFIG['mingw32'] = PGO_CONFIG['gcc']
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
if IS_PY2:
|
| 97 |
+
def encode_fs(name):
|
| 98 |
+
return name if isinstance(name, bytes) else name.encode(IO_ENCODING)
|
| 99 |
+
else:
|
| 100 |
+
def encode_fs(name):
|
| 101 |
+
return name
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
@magics_class
|
| 105 |
+
class CythonMagics(Magics):
|
| 106 |
+
|
| 107 |
+
def __init__(self, shell):
|
| 108 |
+
super(CythonMagics, self).__init__(shell)
|
| 109 |
+
self._reloads = {}
|
| 110 |
+
self._code_cache = {}
|
| 111 |
+
self._pyximport_installed = False
|
| 112 |
+
|
| 113 |
+
def _import_all(self, module):
|
| 114 |
+
mdict = module.__dict__
|
| 115 |
+
if '__all__' in mdict:
|
| 116 |
+
keys = mdict['__all__']
|
| 117 |
+
else:
|
| 118 |
+
keys = [k for k in mdict if not k.startswith('_')]
|
| 119 |
+
|
| 120 |
+
for k in keys:
|
| 121 |
+
try:
|
| 122 |
+
self.shell.push({k: mdict[k]})
|
| 123 |
+
except KeyError:
|
| 124 |
+
msg = "'module' object has no attribute '%s'" % k
|
| 125 |
+
raise AttributeError(msg)
|
| 126 |
+
|
| 127 |
+
@cell_magic
|
| 128 |
+
def cython_inline(self, line, cell):
|
| 129 |
+
"""Compile and run a Cython code cell using Cython.inline.
|
| 130 |
+
|
| 131 |
+
This magic simply passes the body of the cell to Cython.inline
|
| 132 |
+
and returns the result. If the variables `a` and `b` are defined
|
| 133 |
+
in the user's namespace, here is a simple example that returns
|
| 134 |
+
their sum::
|
| 135 |
+
|
| 136 |
+
%%cython_inline
|
| 137 |
+
return a+b
|
| 138 |
+
|
| 139 |
+
For most purposes, we recommend the usage of the `%%cython` magic.
|
| 140 |
+
"""
|
| 141 |
+
locs = self.shell.user_global_ns
|
| 142 |
+
globs = self.shell.user_ns
|
| 143 |
+
return cython_inline(cell, locals=locs, globals=globs)
|
| 144 |
+
|
| 145 |
+
@cell_magic
|
| 146 |
+
def cython_pyximport(self, line, cell):
|
| 147 |
+
"""Compile and import a Cython code cell using pyximport.
|
| 148 |
+
|
| 149 |
+
The contents of the cell are written to a `.pyx` file in the current
|
| 150 |
+
working directory, which is then imported using `pyximport`. This
|
| 151 |
+
magic requires a module name to be passed::
|
| 152 |
+
|
| 153 |
+
%%cython_pyximport modulename
|
| 154 |
+
def f(x):
|
| 155 |
+
return 2.0*x
|
| 156 |
+
|
| 157 |
+
The compiled module is then imported and all of its symbols are
|
| 158 |
+
injected into the user's namespace. For most purposes, we recommend
|
| 159 |
+
the usage of the `%%cython` magic.
|
| 160 |
+
"""
|
| 161 |
+
module_name = line.strip()
|
| 162 |
+
if not module_name:
|
| 163 |
+
raise ValueError('module name must be given')
|
| 164 |
+
fname = module_name + '.pyx'
|
| 165 |
+
with io.open(fname, 'w', encoding='utf-8') as f:
|
| 166 |
+
f.write(cell)
|
| 167 |
+
if 'pyximport' not in sys.modules or not self._pyximport_installed:
|
| 168 |
+
import pyximport
|
| 169 |
+
pyximport.install()
|
| 170 |
+
self._pyximport_installed = True
|
| 171 |
+
if module_name in self._reloads:
|
| 172 |
+
module = self._reloads[module_name]
|
| 173 |
+
# Note: reloading extension modules is not actually supported
|
| 174 |
+
# (requires PEP-489 reinitialisation support).
|
| 175 |
+
# Don't know why this should ever have worked as it reads here.
|
| 176 |
+
# All we really need to do is to update the globals below.
|
| 177 |
+
#reload(module)
|
| 178 |
+
else:
|
| 179 |
+
__import__(module_name)
|
| 180 |
+
module = sys.modules[module_name]
|
| 181 |
+
self._reloads[module_name] = module
|
| 182 |
+
self._import_all(module)
|
| 183 |
+
|
| 184 |
+
@magic_arguments.magic_arguments()
|
| 185 |
+
@magic_arguments.argument(
|
| 186 |
+
'-a', '--annotate', action='store_const', const='default', dest='annotate',
|
| 187 |
+
help="Produce a colorized HTML version of the source."
|
| 188 |
+
)
|
| 189 |
+
@magic_arguments.argument(
|
| 190 |
+
'--annotate-fullc', action='store_const', const='fullc', dest='annotate',
|
| 191 |
+
help="Produce a colorized HTML version of the source "
|
| 192 |
+
"which includes entire generated C/C++-code."
|
| 193 |
+
)
|
| 194 |
+
@magic_arguments.argument(
|
| 195 |
+
'-+', '--cplus', action='store_true', default=False,
|
| 196 |
+
help="Output a C++ rather than C file."
|
| 197 |
+
)
|
| 198 |
+
@magic_arguments.argument(
|
| 199 |
+
'-3', dest='language_level', action='store_const', const=3, default=None,
|
| 200 |
+
help="Select Python 3 syntax."
|
| 201 |
+
)
|
| 202 |
+
@magic_arguments.argument(
|
| 203 |
+
'-2', dest='language_level', action='store_const', const=2, default=None,
|
| 204 |
+
help="Select Python 2 syntax."
|
| 205 |
+
)
|
| 206 |
+
@magic_arguments.argument(
|
| 207 |
+
'-f', '--force', action='store_true', default=False,
|
| 208 |
+
help="Force the compilation of a new module, even if the source has been "
|
| 209 |
+
"previously compiled."
|
| 210 |
+
)
|
| 211 |
+
@magic_arguments.argument(
|
| 212 |
+
'-c', '--compile-args', action='append', default=[],
|
| 213 |
+
help="Extra flags to pass to compiler via the `extra_compile_args` "
|
| 214 |
+
"Extension flag (can be specified multiple times)."
|
| 215 |
+
)
|
| 216 |
+
@magic_arguments.argument(
|
| 217 |
+
'--link-args', action='append', default=[],
|
| 218 |
+
help="Extra flags to pass to linker via the `extra_link_args` "
|
| 219 |
+
"Extension flag (can be specified multiple times)."
|
| 220 |
+
)
|
| 221 |
+
@magic_arguments.argument(
|
| 222 |
+
'-l', '--lib', action='append', default=[],
|
| 223 |
+
help="Add a library to link the extension against (can be specified "
|
| 224 |
+
"multiple times)."
|
| 225 |
+
)
|
| 226 |
+
@magic_arguments.argument(
|
| 227 |
+
'-n', '--name',
|
| 228 |
+
help="Specify a name for the Cython module."
|
| 229 |
+
)
|
| 230 |
+
@magic_arguments.argument(
|
| 231 |
+
'-L', dest='library_dirs', metavar='dir', action='append', default=[],
|
| 232 |
+
help="Add a path to the list of library directories (can be specified "
|
| 233 |
+
"multiple times)."
|
| 234 |
+
)
|
| 235 |
+
@magic_arguments.argument(
|
| 236 |
+
'-I', '--include', action='append', default=[],
|
| 237 |
+
help="Add a path to the list of include directories (can be specified "
|
| 238 |
+
"multiple times)."
|
| 239 |
+
)
|
| 240 |
+
@magic_arguments.argument(
|
| 241 |
+
'-S', '--src', action='append', default=[],
|
| 242 |
+
help="Add a path to the list of src files (can be specified "
|
| 243 |
+
"multiple times)."
|
| 244 |
+
)
|
| 245 |
+
@magic_arguments.argument(
|
| 246 |
+
'--pgo', dest='pgo', action='store_true', default=False,
|
| 247 |
+
help=("Enable profile guided optimisation in the C compiler. "
|
| 248 |
+
"Compiles the cell twice and executes it in between to generate a runtime profile.")
|
| 249 |
+
)
|
| 250 |
+
@magic_arguments.argument(
|
| 251 |
+
'--verbose', dest='quiet', action='store_false', default=True,
|
| 252 |
+
help=("Print debug information like generated .c/.cpp file location "
|
| 253 |
+
"and exact gcc/g++ command invoked.")
|
| 254 |
+
)
|
| 255 |
+
@cell_magic
|
| 256 |
+
def cython(self, line, cell):
|
| 257 |
+
"""Compile and import everything from a Cython code cell.
|
| 258 |
+
|
| 259 |
+
The contents of the cell are written to a `.pyx` file in the
|
| 260 |
+
directory `IPYTHONDIR/cython` using a filename with the hash of the
|
| 261 |
+
code. This file is then cythonized and compiled. The resulting module
|
| 262 |
+
is imported and all of its symbols are injected into the user's
|
| 263 |
+
namespace. The usage is similar to that of `%%cython_pyximport` but
|
| 264 |
+
you don't have to pass a module name::
|
| 265 |
+
|
| 266 |
+
%%cython
|
| 267 |
+
def f(x):
|
| 268 |
+
return 2.0*x
|
| 269 |
+
|
| 270 |
+
To compile OpenMP codes, pass the required `--compile-args`
|
| 271 |
+
and `--link-args`. For example with gcc::
|
| 272 |
+
|
| 273 |
+
%%cython --compile-args=-fopenmp --link-args=-fopenmp
|
| 274 |
+
...
|
| 275 |
+
|
| 276 |
+
To enable profile guided optimisation, pass the ``--pgo`` option.
|
| 277 |
+
Note that the cell itself needs to take care of establishing a suitable
|
| 278 |
+
profile when executed. This can be done by implementing the functions to
|
| 279 |
+
optimise, and then calling them directly in the same cell on some realistic
|
| 280 |
+
training data like this::
|
| 281 |
+
|
| 282 |
+
%%cython --pgo
|
| 283 |
+
def critical_function(data):
|
| 284 |
+
for item in data:
|
| 285 |
+
...
|
| 286 |
+
|
| 287 |
+
# execute function several times to build profile
|
| 288 |
+
from somewhere import some_typical_data
|
| 289 |
+
for _ in range(100):
|
| 290 |
+
critical_function(some_typical_data)
|
| 291 |
+
|
| 292 |
+
In Python 3.5 and later, you can distinguish between the profile and
|
| 293 |
+
non-profile runs as follows::
|
| 294 |
+
|
| 295 |
+
if "_pgo_" in __name__:
|
| 296 |
+
... # execute critical code here
|
| 297 |
+
"""
|
| 298 |
+
args = magic_arguments.parse_argstring(self.cython, line)
|
| 299 |
+
code = cell if cell.endswith('\n') else cell + '\n'
|
| 300 |
+
lib_dir = os.path.join(get_ipython_cache_dir(), 'cython')
|
| 301 |
+
key = (code, line, sys.version_info, sys.executable, cython_version)
|
| 302 |
+
|
| 303 |
+
if not os.path.exists(lib_dir):
|
| 304 |
+
os.makedirs(lib_dir)
|
| 305 |
+
|
| 306 |
+
if args.pgo:
|
| 307 |
+
key += ('pgo',)
|
| 308 |
+
if args.force:
|
| 309 |
+
# Force a new module name by adding the current time to the
|
| 310 |
+
# key which is hashed to determine the module name.
|
| 311 |
+
key += (time.time(),)
|
| 312 |
+
|
| 313 |
+
if args.name:
|
| 314 |
+
module_name = str(args.name) # no-op in Py3
|
| 315 |
+
else:
|
| 316 |
+
module_name = "_cython_magic_" + hashlib.sha1(str(key).encode('utf-8')).hexdigest()
|
| 317 |
+
html_file = os.path.join(lib_dir, module_name + '.html')
|
| 318 |
+
module_path = os.path.join(lib_dir, module_name + self.so_ext)
|
| 319 |
+
|
| 320 |
+
have_module = os.path.isfile(module_path)
|
| 321 |
+
need_cythonize = args.pgo or not have_module
|
| 322 |
+
|
| 323 |
+
if args.annotate:
|
| 324 |
+
if not os.path.isfile(html_file):
|
| 325 |
+
need_cythonize = True
|
| 326 |
+
|
| 327 |
+
extension = None
|
| 328 |
+
if need_cythonize:
|
| 329 |
+
extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
|
| 330 |
+
if extensions is None:
|
| 331 |
+
# Compilation failed and printed error message
|
| 332 |
+
return None
|
| 333 |
+
assert len(extensions) == 1
|
| 334 |
+
extension = extensions[0]
|
| 335 |
+
self._code_cache[key] = module_name
|
| 336 |
+
|
| 337 |
+
if args.pgo:
|
| 338 |
+
self._profile_pgo_wrapper(extension, lib_dir)
|
| 339 |
+
|
| 340 |
+
def print_compiler_output(stdout, stderr, where):
|
| 341 |
+
# On windows, errors are printed to stdout, we redirect both to sys.stderr.
|
| 342 |
+
print_captured(stdout, where, u"Content of stdout:\n")
|
| 343 |
+
print_captured(stderr, where, u"Content of stderr:\n")
|
| 344 |
+
|
| 345 |
+
get_stderr = get_stdout = None
|
| 346 |
+
try:
|
| 347 |
+
with captured_fd(1) as get_stdout:
|
| 348 |
+
with captured_fd(2) as get_stderr:
|
| 349 |
+
self._build_extension(
|
| 350 |
+
extension, lib_dir, pgo_step_name='use' if args.pgo else None, quiet=args.quiet)
|
| 351 |
+
except (distutils.errors.CompileError, distutils.errors.LinkError):
|
| 352 |
+
# Build failed, print error message from compiler/linker
|
| 353 |
+
print_compiler_output(get_stdout(), get_stderr(), sys.stderr)
|
| 354 |
+
return None
|
| 355 |
+
|
| 356 |
+
# Build seems ok, but we might still want to show any warnings that occurred
|
| 357 |
+
print_compiler_output(get_stdout(), get_stderr(), sys.stdout)
|
| 358 |
+
|
| 359 |
+
module = load_dynamic(module_name, module_path)
|
| 360 |
+
self._import_all(module)
|
| 361 |
+
|
| 362 |
+
if args.annotate:
|
| 363 |
+
try:
|
| 364 |
+
with io.open(html_file, encoding='utf-8') as f:
|
| 365 |
+
annotated_html = f.read()
|
| 366 |
+
except IOError as e:
|
| 367 |
+
# File could not be opened. Most likely the user has a version
|
| 368 |
+
# of Cython before 0.15.1 (when `cythonize` learned the
|
| 369 |
+
# `force` keyword argument) and has already compiled this
|
| 370 |
+
# exact source without annotation.
|
| 371 |
+
print('Cython completed successfully but the annotated '
|
| 372 |
+
'source could not be read.', file=sys.stderr)
|
| 373 |
+
print(e, file=sys.stderr)
|
| 374 |
+
else:
|
| 375 |
+
return display.HTML(self.clean_annotated_html(annotated_html))
|
| 376 |
+
|
| 377 |
+
def _profile_pgo_wrapper(self, extension, lib_dir):
|
| 378 |
+
"""
|
| 379 |
+
Generate a .c file for a separate extension module that calls the
|
| 380 |
+
module init function of the original module. This makes sure that the
|
| 381 |
+
PGO profiler sees the correct .o file of the final module, but it still
|
| 382 |
+
allows us to import the module under a different name for profiling,
|
| 383 |
+
before recompiling it into the PGO optimised module. Overwriting and
|
| 384 |
+
reimporting the same shared library is not portable.
|
| 385 |
+
"""
|
| 386 |
+
extension = copy.copy(extension) # shallow copy, do not modify sources in place!
|
| 387 |
+
module_name = extension.name
|
| 388 |
+
pgo_module_name = '_pgo_' + module_name
|
| 389 |
+
pgo_wrapper_c_file = os.path.join(lib_dir, pgo_module_name + '.c')
|
| 390 |
+
with io.open(pgo_wrapper_c_file, 'w', encoding='utf-8') as f:
|
| 391 |
+
f.write(textwrap.dedent(u"""
|
| 392 |
+
#include "Python.h"
|
| 393 |
+
#if PY_MAJOR_VERSION < 3
|
| 394 |
+
extern PyMODINIT_FUNC init%(module_name)s(void);
|
| 395 |
+
PyMODINIT_FUNC init%(pgo_module_name)s(void); /*proto*/
|
| 396 |
+
PyMODINIT_FUNC init%(pgo_module_name)s(void) {
|
| 397 |
+
PyObject *sys_modules;
|
| 398 |
+
init%(module_name)s(); if (PyErr_Occurred()) return;
|
| 399 |
+
sys_modules = PyImport_GetModuleDict(); /* borrowed, no exception, "never" fails */
|
| 400 |
+
if (sys_modules) {
|
| 401 |
+
PyObject *module = PyDict_GetItemString(sys_modules, "%(module_name)s"); if (!module) return;
|
| 402 |
+
PyDict_SetItemString(sys_modules, "%(pgo_module_name)s", module);
|
| 403 |
+
Py_DECREF(module);
|
| 404 |
+
}
|
| 405 |
+
}
|
| 406 |
+
#else
|
| 407 |
+
extern PyMODINIT_FUNC PyInit_%(module_name)s(void);
|
| 408 |
+
PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void); /*proto*/
|
| 409 |
+
PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void) {
|
| 410 |
+
return PyInit_%(module_name)s();
|
| 411 |
+
}
|
| 412 |
+
#endif
|
| 413 |
+
""" % {'module_name': module_name, 'pgo_module_name': pgo_module_name}))
|
| 414 |
+
|
| 415 |
+
extension.sources = extension.sources + [pgo_wrapper_c_file] # do not modify in place!
|
| 416 |
+
extension.name = pgo_module_name
|
| 417 |
+
|
| 418 |
+
self._build_extension(extension, lib_dir, pgo_step_name='gen')
|
| 419 |
+
|
| 420 |
+
# import and execute module code to generate profile
|
| 421 |
+
so_module_path = os.path.join(lib_dir, pgo_module_name + self.so_ext)
|
| 422 |
+
load_dynamic(pgo_module_name, so_module_path)
|
| 423 |
+
|
| 424 |
+
def _cythonize(self, module_name, code, lib_dir, args, quiet=True):
|
| 425 |
+
pyx_file = os.path.join(lib_dir, module_name + '.pyx')
|
| 426 |
+
pyx_file = encode_fs(pyx_file)
|
| 427 |
+
|
| 428 |
+
c_include_dirs = args.include
|
| 429 |
+
c_src_files = list(map(str, args.src))
|
| 430 |
+
if 'numpy' in code:
|
| 431 |
+
import numpy
|
| 432 |
+
c_include_dirs.append(numpy.get_include())
|
| 433 |
+
with io.open(pyx_file, 'w', encoding='utf-8') as f:
|
| 434 |
+
f.write(code)
|
| 435 |
+
extension = Extension(
|
| 436 |
+
name=module_name,
|
| 437 |
+
sources=[pyx_file] + c_src_files,
|
| 438 |
+
include_dirs=c_include_dirs,
|
| 439 |
+
library_dirs=args.library_dirs,
|
| 440 |
+
extra_compile_args=args.compile_args,
|
| 441 |
+
extra_link_args=args.link_args,
|
| 442 |
+
libraries=args.lib,
|
| 443 |
+
language='c++' if args.cplus else 'c',
|
| 444 |
+
)
|
| 445 |
+
try:
|
| 446 |
+
opts = dict(
|
| 447 |
+
quiet=quiet,
|
| 448 |
+
annotate=args.annotate,
|
| 449 |
+
force=True,
|
| 450 |
+
language_level=min(3, sys.version_info[0]),
|
| 451 |
+
)
|
| 452 |
+
if args.language_level is not None:
|
| 453 |
+
assert args.language_level in (2, 3)
|
| 454 |
+
opts['language_level'] = args.language_level
|
| 455 |
+
return cythonize([extension], **opts)
|
| 456 |
+
except CompileError:
|
| 457 |
+
return None
|
| 458 |
+
|
| 459 |
+
def _build_extension(self, extension, lib_dir, temp_dir=None, pgo_step_name=None, quiet=True):
|
| 460 |
+
build_extension = self._get_build_extension(
|
| 461 |
+
extension, lib_dir=lib_dir, temp_dir=temp_dir, pgo_step_name=pgo_step_name)
|
| 462 |
+
old_threshold = None
|
| 463 |
+
try:
|
| 464 |
+
if not quiet:
|
| 465 |
+
old_threshold = distutils.log.set_threshold(distutils.log.DEBUG)
|
| 466 |
+
build_extension.run()
|
| 467 |
+
finally:
|
| 468 |
+
if not quiet and old_threshold is not None:
|
| 469 |
+
distutils.log.set_threshold(old_threshold)
|
| 470 |
+
|
| 471 |
+
def _add_pgo_flags(self, build_extension, step_name, temp_dir):
|
| 472 |
+
compiler_type = build_extension.compiler.compiler_type
|
| 473 |
+
if compiler_type == 'unix':
|
| 474 |
+
compiler_cmd = build_extension.compiler.compiler_so
|
| 475 |
+
# TODO: we could try to call "[cmd] --version" for better insights
|
| 476 |
+
if not compiler_cmd:
|
| 477 |
+
pass
|
| 478 |
+
elif 'clang' in compiler_cmd or 'clang' in compiler_cmd[0]:
|
| 479 |
+
compiler_type = 'clang'
|
| 480 |
+
elif 'icc' in compiler_cmd or 'icc' in compiler_cmd[0]:
|
| 481 |
+
compiler_type = 'icc'
|
| 482 |
+
elif 'gcc' in compiler_cmd or 'gcc' in compiler_cmd[0]:
|
| 483 |
+
compiler_type = 'gcc'
|
| 484 |
+
elif 'g++' in compiler_cmd or 'g++' in compiler_cmd[0]:
|
| 485 |
+
compiler_type = 'gcc'
|
| 486 |
+
config = PGO_CONFIG.get(compiler_type)
|
| 487 |
+
orig_flags = []
|
| 488 |
+
if config and step_name in config:
|
| 489 |
+
flags = [f.format(TEMPDIR=temp_dir) for f in config[step_name]]
|
| 490 |
+
for extension in build_extension.extensions:
|
| 491 |
+
orig_flags.append((extension.extra_compile_args, extension.extra_link_args))
|
| 492 |
+
extension.extra_compile_args = extension.extra_compile_args + flags
|
| 493 |
+
extension.extra_link_args = extension.extra_link_args + flags
|
| 494 |
+
else:
|
| 495 |
+
print("No PGO %s configuration known for C compiler type '%s'" % (step_name, compiler_type),
|
| 496 |
+
file=sys.stderr)
|
| 497 |
+
return orig_flags
|
| 498 |
+
|
| 499 |
+
@property
|
| 500 |
+
def so_ext(self):
|
| 501 |
+
"""The extension suffix for compiled modules."""
|
| 502 |
+
try:
|
| 503 |
+
return self._so_ext
|
| 504 |
+
except AttributeError:
|
| 505 |
+
self._so_ext = self._get_build_extension().get_ext_filename('')
|
| 506 |
+
return self._so_ext
|
| 507 |
+
|
| 508 |
+
def _clear_distutils_mkpath_cache(self):
|
| 509 |
+
"""clear distutils mkpath cache
|
| 510 |
+
|
| 511 |
+
prevents distutils from skipping re-creation of dirs that have been removed
|
| 512 |
+
"""
|
| 513 |
+
try:
|
| 514 |
+
from distutils.dir_util import _path_created
|
| 515 |
+
except ImportError:
|
| 516 |
+
pass
|
| 517 |
+
else:
|
| 518 |
+
_path_created.clear()
|
| 519 |
+
|
| 520 |
+
def _get_build_extension(self, extension=None, lib_dir=None, temp_dir=None,
|
| 521 |
+
pgo_step_name=None, _build_ext=build_ext):
|
| 522 |
+
self._clear_distutils_mkpath_cache()
|
| 523 |
+
dist = Distribution()
|
| 524 |
+
config_files = dist.find_config_files()
|
| 525 |
+
try:
|
| 526 |
+
config_files.remove('setup.cfg')
|
| 527 |
+
except ValueError:
|
| 528 |
+
pass
|
| 529 |
+
dist.parse_config_files(config_files)
|
| 530 |
+
|
| 531 |
+
if not temp_dir:
|
| 532 |
+
temp_dir = lib_dir
|
| 533 |
+
add_pgo_flags = self._add_pgo_flags
|
| 534 |
+
|
| 535 |
+
if pgo_step_name:
|
| 536 |
+
base_build_ext = _build_ext
|
| 537 |
+
class _build_ext(_build_ext):
|
| 538 |
+
def build_extensions(self):
|
| 539 |
+
add_pgo_flags(self, pgo_step_name, temp_dir)
|
| 540 |
+
base_build_ext.build_extensions(self)
|
| 541 |
+
|
| 542 |
+
build_extension = _build_ext(dist)
|
| 543 |
+
build_extension.finalize_options()
|
| 544 |
+
if temp_dir:
|
| 545 |
+
temp_dir = encode_fs(temp_dir)
|
| 546 |
+
build_extension.build_temp = temp_dir
|
| 547 |
+
if lib_dir:
|
| 548 |
+
lib_dir = encode_fs(lib_dir)
|
| 549 |
+
build_extension.build_lib = lib_dir
|
| 550 |
+
if extension is not None:
|
| 551 |
+
build_extension.extensions = [extension]
|
| 552 |
+
return build_extension
|
| 553 |
+
|
| 554 |
+
@staticmethod
|
| 555 |
+
def clean_annotated_html(html):
|
| 556 |
+
"""Clean up the annotated HTML source.
|
| 557 |
+
|
| 558 |
+
Strips the link to the generated C or C++ file, which we do not
|
| 559 |
+
present to the user.
|
| 560 |
+
"""
|
| 561 |
+
r = re.compile('<p>Raw output: <a href="(.*)">(.*)</a>')
|
| 562 |
+
html = '\n'.join(l for l in html.splitlines() if not r.match(l))
|
| 563 |
+
return html
|
| 564 |
+
|
| 565 |
+
__doc__ = __doc__.format(
|
| 566 |
+
# rST doesn't see the -+ flag as part of an option list, so we
|
| 567 |
+
# hide it from the module-level docstring.
|
| 568 |
+
CYTHON_DOC=dedent(CythonMagics.cython.__doc__
|
| 569 |
+
.replace('-+, --cplus', '--cplus ')),
|
| 570 |
+
CYTHON_INLINE_DOC=dedent(CythonMagics.cython_inline.__doc__),
|
| 571 |
+
CYTHON_PYXIMPORT_DOC=dedent(CythonMagics.cython_pyximport.__doc__),
|
| 572 |
+
)
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestCyCache.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import difflib
|
| 2 |
+
import glob
|
| 3 |
+
import gzip
|
| 4 |
+
import os
|
| 5 |
+
import sys
|
| 6 |
+
import tempfile
|
| 7 |
+
import unittest
|
| 8 |
+
|
| 9 |
+
import Cython.Build.Dependencies
|
| 10 |
+
import Cython.Utils
|
| 11 |
+
from Cython.TestUtils import CythonTest
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
class TestCyCache(CythonTest):
|
| 15 |
+
|
| 16 |
+
def setUp(self):
|
| 17 |
+
CythonTest.setUp(self)
|
| 18 |
+
self.temp_dir = tempfile.mkdtemp(
|
| 19 |
+
prefix='cycache-test',
|
| 20 |
+
dir='TEST_TMP' if os.path.isdir('TEST_TMP') else None)
|
| 21 |
+
self.src_dir = tempfile.mkdtemp(prefix='src', dir=self.temp_dir)
|
| 22 |
+
self.cache_dir = tempfile.mkdtemp(prefix='cache', dir=self.temp_dir)
|
| 23 |
+
|
| 24 |
+
def cache_files(self, file_glob):
|
| 25 |
+
return glob.glob(os.path.join(self.cache_dir, file_glob))
|
| 26 |
+
|
| 27 |
+
def fresh_cythonize(self, *args, **kwargs):
|
| 28 |
+
Cython.Utils.clear_function_caches()
|
| 29 |
+
Cython.Build.Dependencies._dep_tree = None # discard method caches
|
| 30 |
+
Cython.Build.Dependencies.cythonize(*args, **kwargs)
|
| 31 |
+
|
| 32 |
+
def test_cycache_switch(self):
|
| 33 |
+
content1 = 'value = 1\n'
|
| 34 |
+
content2 = 'value = 2\n'
|
| 35 |
+
a_pyx = os.path.join(self.src_dir, 'a.pyx')
|
| 36 |
+
a_c = a_pyx[:-4] + '.c'
|
| 37 |
+
|
| 38 |
+
with open(a_pyx, 'w') as f:
|
| 39 |
+
f.write(content1)
|
| 40 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 41 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 42 |
+
self.assertEqual(1, len(self.cache_files('a.c*')))
|
| 43 |
+
with open(a_c) as f:
|
| 44 |
+
a_contents1 = f.read()
|
| 45 |
+
os.unlink(a_c)
|
| 46 |
+
|
| 47 |
+
with open(a_pyx, 'w') as f:
|
| 48 |
+
f.write(content2)
|
| 49 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 50 |
+
with open(a_c) as f:
|
| 51 |
+
a_contents2 = f.read()
|
| 52 |
+
os.unlink(a_c)
|
| 53 |
+
|
| 54 |
+
self.assertNotEqual(a_contents1, a_contents2, 'C file not changed!')
|
| 55 |
+
self.assertEqual(2, len(self.cache_files('a.c*')))
|
| 56 |
+
|
| 57 |
+
with open(a_pyx, 'w') as f:
|
| 58 |
+
f.write(content1)
|
| 59 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 60 |
+
self.assertEqual(2, len(self.cache_files('a.c*')))
|
| 61 |
+
with open(a_c) as f:
|
| 62 |
+
a_contents = f.read()
|
| 63 |
+
self.assertEqual(
|
| 64 |
+
a_contents, a_contents1,
|
| 65 |
+
msg='\n'.join(list(difflib.unified_diff(
|
| 66 |
+
a_contents.split('\n'), a_contents1.split('\n')))[:10]))
|
| 67 |
+
|
| 68 |
+
def test_cycache_uses_cache(self):
|
| 69 |
+
a_pyx = os.path.join(self.src_dir, 'a.pyx')
|
| 70 |
+
a_c = a_pyx[:-4] + '.c'
|
| 71 |
+
with open(a_pyx, 'w') as f:
|
| 72 |
+
f.write('pass')
|
| 73 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 74 |
+
a_cache = os.path.join(self.cache_dir, os.listdir(self.cache_dir)[0])
|
| 75 |
+
with gzip.GzipFile(a_cache, 'wb') as gzipfile:
|
| 76 |
+
gzipfile.write('fake stuff'.encode('ascii'))
|
| 77 |
+
os.unlink(a_c)
|
| 78 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 79 |
+
with open(a_c) as f:
|
| 80 |
+
a_contents = f.read()
|
| 81 |
+
self.assertEqual(a_contents, 'fake stuff',
|
| 82 |
+
'Unexpected contents: %s...' % a_contents[:100])
|
| 83 |
+
|
| 84 |
+
def test_multi_file_output(self):
|
| 85 |
+
a_pyx = os.path.join(self.src_dir, 'a.pyx')
|
| 86 |
+
a_c = a_pyx[:-4] + '.c'
|
| 87 |
+
a_h = a_pyx[:-4] + '.h'
|
| 88 |
+
a_api_h = a_pyx[:-4] + '_api.h'
|
| 89 |
+
with open(a_pyx, 'w') as f:
|
| 90 |
+
f.write('cdef public api int foo(int x): return x\n')
|
| 91 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 92 |
+
expected = [a_c, a_h, a_api_h]
|
| 93 |
+
for output in expected:
|
| 94 |
+
self.assertTrue(os.path.exists(output), output)
|
| 95 |
+
os.unlink(output)
|
| 96 |
+
self.fresh_cythonize(a_pyx, cache=self.cache_dir)
|
| 97 |
+
for output in expected:
|
| 98 |
+
self.assertTrue(os.path.exists(output), output)
|
| 99 |
+
|
| 100 |
+
def test_options_invalidation(self):
|
| 101 |
+
hash_pyx = os.path.join(self.src_dir, 'options.pyx')
|
| 102 |
+
hash_c = hash_pyx[:-len('.pyx')] + '.c'
|
| 103 |
+
|
| 104 |
+
with open(hash_pyx, 'w') as f:
|
| 105 |
+
f.write('pass')
|
| 106 |
+
self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=False)
|
| 107 |
+
self.assertEqual(1, len(self.cache_files('options.c*')))
|
| 108 |
+
|
| 109 |
+
os.unlink(hash_c)
|
| 110 |
+
self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=True)
|
| 111 |
+
self.assertEqual(2, len(self.cache_files('options.c*')))
|
| 112 |
+
|
| 113 |
+
os.unlink(hash_c)
|
| 114 |
+
self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=False, show_version=False)
|
| 115 |
+
self.assertEqual(2, len(self.cache_files('options.c*')))
|
| 116 |
+
|
| 117 |
+
os.unlink(hash_c)
|
| 118 |
+
self.fresh_cythonize(hash_pyx, cache=self.cache_dir, cplus=False, show_version=True)
|
| 119 |
+
self.assertEqual(2, len(self.cache_files('options.c*')))
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestCythonizeArgsParser.py
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from Cython.Build.Cythonize import (
|
| 2 |
+
create_args_parser, parse_args_raw, parse_args,
|
| 3 |
+
parallel_compiles
|
| 4 |
+
)
|
| 5 |
+
|
| 6 |
+
from Cython.Compiler import Options
|
| 7 |
+
from Cython.Compiler.Tests.Utils import backup_Options, restore_Options, check_global_options
|
| 8 |
+
|
| 9 |
+
from unittest import TestCase
|
| 10 |
+
|
| 11 |
+
import sys
|
| 12 |
+
try:
|
| 13 |
+
from StringIO import StringIO
|
| 14 |
+
except ImportError:
|
| 15 |
+
from io import StringIO # doesn't accept 'str' in Py2
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class TestCythonizeArgsParser(TestCase):
|
| 19 |
+
|
| 20 |
+
def setUp(self):
|
| 21 |
+
TestCase.setUp(self)
|
| 22 |
+
self.parse_args = lambda x, parser=create_args_parser() : parse_args_raw(parser, x)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def are_default(self, options, skip):
|
| 26 |
+
# empty containers
|
| 27 |
+
empty_containers = ['directives', 'compile_time_env', 'options', 'excludes']
|
| 28 |
+
are_none = ['language_level', 'annotate', 'build', 'build_inplace', 'force', 'quiet', 'lenient', 'keep_going', 'no_docstrings']
|
| 29 |
+
for opt_name in empty_containers:
|
| 30 |
+
if len(getattr(options, opt_name))!=0 and (opt_name not in skip):
|
| 31 |
+
self.assertEqual(opt_name,"", msg="For option "+opt_name)
|
| 32 |
+
return False
|
| 33 |
+
for opt_name in are_none:
|
| 34 |
+
if (getattr(options, opt_name) is not None) and (opt_name not in skip):
|
| 35 |
+
self.assertEqual(opt_name,"", msg="For option "+opt_name)
|
| 36 |
+
return False
|
| 37 |
+
if options.parallel!=parallel_compiles and ('parallel' not in skip):
|
| 38 |
+
return False
|
| 39 |
+
return True
|
| 40 |
+
|
| 41 |
+
# testing directives:
|
| 42 |
+
def test_directive_short(self):
|
| 43 |
+
options, args = self.parse_args(['-X', 'cdivision=True'])
|
| 44 |
+
self.assertFalse(args)
|
| 45 |
+
self.assertTrue(self.are_default(options, ['directives']))
|
| 46 |
+
self.assertEqual(options.directives['cdivision'], True)
|
| 47 |
+
|
| 48 |
+
def test_directive_long(self):
|
| 49 |
+
options, args = self.parse_args(['--directive', 'cdivision=True'])
|
| 50 |
+
self.assertFalse(args)
|
| 51 |
+
self.assertTrue(self.are_default(options, ['directives']))
|
| 52 |
+
self.assertEqual(options.directives['cdivision'], True)
|
| 53 |
+
|
| 54 |
+
def test_directive_multiple(self):
|
| 55 |
+
options, args = self.parse_args(['-X', 'cdivision=True', '-X', 'c_string_type=bytes'])
|
| 56 |
+
self.assertFalse(args)
|
| 57 |
+
self.assertTrue(self.are_default(options, ['directives']))
|
| 58 |
+
self.assertEqual(options.directives['cdivision'], True)
|
| 59 |
+
self.assertEqual(options.directives['c_string_type'], 'bytes')
|
| 60 |
+
|
| 61 |
+
def test_directive_multiple_v2(self):
|
| 62 |
+
options, args = self.parse_args(['-X', 'cdivision=True,c_string_type=bytes'])
|
| 63 |
+
self.assertFalse(args)
|
| 64 |
+
self.assertTrue(self.are_default(options, ['directives']))
|
| 65 |
+
self.assertEqual(options.directives['cdivision'], True)
|
| 66 |
+
self.assertEqual(options.directives['c_string_type'], 'bytes')
|
| 67 |
+
|
| 68 |
+
def test_directive_value_yes(self):
|
| 69 |
+
options, args = self.parse_args(['-X', 'cdivision=YeS'])
|
| 70 |
+
self.assertFalse(args)
|
| 71 |
+
self.assertTrue(self.are_default(options, ['directives']))
|
| 72 |
+
self.assertEqual(options.directives['cdivision'], True)
|
| 73 |
+
|
| 74 |
+
def test_directive_value_no(self):
|
| 75 |
+
options, args = self.parse_args(['-X', 'cdivision=no'])
|
| 76 |
+
self.assertFalse(args)
|
| 77 |
+
self.assertTrue(self.are_default(options, ['directives']))
|
| 78 |
+
self.assertEqual(options.directives['cdivision'], False)
|
| 79 |
+
|
| 80 |
+
def test_directive_value_invalid(self):
|
| 81 |
+
with self.assertRaises(ValueError) as context:
|
| 82 |
+
options, args = self.parse_args(['-X', 'cdivision=sadfasd'])
|
| 83 |
+
|
| 84 |
+
def test_directive_key_invalid(self):
|
| 85 |
+
with self.assertRaises(ValueError) as context:
|
| 86 |
+
options, args = self.parse_args(['-X', 'abracadabra'])
|
| 87 |
+
|
| 88 |
+
def test_directive_no_value(self):
|
| 89 |
+
with self.assertRaises(ValueError) as context:
|
| 90 |
+
options, args = self.parse_args(['-X', 'cdivision'])
|
| 91 |
+
|
| 92 |
+
def test_directives_types(self):
|
| 93 |
+
directives = {
|
| 94 |
+
'auto_pickle': True,
|
| 95 |
+
'c_string_type': 'bytearray',
|
| 96 |
+
'c_string_type': 'bytes',
|
| 97 |
+
'c_string_type': 'str',
|
| 98 |
+
'c_string_type': 'bytearray',
|
| 99 |
+
'c_string_type': 'unicode',
|
| 100 |
+
'c_string_encoding' : 'ascii',
|
| 101 |
+
'language_level' : 2,
|
| 102 |
+
'language_level' : 3,
|
| 103 |
+
'language_level' : '3str',
|
| 104 |
+
'set_initial_path' : 'my_initial_path',
|
| 105 |
+
}
|
| 106 |
+
for key, value in directives.items():
|
| 107 |
+
cmd = '{key}={value}'.format(key=key, value=str(value))
|
| 108 |
+
options, args = self.parse_args(['-X', cmd])
|
| 109 |
+
self.assertFalse(args)
|
| 110 |
+
self.assertTrue(self.are_default(options, ['directives']), msg = "Error for option: "+cmd)
|
| 111 |
+
self.assertEqual(options.directives[key], value, msg = "Error for option: "+cmd)
|
| 112 |
+
|
| 113 |
+
def test_directives_wrong(self):
|
| 114 |
+
directives = {
|
| 115 |
+
'auto_pickle': 42, # for bool type
|
| 116 |
+
'auto_pickle': 'NONONO', # for bool type
|
| 117 |
+
'c_string_type': 'bites',
|
| 118 |
+
#'c_string_encoding' : 'a',
|
| 119 |
+
#'language_level' : 4,
|
| 120 |
+
}
|
| 121 |
+
for key, value in directives.items():
|
| 122 |
+
cmd = '{key}={value}'.format(key=key, value=str(value))
|
| 123 |
+
with self.assertRaises(ValueError, msg = "Error for option: "+cmd) as context:
|
| 124 |
+
options, args = self.parse_args(['-X', cmd])
|
| 125 |
+
|
| 126 |
+
def test_compile_time_env_short(self):
|
| 127 |
+
options, args = self.parse_args(['-E', 'MYSIZE=10'])
|
| 128 |
+
self.assertFalse(args)
|
| 129 |
+
self.assertTrue(self.are_default(options, ['compile_time_env']))
|
| 130 |
+
self.assertEqual(options.compile_time_env['MYSIZE'], 10)
|
| 131 |
+
|
| 132 |
+
def test_compile_time_env_long(self):
|
| 133 |
+
options, args = self.parse_args(['--compile-time-env', 'MYSIZE=10'])
|
| 134 |
+
self.assertFalse(args)
|
| 135 |
+
self.assertTrue(self.are_default(options, ['compile_time_env']))
|
| 136 |
+
self.assertEqual(options.compile_time_env['MYSIZE'], 10)
|
| 137 |
+
|
| 138 |
+
def test_compile_time_env_multiple(self):
|
| 139 |
+
options, args = self.parse_args(['-E', 'MYSIZE=10', '-E', 'ARRSIZE=11'])
|
| 140 |
+
self.assertFalse(args)
|
| 141 |
+
self.assertTrue(self.are_default(options, ['compile_time_env']))
|
| 142 |
+
self.assertEqual(options.compile_time_env['MYSIZE'], 10)
|
| 143 |
+
self.assertEqual(options.compile_time_env['ARRSIZE'], 11)
|
| 144 |
+
|
| 145 |
+
def test_compile_time_env_multiple_v2(self):
|
| 146 |
+
options, args = self.parse_args(['-E', 'MYSIZE=10,ARRSIZE=11'])
|
| 147 |
+
self.assertFalse(args)
|
| 148 |
+
self.assertTrue(self.are_default(options, ['compile_time_env']))
|
| 149 |
+
self.assertEqual(options.compile_time_env['MYSIZE'], 10)
|
| 150 |
+
self.assertEqual(options.compile_time_env['ARRSIZE'], 11)
|
| 151 |
+
|
| 152 |
+
#testing options
|
| 153 |
+
def test_option_short(self):
|
| 154 |
+
options, args = self.parse_args(['-s', 'docstrings=True'])
|
| 155 |
+
self.assertFalse(args)
|
| 156 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 157 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 158 |
+
|
| 159 |
+
def test_option_long(self):
|
| 160 |
+
options, args = self.parse_args(['--option', 'docstrings=True'])
|
| 161 |
+
self.assertFalse(args)
|
| 162 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 163 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 164 |
+
|
| 165 |
+
def test_option_multiple(self):
|
| 166 |
+
options, args = self.parse_args(['-s', 'docstrings=True', '-s', 'buffer_max_dims=8'])
|
| 167 |
+
self.assertFalse(args)
|
| 168 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 169 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 170 |
+
self.assertEqual(options.options['buffer_max_dims'], True) # really?
|
| 171 |
+
|
| 172 |
+
def test_option_multiple_v2(self):
|
| 173 |
+
options, args = self.parse_args(['-s', 'docstrings=True,buffer_max_dims=8'])
|
| 174 |
+
self.assertFalse(args)
|
| 175 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 176 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 177 |
+
self.assertEqual(options.options['buffer_max_dims'], True) # really?
|
| 178 |
+
|
| 179 |
+
def test_option_value_yes(self):
|
| 180 |
+
options, args = self.parse_args(['-s', 'docstrings=YeS'])
|
| 181 |
+
self.assertFalse(args)
|
| 182 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 183 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 184 |
+
|
| 185 |
+
def test_option_value_4242(self):
|
| 186 |
+
options, args = self.parse_args(['-s', 'docstrings=4242'])
|
| 187 |
+
self.assertFalse(args)
|
| 188 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 189 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 190 |
+
|
| 191 |
+
def test_option_value_0(self):
|
| 192 |
+
options, args = self.parse_args(['-s', 'docstrings=0'])
|
| 193 |
+
self.assertFalse(args)
|
| 194 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 195 |
+
self.assertEqual(options.options['docstrings'], False)
|
| 196 |
+
|
| 197 |
+
def test_option_value_emptystr(self):
|
| 198 |
+
options, args = self.parse_args(['-s', 'docstrings='])
|
| 199 |
+
self.assertFalse(args)
|
| 200 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 201 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 202 |
+
|
| 203 |
+
def test_option_value_a_str(self):
|
| 204 |
+
options, args = self.parse_args(['-s', 'docstrings=BB'])
|
| 205 |
+
self.assertFalse(args)
|
| 206 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 207 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 208 |
+
|
| 209 |
+
def test_option_value_no(self):
|
| 210 |
+
options, args = self.parse_args(['-s', 'docstrings=nO'])
|
| 211 |
+
self.assertFalse(args)
|
| 212 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 213 |
+
self.assertEqual(options.options['docstrings'], False)
|
| 214 |
+
|
| 215 |
+
def test_option_no_value(self):
|
| 216 |
+
options, args = self.parse_args(['-s', 'docstrings'])
|
| 217 |
+
self.assertFalse(args)
|
| 218 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 219 |
+
self.assertEqual(options.options['docstrings'], True)
|
| 220 |
+
|
| 221 |
+
def test_option_any_key(self):
|
| 222 |
+
options, args = self.parse_args(['-s', 'abracadabra'])
|
| 223 |
+
self.assertFalse(args)
|
| 224 |
+
self.assertTrue(self.are_default(options, ['options']))
|
| 225 |
+
self.assertEqual(options.options['abracadabra'], True)
|
| 226 |
+
|
| 227 |
+
def test_language_level_2(self):
|
| 228 |
+
options, args = self.parse_args(['-2'])
|
| 229 |
+
self.assertFalse(args)
|
| 230 |
+
self.assertTrue(self.are_default(options, ['language_level']))
|
| 231 |
+
self.assertEqual(options.language_level, 2)
|
| 232 |
+
|
| 233 |
+
def test_language_level_3(self):
|
| 234 |
+
options, args = self.parse_args(['-3'])
|
| 235 |
+
self.assertFalse(args)
|
| 236 |
+
self.assertTrue(self.are_default(options, ['language_level']))
|
| 237 |
+
self.assertEqual(options.language_level, 3)
|
| 238 |
+
|
| 239 |
+
def test_language_level_3str(self):
|
| 240 |
+
options, args = self.parse_args(['--3str'])
|
| 241 |
+
self.assertFalse(args)
|
| 242 |
+
self.assertTrue(self.are_default(options, ['language_level']))
|
| 243 |
+
self.assertEqual(options.language_level, '3str')
|
| 244 |
+
|
| 245 |
+
def test_annotate_short(self):
|
| 246 |
+
options, args = self.parse_args(['-a'])
|
| 247 |
+
self.assertFalse(args)
|
| 248 |
+
self.assertTrue(self.are_default(options, ['annotate']))
|
| 249 |
+
self.assertEqual(options.annotate, 'default')
|
| 250 |
+
|
| 251 |
+
def test_annotate_long(self):
|
| 252 |
+
options, args = self.parse_args(['--annotate'])
|
| 253 |
+
self.assertFalse(args)
|
| 254 |
+
self.assertTrue(self.are_default(options, ['annotate']))
|
| 255 |
+
self.assertEqual(options.annotate, 'default')
|
| 256 |
+
|
| 257 |
+
def test_annotate_fullc(self):
|
| 258 |
+
options, args = self.parse_args(['--annotate-fullc'])
|
| 259 |
+
self.assertFalse(args)
|
| 260 |
+
self.assertTrue(self.are_default(options, ['annotate']))
|
| 261 |
+
self.assertEqual(options.annotate, 'fullc')
|
| 262 |
+
|
| 263 |
+
def test_annotate_and_positional(self):
|
| 264 |
+
options, args = self.parse_args(['-a', 'foo.pyx'])
|
| 265 |
+
self.assertEqual(args, ['foo.pyx'])
|
| 266 |
+
self.assertTrue(self.are_default(options, ['annotate']))
|
| 267 |
+
self.assertEqual(options.annotate, 'default')
|
| 268 |
+
|
| 269 |
+
def test_annotate_and_optional(self):
|
| 270 |
+
options, args = self.parse_args(['-a', '--3str'])
|
| 271 |
+
self.assertFalse(args)
|
| 272 |
+
self.assertTrue(self.are_default(options, ['annotate', 'language_level']))
|
| 273 |
+
self.assertEqual(options.annotate, 'default')
|
| 274 |
+
self.assertEqual(options.language_level, '3str')
|
| 275 |
+
|
| 276 |
+
def test_exclude_short(self):
|
| 277 |
+
options, args = self.parse_args(['-x', '*.pyx'])
|
| 278 |
+
self.assertFalse(args)
|
| 279 |
+
self.assertTrue(self.are_default(options, ['excludes']))
|
| 280 |
+
self.assertTrue('*.pyx' in options.excludes)
|
| 281 |
+
|
| 282 |
+
def test_exclude_long(self):
|
| 283 |
+
options, args = self.parse_args(['--exclude', '*.pyx'])
|
| 284 |
+
self.assertFalse(args)
|
| 285 |
+
self.assertTrue(self.are_default(options, ['excludes']))
|
| 286 |
+
self.assertTrue('*.pyx' in options.excludes)
|
| 287 |
+
|
| 288 |
+
def test_exclude_multiple(self):
|
| 289 |
+
options, args = self.parse_args(['--exclude', '*.pyx', '--exclude', '*.py', ])
|
| 290 |
+
self.assertFalse(args)
|
| 291 |
+
self.assertTrue(self.are_default(options, ['excludes']))
|
| 292 |
+
self.assertEqual(options.excludes, ['*.pyx', '*.py'])
|
| 293 |
+
|
| 294 |
+
def test_build_short(self):
|
| 295 |
+
options, args = self.parse_args(['-b'])
|
| 296 |
+
self.assertFalse(args)
|
| 297 |
+
self.assertTrue(self.are_default(options, ['build']))
|
| 298 |
+
self.assertEqual(options.build, True)
|
| 299 |
+
|
| 300 |
+
def test_build_long(self):
|
| 301 |
+
options, args = self.parse_args(['--build'])
|
| 302 |
+
self.assertFalse(args)
|
| 303 |
+
self.assertTrue(self.are_default(options, ['build']))
|
| 304 |
+
self.assertEqual(options.build, True)
|
| 305 |
+
|
| 306 |
+
def test_inplace_short(self):
|
| 307 |
+
options, args = self.parse_args(['-i'])
|
| 308 |
+
self.assertFalse(args)
|
| 309 |
+
self.assertTrue(self.are_default(options, ['build_inplace']))
|
| 310 |
+
self.assertEqual(options.build_inplace, True)
|
| 311 |
+
|
| 312 |
+
def test_inplace_long(self):
|
| 313 |
+
options, args = self.parse_args(['--inplace'])
|
| 314 |
+
self.assertFalse(args)
|
| 315 |
+
self.assertTrue(self.are_default(options, ['build_inplace']))
|
| 316 |
+
self.assertEqual(options.build_inplace, True)
|
| 317 |
+
|
| 318 |
+
def test_parallel_short(self):
|
| 319 |
+
options, args = self.parse_args(['-j', '42'])
|
| 320 |
+
self.assertFalse(args)
|
| 321 |
+
self.assertTrue(self.are_default(options, ['parallel']))
|
| 322 |
+
self.assertEqual(options.parallel, 42)
|
| 323 |
+
|
| 324 |
+
def test_parallel_long(self):
|
| 325 |
+
options, args = self.parse_args(['--parallel', '42'])
|
| 326 |
+
self.assertFalse(args)
|
| 327 |
+
self.assertTrue(self.are_default(options, ['parallel']))
|
| 328 |
+
self.assertEqual(options.parallel, 42)
|
| 329 |
+
|
| 330 |
+
def test_force_short(self):
|
| 331 |
+
options, args = self.parse_args(['-f'])
|
| 332 |
+
self.assertFalse(args)
|
| 333 |
+
self.assertTrue(self.are_default(options, ['force']))
|
| 334 |
+
self.assertEqual(options.force, True)
|
| 335 |
+
|
| 336 |
+
def test_force_long(self):
|
| 337 |
+
options, args = self.parse_args(['--force'])
|
| 338 |
+
self.assertFalse(args)
|
| 339 |
+
self.assertTrue(self.are_default(options, ['force']))
|
| 340 |
+
self.assertEqual(options.force, True)
|
| 341 |
+
|
| 342 |
+
def test_quite_short(self):
|
| 343 |
+
options, args = self.parse_args(['-q'])
|
| 344 |
+
self.assertFalse(args)
|
| 345 |
+
self.assertTrue(self.are_default(options, ['quiet']))
|
| 346 |
+
self.assertEqual(options.quiet, True)
|
| 347 |
+
|
| 348 |
+
def test_quite_long(self):
|
| 349 |
+
options, args = self.parse_args(['--quiet'])
|
| 350 |
+
self.assertFalse(args)
|
| 351 |
+
self.assertTrue(self.are_default(options, ['quiet']))
|
| 352 |
+
self.assertEqual(options.quiet, True)
|
| 353 |
+
|
| 354 |
+
def test_lenient_long(self):
|
| 355 |
+
options, args = self.parse_args(['--lenient'])
|
| 356 |
+
self.assertTrue(self.are_default(options, ['lenient']))
|
| 357 |
+
self.assertFalse(args)
|
| 358 |
+
self.assertEqual(options.lenient, True)
|
| 359 |
+
|
| 360 |
+
def test_keep_going_short(self):
|
| 361 |
+
options, args = self.parse_args(['-k'])
|
| 362 |
+
self.assertFalse(args)
|
| 363 |
+
self.assertTrue(self.are_default(options, ['keep_going']))
|
| 364 |
+
self.assertEqual(options.keep_going, True)
|
| 365 |
+
|
| 366 |
+
def test_keep_going_long(self):
|
| 367 |
+
options, args = self.parse_args(['--keep-going'])
|
| 368 |
+
self.assertFalse(args)
|
| 369 |
+
self.assertTrue(self.are_default(options, ['keep_going']))
|
| 370 |
+
self.assertEqual(options.keep_going, True)
|
| 371 |
+
|
| 372 |
+
def test_no_docstrings_long(self):
|
| 373 |
+
options, args = self.parse_args(['--no-docstrings'])
|
| 374 |
+
self.assertFalse(args)
|
| 375 |
+
self.assertTrue(self.are_default(options, ['no_docstrings']))
|
| 376 |
+
self.assertEqual(options.no_docstrings, True)
|
| 377 |
+
|
| 378 |
+
def test_file_name(self):
|
| 379 |
+
options, args = self.parse_args(['file1.pyx', 'file2.pyx'])
|
| 380 |
+
self.assertEqual(len(args), 2)
|
| 381 |
+
self.assertEqual(args[0], 'file1.pyx')
|
| 382 |
+
self.assertEqual(args[1], 'file2.pyx')
|
| 383 |
+
self.assertTrue(self.are_default(options, []))
|
| 384 |
+
|
| 385 |
+
def test_option_first(self):
|
| 386 |
+
options, args = self.parse_args(['-i', 'file.pyx'])
|
| 387 |
+
self.assertEqual(args, ['file.pyx'])
|
| 388 |
+
self.assertEqual(options.build_inplace, True)
|
| 389 |
+
self.assertTrue(self.are_default(options, ['build_inplace']))
|
| 390 |
+
|
| 391 |
+
def test_file_inbetween(self):
|
| 392 |
+
options, args = self.parse_args(['-i', 'file.pyx', '-a'])
|
| 393 |
+
self.assertEqual(args, ['file.pyx'])
|
| 394 |
+
self.assertEqual(options.build_inplace, True)
|
| 395 |
+
self.assertEqual(options.annotate, 'default')
|
| 396 |
+
self.assertTrue(self.are_default(options, ['build_inplace', 'annotate']))
|
| 397 |
+
|
| 398 |
+
def test_option_trailing(self):
|
| 399 |
+
options, args = self.parse_args(['file.pyx', '-i'])
|
| 400 |
+
self.assertEqual(args, ['file.pyx'])
|
| 401 |
+
self.assertEqual(options.build_inplace, True)
|
| 402 |
+
self.assertTrue(self.are_default(options, ['build_inplace']))
|
| 403 |
+
|
| 404 |
+
def test_interspersed_positional(self):
|
| 405 |
+
options, sources = self.parse_args([
|
| 406 |
+
'file1.pyx', '-a',
|
| 407 |
+
'file2.pyx'
|
| 408 |
+
])
|
| 409 |
+
self.assertEqual(sources, ['file1.pyx', 'file2.pyx'])
|
| 410 |
+
self.assertEqual(options.annotate, 'default')
|
| 411 |
+
self.assertTrue(self.are_default(options, ['annotate']))
|
| 412 |
+
|
| 413 |
+
def test_interspersed_positional2(self):
|
| 414 |
+
options, sources = self.parse_args([
|
| 415 |
+
'file1.pyx', '-a',
|
| 416 |
+
'file2.pyx', '-a', 'file3.pyx'
|
| 417 |
+
])
|
| 418 |
+
self.assertEqual(sources, ['file1.pyx', 'file2.pyx', 'file3.pyx'])
|
| 419 |
+
self.assertEqual(options.annotate, 'default')
|
| 420 |
+
self.assertTrue(self.are_default(options, ['annotate']))
|
| 421 |
+
|
| 422 |
+
def test_interspersed_positional3(self):
|
| 423 |
+
options, sources = self.parse_args([
|
| 424 |
+
'-f', 'f1', 'f2', '-a',
|
| 425 |
+
'f3', 'f4', '-a', 'f5'
|
| 426 |
+
])
|
| 427 |
+
self.assertEqual(sources, ['f1', 'f2', 'f3', 'f4', 'f5'])
|
| 428 |
+
self.assertEqual(options.annotate, 'default')
|
| 429 |
+
self.assertEqual(options.force, True)
|
| 430 |
+
self.assertTrue(self.are_default(options, ['annotate', 'force']))
|
| 431 |
+
|
| 432 |
+
def test_wrong_option(self):
|
| 433 |
+
old_stderr = sys.stderr
|
| 434 |
+
stderr = sys.stderr = StringIO()
|
| 435 |
+
try:
|
| 436 |
+
self.assertRaises(SystemExit, self.parse_args,
|
| 437 |
+
['--unknown-option']
|
| 438 |
+
)
|
| 439 |
+
finally:
|
| 440 |
+
sys.stderr = old_stderr
|
| 441 |
+
self.assertTrue(stderr.getvalue())
|
| 442 |
+
|
| 443 |
+
|
| 444 |
+
class TestParseArgs(TestCase):
|
| 445 |
+
def setUp(self):
|
| 446 |
+
self._options_backup = backup_Options()
|
| 447 |
+
|
| 448 |
+
def tearDown(self):
|
| 449 |
+
restore_Options(self._options_backup)
|
| 450 |
+
|
| 451 |
+
def check_default_global_options(self, white_list=[]):
|
| 452 |
+
self.assertEqual(check_global_options(self._options_backup, white_list), "")
|
| 453 |
+
|
| 454 |
+
def test_build_set_for_inplace(self):
|
| 455 |
+
options, args = parse_args(['foo.pyx', '-i'])
|
| 456 |
+
self.assertEqual(options.build, True)
|
| 457 |
+
self.check_default_global_options()
|
| 458 |
+
|
| 459 |
+
def test_lenient(self):
|
| 460 |
+
options, sources = parse_args(['foo.pyx', '--lenient'])
|
| 461 |
+
self.assertEqual(sources, ['foo.pyx'])
|
| 462 |
+
self.assertEqual(Options.error_on_unknown_names, False)
|
| 463 |
+
self.assertEqual(Options.error_on_uninitialized, False)
|
| 464 |
+
self.check_default_global_options(['error_on_unknown_names', 'error_on_uninitialized'])
|
| 465 |
+
|
| 466 |
+
def test_annotate(self):
|
| 467 |
+
options, sources = parse_args(['foo.pyx', '--annotate'])
|
| 468 |
+
self.assertEqual(sources, ['foo.pyx'])
|
| 469 |
+
self.assertEqual(Options.annotate, 'default')
|
| 470 |
+
self.check_default_global_options(['annotate'])
|
| 471 |
+
|
| 472 |
+
def test_annotate_fullc(self):
|
| 473 |
+
options, sources = parse_args(['foo.pyx', '--annotate-fullc'])
|
| 474 |
+
self.assertEqual(sources, ['foo.pyx'])
|
| 475 |
+
self.assertEqual(Options.annotate, 'fullc')
|
| 476 |
+
self.check_default_global_options(['annotate'])
|
| 477 |
+
|
| 478 |
+
def test_no_docstrings(self):
|
| 479 |
+
options, sources = parse_args(['foo.pyx', '--no-docstrings'])
|
| 480 |
+
self.assertEqual(sources, ['foo.pyx'])
|
| 481 |
+
self.assertEqual(Options.docstrings, False)
|
| 482 |
+
self.check_default_global_options(['docstrings'])
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestDependencies.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import contextlib
|
| 2 |
+
import os.path
|
| 3 |
+
import sys
|
| 4 |
+
import tempfile
|
| 5 |
+
import unittest
|
| 6 |
+
from io import open
|
| 7 |
+
from os.path import join as pjoin
|
| 8 |
+
|
| 9 |
+
from ..Dependencies import extended_iglob
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@contextlib.contextmanager
|
| 13 |
+
def writable_file(dir_path, filename):
|
| 14 |
+
with open(pjoin(dir_path, filename), "w", encoding="utf8") as f:
|
| 15 |
+
yield f
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class TestGlobbing(unittest.TestCase):
|
| 19 |
+
@classmethod
|
| 20 |
+
def setUpClass(cls):
|
| 21 |
+
cls._orig_dir = os.getcwd()
|
| 22 |
+
if sys.version_info[0] < 3:
|
| 23 |
+
temp_path = cls._tmpdir = tempfile.mkdtemp()
|
| 24 |
+
else:
|
| 25 |
+
cls._tmpdir = tempfile.TemporaryDirectory()
|
| 26 |
+
temp_path = cls._tmpdir.name
|
| 27 |
+
os.chdir(temp_path)
|
| 28 |
+
|
| 29 |
+
for dir1 in "abcd":
|
| 30 |
+
for dir1x in [dir1, dir1 + 'x']:
|
| 31 |
+
for dir2 in "xyz":
|
| 32 |
+
dir_path = pjoin(dir1x, dir2)
|
| 33 |
+
os.makedirs(dir_path)
|
| 34 |
+
with writable_file(dir_path, "file2_pyx.pyx") as f:
|
| 35 |
+
f.write(u'""" PYX """')
|
| 36 |
+
with writable_file(dir_path, "file2_py.py") as f:
|
| 37 |
+
f.write(u'""" PY """')
|
| 38 |
+
|
| 39 |
+
with writable_file(dir1x, "file1_pyx.pyx") as f:
|
| 40 |
+
f.write(u'""" PYX """')
|
| 41 |
+
with writable_file(dir1x, "file1_py.py") as f:
|
| 42 |
+
f.write(u'""" PY """')
|
| 43 |
+
|
| 44 |
+
@classmethod
|
| 45 |
+
def tearDownClass(cls):
|
| 46 |
+
os.chdir(cls._orig_dir)
|
| 47 |
+
if sys.version_info[0] < 3:
|
| 48 |
+
import shutil
|
| 49 |
+
shutil.rmtree(cls._tmpdir)
|
| 50 |
+
else:
|
| 51 |
+
cls._tmpdir.cleanup()
|
| 52 |
+
|
| 53 |
+
def files_equal(self, pattern, expected_files):
|
| 54 |
+
expected_files = sorted(expected_files)
|
| 55 |
+
# It's the users's choice whether '/' will appear on Windows.
|
| 56 |
+
matched_files = sorted(path.replace('/', os.sep) for path in extended_iglob(pattern))
|
| 57 |
+
self.assertListEqual(matched_files, expected_files) # /
|
| 58 |
+
|
| 59 |
+
# Special case for Windows: also support '\' in patterns.
|
| 60 |
+
if os.sep == '\\' and '/' in pattern:
|
| 61 |
+
matched_files = sorted(extended_iglob(pattern.replace('/', '\\')))
|
| 62 |
+
self.assertListEqual(matched_files, expected_files) # \
|
| 63 |
+
|
| 64 |
+
def test_extended_iglob_simple(self):
|
| 65 |
+
ax_files = [pjoin("a", "x", "file2_pyx.pyx"), pjoin("a", "x", "file2_py.py")]
|
| 66 |
+
self.files_equal("a/x/*", ax_files)
|
| 67 |
+
self.files_equal("a/x/*.c12", [])
|
| 68 |
+
self.files_equal("a/x/*.{py,pyx,c12}", ax_files)
|
| 69 |
+
self.files_equal("a/x/*.{py,pyx}", ax_files)
|
| 70 |
+
self.files_equal("a/x/*.{pyx}", ax_files[:1])
|
| 71 |
+
self.files_equal("a/x/*.pyx", ax_files[:1])
|
| 72 |
+
self.files_equal("a/x/*.{py}", ax_files[1:])
|
| 73 |
+
self.files_equal("a/x/*.py", ax_files[1:])
|
| 74 |
+
|
| 75 |
+
def test_extended_iglob_simple_star(self):
|
| 76 |
+
for basedir in "ad":
|
| 77 |
+
files = [
|
| 78 |
+
pjoin(basedir, dirname, filename)
|
| 79 |
+
for dirname in "xyz"
|
| 80 |
+
for filename in ["file2_pyx.pyx", "file2_py.py"]
|
| 81 |
+
]
|
| 82 |
+
self.files_equal(basedir + "/*/*", files)
|
| 83 |
+
self.files_equal(basedir + "/*/*.c12", [])
|
| 84 |
+
self.files_equal(basedir + "/*/*.{py,pyx,c12}", files)
|
| 85 |
+
self.files_equal(basedir + "/*/*.{py,pyx}", files)
|
| 86 |
+
self.files_equal(basedir + "/*/*.{pyx}", files[::2])
|
| 87 |
+
self.files_equal(basedir + "/*/*.pyx", files[::2])
|
| 88 |
+
self.files_equal(basedir + "/*/*.{py}", files[1::2])
|
| 89 |
+
self.files_equal(basedir + "/*/*.py", files[1::2])
|
| 90 |
+
|
| 91 |
+
for subdir in "xy*":
|
| 92 |
+
files = [
|
| 93 |
+
pjoin(basedir, dirname, filename)
|
| 94 |
+
for dirname in "xyz"
|
| 95 |
+
if subdir in ('*', dirname)
|
| 96 |
+
for filename in ["file2_pyx.pyx", "file2_py.py"]
|
| 97 |
+
]
|
| 98 |
+
path = basedir + '/' + subdir + '/'
|
| 99 |
+
self.files_equal(path + "*", files)
|
| 100 |
+
self.files_equal(path + "*.{py,pyx}", files)
|
| 101 |
+
self.files_equal(path + "*.{pyx}", files[::2])
|
| 102 |
+
self.files_equal(path + "*.pyx", files[::2])
|
| 103 |
+
self.files_equal(path + "*.{py}", files[1::2])
|
| 104 |
+
self.files_equal(path + "*.py", files[1::2])
|
| 105 |
+
|
| 106 |
+
def test_extended_iglob_double_star(self):
|
| 107 |
+
basedirs = os.listdir(".")
|
| 108 |
+
files = [
|
| 109 |
+
pjoin(basedir, dirname, filename)
|
| 110 |
+
for basedir in basedirs
|
| 111 |
+
for dirname in "xyz"
|
| 112 |
+
for filename in ["file2_pyx.pyx", "file2_py.py"]
|
| 113 |
+
]
|
| 114 |
+
all_files = [
|
| 115 |
+
pjoin(basedir, filename)
|
| 116 |
+
for basedir in basedirs
|
| 117 |
+
for filename in ["file1_pyx.pyx", "file1_py.py"]
|
| 118 |
+
] + files
|
| 119 |
+
self.files_equal("*/*/*", files)
|
| 120 |
+
self.files_equal("*/*/**/*", files)
|
| 121 |
+
self.files_equal("*/**/*.*", all_files)
|
| 122 |
+
self.files_equal("**/*.*", all_files)
|
| 123 |
+
self.files_equal("*/**/*.c12", [])
|
| 124 |
+
self.files_equal("**/*.c12", [])
|
| 125 |
+
self.files_equal("*/*/*.{py,pyx,c12}", files)
|
| 126 |
+
self.files_equal("*/*/**/*.{py,pyx,c12}", files)
|
| 127 |
+
self.files_equal("*/**/*/*.{py,pyx,c12}", files)
|
| 128 |
+
self.files_equal("**/*/*/*.{py,pyx,c12}", files)
|
| 129 |
+
self.files_equal("**/*.{py,pyx,c12}", all_files)
|
| 130 |
+
self.files_equal("*/*/*.{py,pyx}", files)
|
| 131 |
+
self.files_equal("**/*/*/*.{py,pyx}", files)
|
| 132 |
+
self.files_equal("*/**/*/*.{py,pyx}", files)
|
| 133 |
+
self.files_equal("**/*.{py,pyx}", all_files)
|
| 134 |
+
self.files_equal("*/*/*.{pyx}", files[::2])
|
| 135 |
+
self.files_equal("**/*.{pyx}", all_files[::2])
|
| 136 |
+
self.files_equal("*/**/*/*.pyx", files[::2])
|
| 137 |
+
self.files_equal("*/*/*.pyx", files[::2])
|
| 138 |
+
self.files_equal("**/*.pyx", all_files[::2])
|
| 139 |
+
self.files_equal("*/*/*.{py}", files[1::2])
|
| 140 |
+
self.files_equal("**/*.{py}", all_files[1::2])
|
| 141 |
+
self.files_equal("*/*/*.py", files[1::2])
|
| 142 |
+
self.files_equal("**/*.py", all_files[1::2])
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestInline.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import unittest
|
| 4 |
+
from Cython.Shadow import inline
|
| 5 |
+
from Cython.Build.Inline import safe_type
|
| 6 |
+
from Cython.TestUtils import CythonTest
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
import numpy
|
| 10 |
+
has_numpy = True
|
| 11 |
+
except:
|
| 12 |
+
has_numpy = False
|
| 13 |
+
|
| 14 |
+
test_kwds = dict(force=True, quiet=True)
|
| 15 |
+
|
| 16 |
+
global_value = 100
|
| 17 |
+
|
| 18 |
+
class TestInline(CythonTest):
|
| 19 |
+
def setUp(self):
|
| 20 |
+
CythonTest.setUp(self)
|
| 21 |
+
self._call_kwds = dict(test_kwds)
|
| 22 |
+
if os.path.isdir('TEST_TMP'):
|
| 23 |
+
lib_dir = os.path.join('TEST_TMP','inline')
|
| 24 |
+
else:
|
| 25 |
+
lib_dir = tempfile.mkdtemp(prefix='cython_inline_')
|
| 26 |
+
self._call_kwds['lib_dir'] = lib_dir
|
| 27 |
+
|
| 28 |
+
def test_simple(self):
|
| 29 |
+
self.assertEqual(inline("return 1+2", **self._call_kwds), 3)
|
| 30 |
+
|
| 31 |
+
def test_types(self):
|
| 32 |
+
self.assertEqual(inline("""
|
| 33 |
+
cimport cython
|
| 34 |
+
return cython.typeof(a), cython.typeof(b)
|
| 35 |
+
""", a=1.0, b=[], **self._call_kwds), ('double', 'list object'))
|
| 36 |
+
|
| 37 |
+
def test_locals(self):
|
| 38 |
+
a = 1
|
| 39 |
+
b = 2
|
| 40 |
+
self.assertEqual(inline("return a+b", **self._call_kwds), 3)
|
| 41 |
+
|
| 42 |
+
def test_globals(self):
|
| 43 |
+
self.assertEqual(inline("return global_value + 1", **self._call_kwds), global_value + 1)
|
| 44 |
+
|
| 45 |
+
def test_no_return(self):
|
| 46 |
+
self.assertEqual(inline("""
|
| 47 |
+
a = 1
|
| 48 |
+
cdef double b = 2
|
| 49 |
+
cdef c = []
|
| 50 |
+
""", **self._call_kwds), dict(a=1, b=2.0, c=[]))
|
| 51 |
+
|
| 52 |
+
def test_def_node(self):
|
| 53 |
+
foo = inline("def foo(x): return x * x", **self._call_kwds)['foo']
|
| 54 |
+
self.assertEqual(foo(7), 49)
|
| 55 |
+
|
| 56 |
+
def test_class_ref(self):
|
| 57 |
+
class Type(object):
|
| 58 |
+
pass
|
| 59 |
+
tp = inline("Type")['Type']
|
| 60 |
+
self.assertEqual(tp, Type)
|
| 61 |
+
|
| 62 |
+
def test_pure(self):
|
| 63 |
+
import cython as cy
|
| 64 |
+
b = inline("""
|
| 65 |
+
b = cy.declare(float, a)
|
| 66 |
+
c = cy.declare(cy.pointer(cy.float), &b)
|
| 67 |
+
return b
|
| 68 |
+
""", a=3, **self._call_kwds)
|
| 69 |
+
self.assertEqual(type(b), float)
|
| 70 |
+
|
| 71 |
+
def test_compiler_directives(self):
|
| 72 |
+
self.assertEqual(
|
| 73 |
+
inline('return sum(x)',
|
| 74 |
+
x=[1, 2, 3],
|
| 75 |
+
cython_compiler_directives={'boundscheck': False}),
|
| 76 |
+
6
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
def test_lang_version(self):
|
| 80 |
+
# GH-3419. Caching for inline code didn't always respect compiler directives.
|
| 81 |
+
inline_divcode = "def f(int a, int b): return a/b"
|
| 82 |
+
self.assertEqual(
|
| 83 |
+
inline(inline_divcode, language_level=2)['f'](5,2),
|
| 84 |
+
2
|
| 85 |
+
)
|
| 86 |
+
self.assertEqual(
|
| 87 |
+
inline(inline_divcode, language_level=3)['f'](5,2),
|
| 88 |
+
2.5
|
| 89 |
+
)
|
| 90 |
+
self.assertEqual(
|
| 91 |
+
inline(inline_divcode, language_level=2)['f'](5,2),
|
| 92 |
+
2
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
def test_repeated_use(self):
|
| 96 |
+
inline_mulcode = "def f(int a, int b): return a * b"
|
| 97 |
+
self.assertEqual(inline(inline_mulcode)['f'](5, 2), 10)
|
| 98 |
+
self.assertEqual(inline(inline_mulcode)['f'](5, 3), 15)
|
| 99 |
+
self.assertEqual(inline(inline_mulcode)['f'](6, 2), 12)
|
| 100 |
+
self.assertEqual(inline(inline_mulcode)['f'](5, 2), 10)
|
| 101 |
+
|
| 102 |
+
f = inline(inline_mulcode)['f']
|
| 103 |
+
self.assertEqual(f(5, 2), 10)
|
| 104 |
+
self.assertEqual(f(5, 3), 15)
|
| 105 |
+
|
| 106 |
+
@unittest.skipIf(not has_numpy, "NumPy is not available")
|
| 107 |
+
def test_numpy(self):
|
| 108 |
+
import numpy
|
| 109 |
+
a = numpy.ndarray((10, 20))
|
| 110 |
+
a[0,0] = 10
|
| 111 |
+
self.assertEqual(safe_type(a), 'numpy.ndarray[numpy.float64_t, ndim=2]')
|
| 112 |
+
self.assertEqual(inline("return a[0,0]", a=a, **self._call_kwds), 10.0)
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestIpythonMagic.py
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
# tag: ipython
|
| 3 |
+
|
| 4 |
+
"""Tests for the Cython magics extension."""
|
| 5 |
+
|
| 6 |
+
from __future__ import absolute_import
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import io
|
| 10 |
+
import sys
|
| 11 |
+
from contextlib import contextmanager
|
| 12 |
+
from unittest import skipIf
|
| 13 |
+
|
| 14 |
+
from Cython.Build import IpythonMagic
|
| 15 |
+
from Cython.TestUtils import CythonTest
|
| 16 |
+
from Cython.Compiler.Annotate import AnnotationCCodeWriter
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
import IPython.testing.globalipapp
|
| 20 |
+
except ImportError:
|
| 21 |
+
# Disable tests and fake helpers for initialisation below.
|
| 22 |
+
def skip_if_not_installed(_):
|
| 23 |
+
return None
|
| 24 |
+
else:
|
| 25 |
+
def skip_if_not_installed(c):
|
| 26 |
+
return c
|
| 27 |
+
|
| 28 |
+
# not using IPython's decorators here because they depend on "nose"
|
| 29 |
+
skip_win32 = skipIf(sys.platform == 'win32', "Skip on Windows")
|
| 30 |
+
skip_py27 = skipIf(sys.version_info[:2] == (2,7), "Disabled in Py2.7")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# disable IPython history thread before it gets started to avoid having to clean it up
|
| 34 |
+
from IPython.core.history import HistoryManager
|
| 35 |
+
HistoryManager.enabled = False
|
| 36 |
+
except ImportError:
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@contextmanager
|
| 41 |
+
def capture_output():
|
| 42 |
+
backup = sys.stdout, sys.stderr
|
| 43 |
+
try:
|
| 44 |
+
replacement = [
|
| 45 |
+
io.TextIOWrapper(io.BytesIO(), encoding=sys.stdout.encoding),
|
| 46 |
+
io.TextIOWrapper(io.BytesIO(), encoding=sys.stderr.encoding),
|
| 47 |
+
]
|
| 48 |
+
sys.stdout, sys.stderr = replacement
|
| 49 |
+
output = []
|
| 50 |
+
yield output
|
| 51 |
+
finally:
|
| 52 |
+
sys.stdout, sys.stderr = backup
|
| 53 |
+
for wrapper in replacement:
|
| 54 |
+
wrapper.seek(0) # rewind
|
| 55 |
+
output.append(wrapper.read())
|
| 56 |
+
wrapper.close()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
code = u"""\
|
| 60 |
+
def f(x):
|
| 61 |
+
return 2*x
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
cython3_code = u"""\
|
| 65 |
+
def f(int x):
|
| 66 |
+
return 2 / x
|
| 67 |
+
|
| 68 |
+
def call(x):
|
| 69 |
+
return f(*(x,))
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
pgo_cython3_code = cython3_code + u"""\
|
| 73 |
+
def main():
|
| 74 |
+
for _ in range(100): call(5)
|
| 75 |
+
main()
|
| 76 |
+
"""
|
| 77 |
+
|
| 78 |
+
compile_error_code = u'''\
|
| 79 |
+
cdef extern from *:
|
| 80 |
+
"""
|
| 81 |
+
xxx a=1;
|
| 82 |
+
"""
|
| 83 |
+
int a;
|
| 84 |
+
def doit():
|
| 85 |
+
return a
|
| 86 |
+
'''
|
| 87 |
+
|
| 88 |
+
compile_warning_code = u'''\
|
| 89 |
+
cdef extern from *:
|
| 90 |
+
"""
|
| 91 |
+
#pragma message ( "CWarning" )
|
| 92 |
+
int a = 42;
|
| 93 |
+
"""
|
| 94 |
+
int a;
|
| 95 |
+
def doit():
|
| 96 |
+
return a
|
| 97 |
+
'''
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
@skip_if_not_installed
|
| 101 |
+
class TestIPythonMagic(CythonTest):
|
| 102 |
+
|
| 103 |
+
@classmethod
|
| 104 |
+
def setUpClass(cls):
|
| 105 |
+
CythonTest.setUpClass()
|
| 106 |
+
cls._ip = IPython.testing.globalipapp.get_ipython()
|
| 107 |
+
|
| 108 |
+
def setUp(self):
|
| 109 |
+
CythonTest.setUp(self)
|
| 110 |
+
self._ip.extension_manager.load_extension('cython')
|
| 111 |
+
|
| 112 |
+
def test_cython_inline(self):
|
| 113 |
+
ip = self._ip
|
| 114 |
+
ip.ex('a=10; b=20')
|
| 115 |
+
result = ip.run_cell_magic('cython_inline', '', 'return a+b')
|
| 116 |
+
self.assertEqual(result, 30)
|
| 117 |
+
|
| 118 |
+
@skip_win32
|
| 119 |
+
def test_cython_pyximport(self):
|
| 120 |
+
ip = self._ip
|
| 121 |
+
module_name = '_test_cython_pyximport'
|
| 122 |
+
ip.run_cell_magic('cython_pyximport', module_name, code)
|
| 123 |
+
ip.ex('g = f(10)')
|
| 124 |
+
self.assertEqual(ip.user_ns['g'], 20.0)
|
| 125 |
+
ip.run_cell_magic('cython_pyximport', module_name, code)
|
| 126 |
+
ip.ex('h = f(-10)')
|
| 127 |
+
self.assertEqual(ip.user_ns['h'], -20.0)
|
| 128 |
+
try:
|
| 129 |
+
os.remove(module_name + '.pyx')
|
| 130 |
+
except OSError:
|
| 131 |
+
pass
|
| 132 |
+
|
| 133 |
+
def test_cython(self):
|
| 134 |
+
ip = self._ip
|
| 135 |
+
ip.run_cell_magic('cython', '', code)
|
| 136 |
+
ip.ex('g = f(10)')
|
| 137 |
+
self.assertEqual(ip.user_ns['g'], 20.0)
|
| 138 |
+
|
| 139 |
+
def test_cython_name(self):
|
| 140 |
+
# The Cython module named 'mymodule' defines the function f.
|
| 141 |
+
ip = self._ip
|
| 142 |
+
ip.run_cell_magic('cython', '--name=mymodule', code)
|
| 143 |
+
# This module can now be imported in the interactive namespace.
|
| 144 |
+
ip.ex('import mymodule; g = mymodule.f(10)')
|
| 145 |
+
self.assertEqual(ip.user_ns['g'], 20.0)
|
| 146 |
+
|
| 147 |
+
def test_cython_language_level(self):
|
| 148 |
+
# The Cython cell defines the functions f() and call().
|
| 149 |
+
ip = self._ip
|
| 150 |
+
ip.run_cell_magic('cython', '', cython3_code)
|
| 151 |
+
ip.ex('g = f(10); h = call(10)')
|
| 152 |
+
if sys.version_info[0] < 3:
|
| 153 |
+
self.assertEqual(ip.user_ns['g'], 2 // 10)
|
| 154 |
+
self.assertEqual(ip.user_ns['h'], 2 // 10)
|
| 155 |
+
else:
|
| 156 |
+
self.assertEqual(ip.user_ns['g'], 2.0 / 10.0)
|
| 157 |
+
self.assertEqual(ip.user_ns['h'], 2.0 / 10.0)
|
| 158 |
+
|
| 159 |
+
def test_cython3(self):
|
| 160 |
+
# The Cython cell defines the functions f() and call().
|
| 161 |
+
ip = self._ip
|
| 162 |
+
ip.run_cell_magic('cython', '-3', cython3_code)
|
| 163 |
+
ip.ex('g = f(10); h = call(10)')
|
| 164 |
+
self.assertEqual(ip.user_ns['g'], 2.0 / 10.0)
|
| 165 |
+
self.assertEqual(ip.user_ns['h'], 2.0 / 10.0)
|
| 166 |
+
|
| 167 |
+
def test_cython2(self):
|
| 168 |
+
# The Cython cell defines the functions f() and call().
|
| 169 |
+
ip = self._ip
|
| 170 |
+
ip.run_cell_magic('cython', '-2', cython3_code)
|
| 171 |
+
ip.ex('g = f(10); h = call(10)')
|
| 172 |
+
self.assertEqual(ip.user_ns['g'], 2 // 10)
|
| 173 |
+
self.assertEqual(ip.user_ns['h'], 2 // 10)
|
| 174 |
+
|
| 175 |
+
def test_cython_compile_error_shown(self):
|
| 176 |
+
ip = self._ip
|
| 177 |
+
with capture_output() as out:
|
| 178 |
+
ip.run_cell_magic('cython', '-3', compile_error_code)
|
| 179 |
+
captured_out, captured_err = out
|
| 180 |
+
|
| 181 |
+
# it could be that c-level output is captured by distutil-extension
|
| 182 |
+
# (and not by us) and is printed to stdout:
|
| 183 |
+
captured_all = captured_out + "\n" + captured_err
|
| 184 |
+
self.assertTrue("error" in captured_all, msg="error in " + captured_all)
|
| 185 |
+
|
| 186 |
+
def test_cython_link_error_shown(self):
|
| 187 |
+
ip = self._ip
|
| 188 |
+
with capture_output() as out:
|
| 189 |
+
ip.run_cell_magic('cython', '-3 -l=xxxxxxxx', code)
|
| 190 |
+
captured_out, captured_err = out
|
| 191 |
+
|
| 192 |
+
# it could be that c-level output is captured by distutil-extension
|
| 193 |
+
# (and not by us) and is printed to stdout:
|
| 194 |
+
captured_all = captured_out + "\n!" + captured_err
|
| 195 |
+
self.assertTrue("error" in captured_all, msg="error in " + captured_all)
|
| 196 |
+
|
| 197 |
+
def test_cython_warning_shown(self):
|
| 198 |
+
ip = self._ip
|
| 199 |
+
with capture_output() as out:
|
| 200 |
+
# force rebuild, otherwise no warning as after the first success
|
| 201 |
+
# no build step is performed
|
| 202 |
+
ip.run_cell_magic('cython', '-3 -f', compile_warning_code)
|
| 203 |
+
captured_out, captured_err = out
|
| 204 |
+
|
| 205 |
+
# check that warning was printed to stdout even if build hasn't failed
|
| 206 |
+
self.assertTrue("CWarning" in captured_out)
|
| 207 |
+
|
| 208 |
+
@skip_py27 # Not strictly broken in Py2.7 but currently fails in CI due to C compiler issues.
|
| 209 |
+
@skip_win32
|
| 210 |
+
def test_cython3_pgo(self):
|
| 211 |
+
# The Cython cell defines the functions f() and call().
|
| 212 |
+
ip = self._ip
|
| 213 |
+
ip.run_cell_magic('cython', '-3 --pgo', pgo_cython3_code)
|
| 214 |
+
ip.ex('g = f(10); h = call(10); main()')
|
| 215 |
+
self.assertEqual(ip.user_ns['g'], 2.0 / 10.0)
|
| 216 |
+
self.assertEqual(ip.user_ns['h'], 2.0 / 10.0)
|
| 217 |
+
|
| 218 |
+
@skip_win32
|
| 219 |
+
def test_extlibs(self):
|
| 220 |
+
ip = self._ip
|
| 221 |
+
code = u"""
|
| 222 |
+
from libc.math cimport sin
|
| 223 |
+
x = sin(0.0)
|
| 224 |
+
"""
|
| 225 |
+
ip.user_ns['x'] = 1
|
| 226 |
+
ip.run_cell_magic('cython', '-l m', code)
|
| 227 |
+
self.assertEqual(ip.user_ns['x'], 0)
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def test_cython_verbose(self):
|
| 231 |
+
ip = self._ip
|
| 232 |
+
ip.run_cell_magic('cython', '--verbose', code)
|
| 233 |
+
ip.ex('g = f(10)')
|
| 234 |
+
self.assertEqual(ip.user_ns['g'], 20.0)
|
| 235 |
+
|
| 236 |
+
def test_cython_verbose_thresholds(self):
|
| 237 |
+
@contextmanager
|
| 238 |
+
def mock_distutils():
|
| 239 |
+
class MockLog:
|
| 240 |
+
DEBUG = 1
|
| 241 |
+
INFO = 2
|
| 242 |
+
thresholds = [INFO]
|
| 243 |
+
|
| 244 |
+
def set_threshold(self, val):
|
| 245 |
+
self.thresholds.append(val)
|
| 246 |
+
return self.thresholds[-2]
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
new_log = MockLog()
|
| 250 |
+
old_log = IpythonMagic.distutils.log
|
| 251 |
+
try:
|
| 252 |
+
IpythonMagic.distutils.log = new_log
|
| 253 |
+
yield new_log
|
| 254 |
+
finally:
|
| 255 |
+
IpythonMagic.distutils.log = old_log
|
| 256 |
+
|
| 257 |
+
ip = self._ip
|
| 258 |
+
with mock_distutils() as verbose_log:
|
| 259 |
+
ip.run_cell_magic('cython', '--verbose', code)
|
| 260 |
+
ip.ex('g = f(10)')
|
| 261 |
+
self.assertEqual(ip.user_ns['g'], 20.0)
|
| 262 |
+
self.assertEqual([verbose_log.INFO, verbose_log.DEBUG, verbose_log.INFO],
|
| 263 |
+
verbose_log.thresholds)
|
| 264 |
+
|
| 265 |
+
with mock_distutils() as normal_log:
|
| 266 |
+
ip.run_cell_magic('cython', '', code)
|
| 267 |
+
ip.ex('g = f(10)')
|
| 268 |
+
self.assertEqual(ip.user_ns['g'], 20.0)
|
| 269 |
+
self.assertEqual([normal_log.INFO], normal_log.thresholds)
|
| 270 |
+
|
| 271 |
+
def test_cython_no_annotate(self):
|
| 272 |
+
ip = self._ip
|
| 273 |
+
html = ip.run_cell_magic('cython', '', code)
|
| 274 |
+
self.assertTrue(html is None)
|
| 275 |
+
|
| 276 |
+
def test_cython_annotate(self):
|
| 277 |
+
ip = self._ip
|
| 278 |
+
html = ip.run_cell_magic('cython', '--annotate', code)
|
| 279 |
+
# somewhat brittle way to differentiate between annotated htmls
|
| 280 |
+
# with/without complete source code:
|
| 281 |
+
self.assertTrue(AnnotationCCodeWriter.COMPLETE_CODE_TITLE not in html.data)
|
| 282 |
+
|
| 283 |
+
def test_cython_annotate_default(self):
|
| 284 |
+
ip = self._ip
|
| 285 |
+
html = ip.run_cell_magic('cython', '-a', code)
|
| 286 |
+
# somewhat brittle way to differentiate between annotated htmls
|
| 287 |
+
# with/without complete source code:
|
| 288 |
+
self.assertTrue(AnnotationCCodeWriter.COMPLETE_CODE_TITLE not in html.data)
|
| 289 |
+
|
| 290 |
+
def test_cython_annotate_complete_c_code(self):
|
| 291 |
+
ip = self._ip
|
| 292 |
+
html = ip.run_cell_magic('cython', '--annotate-fullc', code)
|
| 293 |
+
# somewhat brittle way to differentiate between annotated htmls
|
| 294 |
+
# with/without complete source code:
|
| 295 |
+
self.assertTrue(AnnotationCCodeWriter.COMPLETE_CODE_TITLE in html.data)
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestRecythonize.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import shutil
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
import Cython.Build.Dependencies
|
| 7 |
+
import Cython.Utils
|
| 8 |
+
from Cython.TestUtils import CythonTest
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def fresh_cythonize(*args, **kwargs):
|
| 12 |
+
Cython.Utils.clear_function_caches()
|
| 13 |
+
Cython.Build.Dependencies._dep_tree = None # discard method caches
|
| 14 |
+
Cython.Build.Dependencies.cythonize(*args, **kwargs)
|
| 15 |
+
|
| 16 |
+
class TestRecythonize(CythonTest):
|
| 17 |
+
|
| 18 |
+
def setUp(self):
|
| 19 |
+
CythonTest.setUp(self)
|
| 20 |
+
self.temp_dir = (
|
| 21 |
+
tempfile.mkdtemp(
|
| 22 |
+
prefix='recythonize-test',
|
| 23 |
+
dir='TEST_TMP' if os.path.isdir('TEST_TMP') else None
|
| 24 |
+
)
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def tearDown(self):
|
| 28 |
+
CythonTest.tearDown(self)
|
| 29 |
+
shutil.rmtree(self.temp_dir)
|
| 30 |
+
|
| 31 |
+
def test_recythonize_pyx_on_pxd_change(self):
|
| 32 |
+
|
| 33 |
+
src_dir = tempfile.mkdtemp(prefix='src', dir=self.temp_dir)
|
| 34 |
+
|
| 35 |
+
a_pxd = os.path.join(src_dir, 'a.pxd')
|
| 36 |
+
a_pyx = os.path.join(src_dir, 'a.pyx')
|
| 37 |
+
a_c = os.path.join(src_dir, 'a.c')
|
| 38 |
+
dep_tree = Cython.Build.Dependencies.create_dependency_tree()
|
| 39 |
+
|
| 40 |
+
with open(a_pxd, 'w') as f:
|
| 41 |
+
f.write('cdef int value\n')
|
| 42 |
+
|
| 43 |
+
with open(a_pyx, 'w') as f:
|
| 44 |
+
f.write('value = 1\n')
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# The dependencies for "a.pyx" are "a.pxd" and "a.pyx".
|
| 48 |
+
self.assertEqual({a_pxd, a_pyx}, dep_tree.all_dependencies(a_pyx))
|
| 49 |
+
|
| 50 |
+
# Cythonize to create a.c
|
| 51 |
+
fresh_cythonize(a_pyx)
|
| 52 |
+
|
| 53 |
+
# Sleep to address coarse time-stamp precision.
|
| 54 |
+
time.sleep(1)
|
| 55 |
+
|
| 56 |
+
with open(a_c) as f:
|
| 57 |
+
a_c_contents1 = f.read()
|
| 58 |
+
|
| 59 |
+
with open(a_pxd, 'w') as f:
|
| 60 |
+
f.write('cdef double value\n')
|
| 61 |
+
|
| 62 |
+
fresh_cythonize(a_pyx)
|
| 63 |
+
|
| 64 |
+
with open(a_c) as f:
|
| 65 |
+
a_c_contents2 = f.read()
|
| 66 |
+
|
| 67 |
+
self.assertTrue("__pyx_v_1a_value = 1;" in a_c_contents1)
|
| 68 |
+
self.assertFalse("__pyx_v_1a_value = 1;" in a_c_contents2)
|
| 69 |
+
self.assertTrue("__pyx_v_1a_value = 1.0;" in a_c_contents2)
|
| 70 |
+
self.assertFalse("__pyx_v_1a_value = 1.0;" in a_c_contents1)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_recythonize_py_on_pxd_change(self):
|
| 74 |
+
|
| 75 |
+
src_dir = tempfile.mkdtemp(prefix='src', dir=self.temp_dir)
|
| 76 |
+
|
| 77 |
+
a_pxd = os.path.join(src_dir, 'a.pxd')
|
| 78 |
+
a_py = os.path.join(src_dir, 'a.py')
|
| 79 |
+
a_c = os.path.join(src_dir, 'a.c')
|
| 80 |
+
dep_tree = Cython.Build.Dependencies.create_dependency_tree()
|
| 81 |
+
|
| 82 |
+
with open(a_pxd, 'w') as f:
|
| 83 |
+
f.write('cdef int value\n')
|
| 84 |
+
|
| 85 |
+
with open(a_py, 'w') as f:
|
| 86 |
+
f.write('value = 1\n')
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# The dependencies for "a.py" are "a.pxd" and "a.py".
|
| 90 |
+
self.assertEqual({a_pxd, a_py}, dep_tree.all_dependencies(a_py))
|
| 91 |
+
|
| 92 |
+
# Cythonize to create a.c
|
| 93 |
+
fresh_cythonize(a_py)
|
| 94 |
+
|
| 95 |
+
# Sleep to address coarse time-stamp precision.
|
| 96 |
+
time.sleep(1)
|
| 97 |
+
|
| 98 |
+
with open(a_c) as f:
|
| 99 |
+
a_c_contents1 = f.read()
|
| 100 |
+
|
| 101 |
+
with open(a_pxd, 'w') as f:
|
| 102 |
+
f.write('cdef double value\n')
|
| 103 |
+
|
| 104 |
+
fresh_cythonize(a_py)
|
| 105 |
+
|
| 106 |
+
with open(a_c) as f:
|
| 107 |
+
a_c_contents2 = f.read()
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
self.assertTrue("__pyx_v_1a_value = 1;" in a_c_contents1)
|
| 111 |
+
self.assertFalse("__pyx_v_1a_value = 1;" in a_c_contents2)
|
| 112 |
+
self.assertTrue("__pyx_v_1a_value = 1.0;" in a_c_contents2)
|
| 113 |
+
self.assertFalse("__pyx_v_1a_value = 1.0;" in a_c_contents1)
|
| 114 |
+
|
| 115 |
+
def test_recythonize_pyx_on_dep_pxd_change(self):
|
| 116 |
+
src_dir = tempfile.mkdtemp(prefix='src', dir=self.temp_dir)
|
| 117 |
+
|
| 118 |
+
a_pxd = os.path.join(src_dir, 'a.pxd')
|
| 119 |
+
a_pyx = os.path.join(src_dir, 'a.pyx')
|
| 120 |
+
b_pyx = os.path.join(src_dir, 'b.pyx')
|
| 121 |
+
b_c = os.path.join(src_dir, 'b.c')
|
| 122 |
+
dep_tree = Cython.Build.Dependencies.create_dependency_tree()
|
| 123 |
+
|
| 124 |
+
with open(a_pxd, 'w') as f:
|
| 125 |
+
f.write('cdef int value\n')
|
| 126 |
+
|
| 127 |
+
with open(a_pyx, 'w') as f:
|
| 128 |
+
f.write('value = 1\n')
|
| 129 |
+
|
| 130 |
+
with open(b_pyx, 'w') as f:
|
| 131 |
+
f.write('cimport a\n' + 'a.value = 2\n')
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# The dependencies for "b.pyx" are "a.pxd" and "b.pyx".
|
| 135 |
+
self.assertEqual({a_pxd, b_pyx}, dep_tree.all_dependencies(b_pyx))
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
# Cythonize to create b.c
|
| 139 |
+
fresh_cythonize([a_pyx, b_pyx])
|
| 140 |
+
|
| 141 |
+
# Sleep to address coarse time-stamp precision.
|
| 142 |
+
time.sleep(1)
|
| 143 |
+
|
| 144 |
+
with open(b_c) as f:
|
| 145 |
+
b_c_contents1 = f.read()
|
| 146 |
+
|
| 147 |
+
with open(a_pxd, 'w') as f:
|
| 148 |
+
f.write('cdef double value\n')
|
| 149 |
+
|
| 150 |
+
fresh_cythonize([a_pyx, b_pyx])
|
| 151 |
+
|
| 152 |
+
with open(b_c) as f:
|
| 153 |
+
b_c_contents2 = f.read()
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
self.assertTrue("__pyx_v_1a_value = 2;" in b_c_contents1)
|
| 158 |
+
self.assertFalse("__pyx_v_1a_value = 2;" in b_c_contents2)
|
| 159 |
+
self.assertTrue("__pyx_v_1a_value = 2.0;" in b_c_contents2)
|
| 160 |
+
self.assertFalse("__pyx_v_1a_value = 2.0;" in b_c_contents1)
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
def test_recythonize_py_on_dep_pxd_change(self):
|
| 165 |
+
|
| 166 |
+
src_dir = tempfile.mkdtemp(prefix='src', dir=self.temp_dir)
|
| 167 |
+
|
| 168 |
+
a_pxd = os.path.join(src_dir, 'a.pxd')
|
| 169 |
+
a_pyx = os.path.join(src_dir, 'a.pyx')
|
| 170 |
+
b_pxd = os.path.join(src_dir, 'b.pxd')
|
| 171 |
+
b_py = os.path.join(src_dir, 'b.py')
|
| 172 |
+
b_c = os.path.join(src_dir, 'b.c')
|
| 173 |
+
dep_tree = Cython.Build.Dependencies.create_dependency_tree()
|
| 174 |
+
|
| 175 |
+
with open(a_pxd, 'w') as f:
|
| 176 |
+
f.write('cdef int value\n')
|
| 177 |
+
|
| 178 |
+
with open(a_pyx, 'w') as f:
|
| 179 |
+
f.write('value = 1\n')
|
| 180 |
+
|
| 181 |
+
with open(b_pxd, 'w') as f:
|
| 182 |
+
f.write('cimport a\n')
|
| 183 |
+
|
| 184 |
+
with open(b_py, 'w') as f:
|
| 185 |
+
f.write('a.value = 2\n')
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
# The dependencies for b.py are "a.pxd", "b.pxd" and "b.py".
|
| 189 |
+
self.assertEqual({a_pxd, b_pxd, b_py}, dep_tree.all_dependencies(b_py))
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
# Cythonize to create b.c
|
| 193 |
+
fresh_cythonize([a_pyx, b_py])
|
| 194 |
+
|
| 195 |
+
# Sleep to address coarse time-stamp precision.
|
| 196 |
+
time.sleep(1)
|
| 197 |
+
|
| 198 |
+
with open(b_c) as f:
|
| 199 |
+
b_c_contents1 = f.read()
|
| 200 |
+
|
| 201 |
+
with open(a_pxd, 'w') as f:
|
| 202 |
+
f.write('cdef double value\n')
|
| 203 |
+
|
| 204 |
+
fresh_cythonize([a_pyx, b_py])
|
| 205 |
+
|
| 206 |
+
with open(b_c) as f:
|
| 207 |
+
b_c_contents2 = f.read()
|
| 208 |
+
|
| 209 |
+
self.assertTrue("__pyx_v_1a_value = 2;" in b_c_contents1)
|
| 210 |
+
self.assertFalse("__pyx_v_1a_value = 2;" in b_c_contents2)
|
| 211 |
+
self.assertTrue("__pyx_v_1a_value = 2.0;" in b_c_contents2)
|
| 212 |
+
self.assertFalse("__pyx_v_1a_value = 2.0;" in b_c_contents1)
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/TestStripLiterals.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from Cython.Build.Dependencies import strip_string_literals
|
| 2 |
+
|
| 3 |
+
from Cython.TestUtils import CythonTest
|
| 4 |
+
|
| 5 |
+
class TestStripLiterals(CythonTest):
|
| 6 |
+
|
| 7 |
+
def t(self, before, expected):
|
| 8 |
+
actual, literals = strip_string_literals(before, prefix="_L")
|
| 9 |
+
self.assertEqual(expected, actual)
|
| 10 |
+
for key, value in literals.items():
|
| 11 |
+
actual = actual.replace(key, value)
|
| 12 |
+
self.assertEqual(before, actual)
|
| 13 |
+
|
| 14 |
+
def test_empty(self):
|
| 15 |
+
self.t("", "")
|
| 16 |
+
|
| 17 |
+
def test_single_quote(self):
|
| 18 |
+
self.t("'x'", "'_L1_'")
|
| 19 |
+
|
| 20 |
+
def test_double_quote(self):
|
| 21 |
+
self.t('"x"', '"_L1_"')
|
| 22 |
+
|
| 23 |
+
def test_nested_quotes(self):
|
| 24 |
+
self.t(""" '"' "'" """, """ '_L1_' "_L2_" """)
|
| 25 |
+
|
| 26 |
+
def test_triple_quote(self):
|
| 27 |
+
self.t(" '''a\n''' ", " '''_L1_''' ")
|
| 28 |
+
|
| 29 |
+
def test_backslash(self):
|
| 30 |
+
self.t(r"'a\'b'", "'_L1_'")
|
| 31 |
+
self.t(r"'a\\'", "'_L1_'")
|
| 32 |
+
self.t(r"'a\\\'b'", "'_L1_'")
|
| 33 |
+
|
| 34 |
+
def test_unicode(self):
|
| 35 |
+
self.t("u'abc'", "u'_L1_'")
|
| 36 |
+
|
| 37 |
+
def test_raw(self):
|
| 38 |
+
self.t(r"r'abc\\'", "r'_L1_'")
|
| 39 |
+
|
| 40 |
+
def test_raw_unicode(self):
|
| 41 |
+
self.t(r"ru'abc\\'", "ru'_L1_'")
|
| 42 |
+
|
| 43 |
+
def test_comment(self):
|
| 44 |
+
self.t("abc # foo", "abc #_L1_")
|
| 45 |
+
|
| 46 |
+
def test_comment_and_quote(self):
|
| 47 |
+
self.t("abc # 'x'", "abc #_L1_")
|
| 48 |
+
self.t("'abc#'", "'_L1_'")
|
| 49 |
+
|
| 50 |
+
def test_include(self):
|
| 51 |
+
self.t("include 'a.pxi' # something here",
|
| 52 |
+
"include '_L1_' #_L2_")
|
| 53 |
+
|
| 54 |
+
def test_extern(self):
|
| 55 |
+
self.t("cdef extern from 'a.h': # comment",
|
| 56 |
+
"cdef extern from '_L1_': #_L2_")
|
Restormer/.eggs/Cython-3.0.12-py3.7-linux-x86_64.egg/Cython/Build/Tests/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# empty file
|