Cython 3.3.0a1__cp315-cp315-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- Cython/Build/BuildExecutable.py +156 -0
- Cython/Build/Cache.py +199 -0
- Cython/Build/Cythonize.py +349 -0
- Cython/Build/Dependencies.py +1276 -0
- Cython/Build/Distutils.py +1 -0
- Cython/Build/Inline.py +467 -0
- Cython/Build/IpythonMagic.py +559 -0
- Cython/Build/SharedModule.py +84 -0
- Cython/Build/Tests/TestCyCache.py +195 -0
- Cython/Build/Tests/TestCythonizeArgsParser.py +480 -0
- Cython/Build/Tests/TestDependencies.py +133 -0
- Cython/Build/Tests/TestInline.py +177 -0
- Cython/Build/Tests/TestIpythonMagic.py +303 -0
- Cython/Build/Tests/TestRecythonize.py +212 -0
- Cython/Build/Tests/TestStripLiterals.py +155 -0
- Cython/Build/Tests/__init__.py +1 -0
- Cython/Build/__init__.py +11 -0
- Cython/CodeWriter.py +815 -0
- Cython/Compiler/AnalysedTreeTransforms.py +97 -0
- Cython/Compiler/Annotate.py +328 -0
- Cython/Compiler/AutoDocTransforms.py +320 -0
- Cython/Compiler/Buffer.py +680 -0
- Cython/Compiler/Builtin.py +997 -0
- Cython/Compiler/CmdLine.py +263 -0
- Cython/Compiler/Code.cp315-win_amd64.pyd +0 -0
- Cython/Compiler/Code.pxd +152 -0
- Cython/Compiler/Code.py +3907 -0
- Cython/Compiler/CodeGeneration.py +33 -0
- Cython/Compiler/CythonScope.py +194 -0
- Cython/Compiler/Dataclass.py +890 -0
- Cython/Compiler/DebugFlags.py +24 -0
- Cython/Compiler/Errors.py +310 -0
- Cython/Compiler/ExprNodes.py +15983 -0
- Cython/Compiler/FlowControl.cp315-win_amd64.pyd +0 -0
- Cython/Compiler/FlowControl.pxd +99 -0
- Cython/Compiler/FlowControl.py +1571 -0
- Cython/Compiler/FusedNode.cp315-win_amd64.pyd +0 -0
- Cython/Compiler/FusedNode.py +976 -0
- Cython/Compiler/Future.py +16 -0
- Cython/Compiler/Interpreter.py +57 -0
- Cython/Compiler/Lexicon.py +422 -0
- Cython/Compiler/LineTable.cp315-win_amd64.pyd +0 -0
- Cython/Compiler/LineTable.py +114 -0
- Cython/Compiler/Main.py +856 -0
- Cython/Compiler/MatchCaseNodes.py +2197 -0
- Cython/Compiler/MemoryView.py +930 -0
- Cython/Compiler/ModuleNode.py +4517 -0
- Cython/Compiler/Naming.py +367 -0
- Cython/Compiler/Nodes.py +10941 -0
- Cython/Compiler/Optimize.py +5455 -0
- Cython/Compiler/Options.py +838 -0
- Cython/Compiler/ParseTreeTransforms.pxd +79 -0
- Cython/Compiler/ParseTreeTransforms.py +4744 -0
- Cython/Compiler/Parsing.cp315-win_amd64.pyd +0 -0
- Cython/Compiler/Parsing.pxd +9 -0
- Cython/Compiler/Parsing.py +4792 -0
- Cython/Compiler/Pipeline.py +439 -0
- Cython/Compiler/PyrexTypes.py +6111 -0
- Cython/Compiler/Pythran.py +232 -0
- Cython/Compiler/Scanning.cp315-win_amd64.pyd +0 -0
- Cython/Compiler/Scanning.pxd +70 -0
- Cython/Compiler/Scanning.py +720 -0
- Cython/Compiler/StringEncoding.py +297 -0
- Cython/Compiler/Symtab.py +3092 -0
- Cython/Compiler/Tests/TestBuffer.py +105 -0
- Cython/Compiler/Tests/TestBuiltin.py +117 -0
- Cython/Compiler/Tests/TestCmdLine.py +587 -0
- Cython/Compiler/Tests/TestCode.py +145 -0
- Cython/Compiler/Tests/TestFlowControl.py +65 -0
- Cython/Compiler/Tests/TestGrammar.py +202 -0
- Cython/Compiler/Tests/TestMemView.py +71 -0
- Cython/Compiler/Tests/TestParseTreeTransforms.py +285 -0
- Cython/Compiler/Tests/TestScanning.py +132 -0
- Cython/Compiler/Tests/TestSignatureMatching.py +73 -0
- Cython/Compiler/Tests/TestStringEncoding.py +20 -0
- Cython/Compiler/Tests/TestTreeFragment.py +63 -0
- Cython/Compiler/Tests/TestTreePath.py +103 -0
- Cython/Compiler/Tests/TestTypes.py +118 -0
- Cython/Compiler/Tests/TestUtilityLoad.py +112 -0
- Cython/Compiler/Tests/TestVisitor.py +61 -0
- Cython/Compiler/Tests/Utils.py +36 -0
- Cython/Compiler/Tests/__init__.py +1 -0
- Cython/Compiler/TreeFragment.py +278 -0
- Cython/Compiler/TreePath.py +303 -0
- Cython/Compiler/TypeInference.py +611 -0
- Cython/Compiler/TypeSlots.py +1329 -0
- Cython/Compiler/UFuncs.py +311 -0
- Cython/Compiler/UtilNodes.py +413 -0
- Cython/Compiler/UtilityCode.py +348 -0
- Cython/Compiler/Version.py +8 -0
- Cython/Compiler/Visitor.cp315-win_amd64.pyd +0 -0
- Cython/Compiler/Visitor.pxd +53 -0
- Cython/Compiler/Visitor.py +864 -0
- Cython/Compiler/__init__.py +1 -0
- Cython/Coverage.py +448 -0
- Cython/Debugger/Cygdb.py +177 -0
- Cython/Debugger/DebugWriter.py +82 -0
- Cython/Debugger/Tests/TestLibCython.py +280 -0
- Cython/Debugger/Tests/__init__.py +1 -0
- Cython/Debugger/Tests/cfuncs.c +8 -0
- Cython/Debugger/Tests/codefile +49 -0
- Cython/Debugger/Tests/test_libcython_in_gdb.py +580 -0
- Cython/Debugger/Tests/test_libpython_in_gdb.py +90 -0
- Cython/Debugger/__init__.py +1 -0
- Cython/Debugger/libcython.py +1548 -0
- Cython/Debugger/libpython.py +2821 -0
- Cython/Debugging.py +20 -0
- Cython/Distutils/__init__.py +2 -0
- Cython/Distutils/build_ext.py +139 -0
- Cython/Distutils/extension.py +96 -0
- Cython/Distutils/old_build_ext.py +351 -0
- Cython/Includes/cpython/__init__.pxd +173 -0
- Cython/Includes/cpython/array.pxd +152 -0
- Cython/Includes/cpython/bool.pxd +37 -0
- Cython/Includes/cpython/buffer.pxd +112 -0
- Cython/Includes/cpython/bytearray.pxd +33 -0
- Cython/Includes/cpython/bytes.pxd +200 -0
- Cython/Includes/cpython/cellobject.pxd +35 -0
- Cython/Includes/cpython/ceval.pxd +8 -0
- Cython/Includes/cpython/codecs.pxd +121 -0
- Cython/Includes/cpython/complex.pxd +60 -0
- Cython/Includes/cpython/contextvars.pxd +145 -0
- Cython/Includes/cpython/conversion.pxd +36 -0
- Cython/Includes/cpython/datetime.pxd +395 -0
- Cython/Includes/cpython/descr.pxd +26 -0
- Cython/Includes/cpython/dict.pxd +268 -0
- Cython/Includes/cpython/exc.pxd +263 -0
- Cython/Includes/cpython/fileobject.pxd +57 -0
- Cython/Includes/cpython/float.pxd +56 -0
- Cython/Includes/cpython/frozendict.pxd +37 -0
- Cython/Includes/cpython/function.pxd +65 -0
- Cython/Includes/cpython/genobject.pxd +25 -0
- Cython/Includes/cpython/getargs.pxd +12 -0
- Cython/Includes/cpython/instance.pxd +25 -0
- Cython/Includes/cpython/iterator.pxd +36 -0
- Cython/Includes/cpython/iterobject.pxd +24 -0
- Cython/Includes/cpython/list.pxd +144 -0
- Cython/Includes/cpython/long.pxd +180 -0
- Cython/Includes/cpython/longintrepr.pxd +14 -0
- Cython/Includes/cpython/mapping.pxd +63 -0
- Cython/Includes/cpython/marshal.pxd +66 -0
- Cython/Includes/cpython/mem.pxd +120 -0
- Cython/Includes/cpython/memoryview.pxd +50 -0
- Cython/Includes/cpython/method.pxd +49 -0
- Cython/Includes/cpython/module.pxd +208 -0
- Cython/Includes/cpython/number.pxd +258 -0
- Cython/Includes/cpython/object.pxd +430 -0
- Cython/Includes/cpython/pycapsule.pxd +143 -0
- Cython/Includes/cpython/pylifecycle.pxd +68 -0
- Cython/Includes/cpython/pyport.pxd +8 -0
- Cython/Includes/cpython/pystate.pxd +95 -0
- Cython/Includes/cpython/pythread.pxd +53 -0
- Cython/Includes/cpython/ref.pxd +141 -0
- Cython/Includes/cpython/sentinel.pxd +17 -0
- Cython/Includes/cpython/sequence.pxd +134 -0
- Cython/Includes/cpython/set.pxd +119 -0
- Cython/Includes/cpython/slice.pxd +70 -0
- Cython/Includes/cpython/time.pxd +129 -0
- Cython/Includes/cpython/tuple.pxd +72 -0
- Cython/Includes/cpython/type.pxd +146 -0
- Cython/Includes/cpython/unicode.pxd +639 -0
- Cython/Includes/cpython/version.pxd +32 -0
- Cython/Includes/cpython/weakref.pxd +78 -0
- Cython/Includes/libc/__init__.pxd +1 -0
- Cython/Includes/libc/complex.pxd +35 -0
- Cython/Includes/libc/errno.pxd +127 -0
- Cython/Includes/libc/float.pxd +43 -0
- Cython/Includes/libc/limits.pxd +28 -0
- Cython/Includes/libc/locale.pxd +46 -0
- Cython/Includes/libc/math.pxd +209 -0
- Cython/Includes/libc/setjmp.pxd +10 -0
- Cython/Includes/libc/signal.pxd +64 -0
- Cython/Includes/libc/stddef.pxd +9 -0
- Cython/Includes/libc/stdint.pxd +105 -0
- Cython/Includes/libc/stdio.pxd +80 -0
- Cython/Includes/libc/stdlib.pxd +72 -0
- Cython/Includes/libc/string.pxd +50 -0
- Cython/Includes/libc/threads.pxd +234 -0
- Cython/Includes/libc/time.pxd +52 -0
- Cython/Includes/libcpp/__init__.pxd +4 -0
- Cython/Includes/libcpp/algorithm.pxd +320 -0
- Cython/Includes/libcpp/any.pxd +16 -0
- Cython/Includes/libcpp/atomic.pxd +59 -0
- Cython/Includes/libcpp/barrier.pxd +22 -0
- Cython/Includes/libcpp/bit.pxd +29 -0
- Cython/Includes/libcpp/cast.pxd +12 -0
- Cython/Includes/libcpp/cmath.pxd +518 -0
- Cython/Includes/libcpp/complex.pxd +106 -0
- Cython/Includes/libcpp/condition_variable.pxd +322 -0
- Cython/Includes/libcpp/deque.pxd +165 -0
- Cython/Includes/libcpp/exception.pxd +216 -0
- Cython/Includes/libcpp/execution.pxd +15 -0
- Cython/Includes/libcpp/forward_list.pxd +63 -0
- Cython/Includes/libcpp/functional.pxd +26 -0
- Cython/Includes/libcpp/future.pxd +103 -0
- Cython/Includes/libcpp/iterator.pxd +34 -0
- Cython/Includes/libcpp/latch.pxd +17 -0
- Cython/Includes/libcpp/limits.pxd +61 -0
- Cython/Includes/libcpp/list.pxd +117 -0
- Cython/Includes/libcpp/map.pxd +252 -0
- Cython/Includes/libcpp/memory.pxd +115 -0
- Cython/Includes/libcpp/mutex.pxd +387 -0
- Cython/Includes/libcpp/numbers.pxd +15 -0
- Cython/Includes/libcpp/numeric.pxd +131 -0
- Cython/Includes/libcpp/optional.pxd +34 -0
- Cython/Includes/libcpp/pair.pxd +1 -0
- Cython/Includes/libcpp/queue.pxd +25 -0
- Cython/Includes/libcpp/random.pxd +166 -0
- Cython/Includes/libcpp/semaphore.pxd +43 -0
- Cython/Includes/libcpp/set.pxd +228 -0
- Cython/Includes/libcpp/shared_mutex.pxd +96 -0
- Cython/Includes/libcpp/span.pxd +87 -0
- Cython/Includes/libcpp/stack.pxd +11 -0
- Cython/Includes/libcpp/stop_token.pxd +117 -0
- Cython/Includes/libcpp/string.pxd +355 -0
- Cython/Includes/libcpp/string_view.pxd +183 -0
- Cython/Includes/libcpp/typeindex.pxd +15 -0
- Cython/Includes/libcpp/typeinfo.pxd +10 -0
- Cython/Includes/libcpp/unordered_map.pxd +193 -0
- Cython/Includes/libcpp/unordered_set.pxd +152 -0
- Cython/Includes/libcpp/utility.pxd +30 -0
- Cython/Includes/libcpp/vector.pxd +186 -0
- Cython/Includes/numpy/math.pxd +150 -0
- Cython/Includes/openmp.pxd +50 -0
- Cython/Includes/posix/__init__.pxd +1 -0
- Cython/Includes/posix/dlfcn.pxd +14 -0
- Cython/Includes/posix/fcntl.pxd +86 -0
- Cython/Includes/posix/ioctl.pxd +4 -0
- Cython/Includes/posix/mman.pxd +101 -0
- Cython/Includes/posix/resource.pxd +57 -0
- Cython/Includes/posix/select.pxd +21 -0
- Cython/Includes/posix/signal.pxd +73 -0
- Cython/Includes/posix/stat.pxd +98 -0
- Cython/Includes/posix/stdio.pxd +37 -0
- Cython/Includes/posix/stdlib.pxd +29 -0
- Cython/Includes/posix/strings.pxd +9 -0
- Cython/Includes/posix/time.pxd +71 -0
- Cython/Includes/posix/types.pxd +30 -0
- Cython/Includes/posix/uio.pxd +26 -0
- Cython/Includes/posix/unistd.pxd +271 -0
- Cython/Includes/posix/wait.pxd +38 -0
- Cython/LZSS.py +170 -0
- Cython/Plex/Actions.cp315-win_amd64.pyd +0 -0
- Cython/Plex/Actions.pxd +24 -0
- Cython/Plex/Actions.py +119 -0
- Cython/Plex/DFA.cp315-win_amd64.pyd +0 -0
- Cython/Plex/DFA.pxd +14 -0
- Cython/Plex/DFA.py +164 -0
- Cython/Plex/Errors.py +48 -0
- Cython/Plex/Lexicons.py +178 -0
- Cython/Plex/Machines.cp315-win_amd64.pyd +0 -0
- Cython/Plex/Machines.pxd +36 -0
- Cython/Plex/Machines.py +238 -0
- Cython/Plex/Regexps.py +535 -0
- Cython/Plex/Scanners.cp315-win_amd64.pyd +0 -0
- Cython/Plex/Scanners.pxd +45 -0
- Cython/Plex/Scanners.py +328 -0
- Cython/Plex/Transitions.cp315-win_amd64.pyd +0 -0
- Cython/Plex/Transitions.pxd +14 -0
- Cython/Plex/Transitions.py +239 -0
- Cython/Plex/__init__.py +34 -0
- Cython/Runtime/__init__.py +1 -0
- Cython/Runtime/refnanny.cp315-win_amd64.pyd +0 -0
- Cython/Runtime/refnanny.pyx +237 -0
- Cython/Shadow.py +1167 -0
- Cython/StringIOTree.cp315-win_amd64.pyd +0 -0
- Cython/StringIOTree.py +169 -0
- Cython/Tempita/__init__.py +4 -0
- Cython/Tempita/_looper.py +154 -0
- Cython/Tempita/_tempita.cp315-win_amd64.pyd +0 -0
- Cython/Tempita/_tempita.py +1087 -0
- Cython/TestUtils.py +464 -0
- Cython/Tests/TestCodeWriter.py +128 -0
- Cython/Tests/TestCythonUtils.py +202 -0
- Cython/Tests/TestJediTyper.py +223 -0
- Cython/Tests/TestShadow.py +110 -0
- Cython/Tests/TestStringIOTree.py +68 -0
- Cython/Tests/TestTestUtils.py +89 -0
- Cython/Tests/__init__.py +1 -0
- Cython/Tests/xmlrunner.py +390 -0
- Cython/Utility/AsyncGen.c +1073 -0
- Cython/Utility/Buffer.c +866 -0
- Cython/Utility/BufferFormatFromTypeInfo.pxd +2 -0
- Cython/Utility/Builtins.c +933 -0
- Cython/Utility/CConvert.pyx +149 -0
- Cython/Utility/CMath.c +104 -0
- Cython/Utility/CommonStructures.c +244 -0
- Cython/Utility/Complex.c +378 -0
- Cython/Utility/Coroutine.c +2337 -0
- Cython/Utility/CpdefEnums.pyx +107 -0
- Cython/Utility/CppConvert.pyx +282 -0
- Cython/Utility/CppSupport.cpp +151 -0
- Cython/Utility/CythonFunction.c +2072 -0
- Cython/Utility/Dataclasses.c +101 -0
- Cython/Utility/Embed.c +129 -0
- Cython/Utility/Exceptions.c +1038 -0
- Cython/Utility/ExtensionTypes.c +1158 -0
- Cython/Utility/FunctionArguments.c +1045 -0
- Cython/Utility/FusedFunction.pyx +44 -0
- Cython/Utility/ImportExport.c +930 -0
- Cython/Utility/MatchCase.c +979 -0
- Cython/Utility/MatchCase_Cy.pyx +12 -0
- Cython/Utility/MemoryView.pxd +108 -0
- Cython/Utility/MemoryView.pyx +1499 -0
- Cython/Utility/MemoryView_C.c +1056 -0
- Cython/Utility/ModuleSetupCode.c +3352 -0
- Cython/Utility/NumpyImportArray.c +46 -0
- Cython/Utility/ObjectHandling.c +3372 -0
- Cython/Utility/Optimize.c +2563 -0
- Cython/Utility/Overflow.c +378 -0
- Cython/Utility/Profile.c +736 -0
- Cython/Utility/StringTools.c +1414 -0
- Cython/Utility/Synchronization.c +438 -0
- Cython/Utility/TString.c +369 -0
- Cython/Utility/TestCyUtilityLoader.pyx +8 -0
- Cython/Utility/TestCythonScope.pyx +75 -0
- Cython/Utility/TestUtilityLoader.c +12 -0
- Cython/Utility/TypeConversion.c +1556 -0
- Cython/Utility/UFuncs.pyx +50 -0
- Cython/Utility/UFuncs_C.c +89 -0
- Cython/Utility/__init__.py +28 -0
- Cython/Utility/arrayarray.h +172 -0
- Cython/Utils.cp315-win_amd64.pyd +0 -0
- Cython/Utils.py +677 -0
- Cython/__init__.py +12 -0
- Cython/py.typed +0 -0
- cython-3.3.0a1.dist-info/METADATA +394 -0
- cython-3.3.0a1.dist-info/RECORD +335 -0
- cython-3.3.0a1.dist-info/WHEEL +5 -0
- cython-3.3.0a1.dist-info/entry_points.txt +4 -0
- cython-3.3.0a1.dist-info/top_level.txt +3 -0
- cython.py +29 -0
- pyximport/__init__.py +4 -0
- pyximport/pyxbuild.py +160 -0
- pyximport/pyximport.py +482 -0
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
"""
|
|
2
|
+
=====================
|
|
3
|
+
Cython related magics
|
|
4
|
+
=====================
|
|
5
|
+
|
|
6
|
+
Magic command interface for interactive work with Cython
|
|
7
|
+
|
|
8
|
+
.. note::
|
|
9
|
+
|
|
10
|
+
The ``Cython`` package needs to be installed separately. It
|
|
11
|
+
can be obtained using ``easy_install`` or ``pip``.
|
|
12
|
+
|
|
13
|
+
Usage
|
|
14
|
+
=====
|
|
15
|
+
|
|
16
|
+
To enable the magics below, execute ``%load_ext cython``.
|
|
17
|
+
|
|
18
|
+
``%%cython``
|
|
19
|
+
|
|
20
|
+
{CYTHON_DOC}
|
|
21
|
+
|
|
22
|
+
``%%cython_inline``
|
|
23
|
+
|
|
24
|
+
{CYTHON_INLINE_DOC}
|
|
25
|
+
|
|
26
|
+
``%%cython_pyximport``
|
|
27
|
+
|
|
28
|
+
{CYTHON_PYXIMPORT_DOC}
|
|
29
|
+
|
|
30
|
+
Author:
|
|
31
|
+
* Brian Granger
|
|
32
|
+
|
|
33
|
+
Code moved from IPython and adapted by:
|
|
34
|
+
* Martín Gaitán
|
|
35
|
+
|
|
36
|
+
Parts of this code were taken from Cython.inline.
|
|
37
|
+
"""
|
|
38
|
+
#-----------------------------------------------------------------------------
|
|
39
|
+
# Copyright (C) 2010-2011, IPython Development Team.
|
|
40
|
+
#
|
|
41
|
+
# Distributed under the terms of the Modified BSD License.
|
|
42
|
+
#
|
|
43
|
+
# The full license is in the file ipython-COPYING.rst, distributed with this software.
|
|
44
|
+
#-----------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
import os
|
|
48
|
+
import re
|
|
49
|
+
import sys
|
|
50
|
+
import time
|
|
51
|
+
import copy
|
|
52
|
+
import distutils.log
|
|
53
|
+
import textwrap
|
|
54
|
+
|
|
55
|
+
IO_ENCODING = sys.getfilesystemencoding()
|
|
56
|
+
|
|
57
|
+
import hashlib
|
|
58
|
+
from distutils.core import Distribution, Extension
|
|
59
|
+
from distutils.command.build_ext import build_ext
|
|
60
|
+
|
|
61
|
+
from IPython.core import display
|
|
62
|
+
from IPython.core import magic_arguments
|
|
63
|
+
from IPython.core.magic import Magics, magics_class, cell_magic
|
|
64
|
+
try:
|
|
65
|
+
from IPython.paths import get_ipython_cache_dir
|
|
66
|
+
except ImportError:
|
|
67
|
+
# older IPython version
|
|
68
|
+
from IPython.utils.path import get_ipython_cache_dir
|
|
69
|
+
from IPython.utils.text import dedent
|
|
70
|
+
|
|
71
|
+
from ..Shadow import __version__ as cython_version
|
|
72
|
+
from ..Compiler.Errors import CompileError
|
|
73
|
+
from .Inline import cython_inline, load_dynamic
|
|
74
|
+
from .Dependencies import cythonize
|
|
75
|
+
from ..Utils import captured_fd, print_captured
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
PGO_CONFIG = {
|
|
79
|
+
'gcc': {
|
|
80
|
+
'gen': ['-fprofile-generate', '-fprofile-dir={TEMPDIR}'],
|
|
81
|
+
'use': ['-fprofile-use', '-fprofile-correction', '-fprofile-dir={TEMPDIR}'],
|
|
82
|
+
},
|
|
83
|
+
# blind copy from 'configure' script in CPython 3.7
|
|
84
|
+
'icc': {
|
|
85
|
+
'gen': ['-prof-gen'],
|
|
86
|
+
'use': ['-prof-use'],
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
PGO_CONFIG['mingw32'] = PGO_CONFIG['gcc']
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@magics_class
|
|
93
|
+
class CythonMagics(Magics):
|
|
94
|
+
|
|
95
|
+
def __init__(self, shell):
|
|
96
|
+
super().__init__(shell)
|
|
97
|
+
self._reloads = {}
|
|
98
|
+
self._code_cache = {}
|
|
99
|
+
self._pyximport_installed = False
|
|
100
|
+
|
|
101
|
+
def _import_all(self, module):
|
|
102
|
+
mdict = module.__dict__
|
|
103
|
+
if '__all__' in mdict:
|
|
104
|
+
keys = mdict['__all__']
|
|
105
|
+
else:
|
|
106
|
+
keys = [k for k in mdict if not k.startswith('_')]
|
|
107
|
+
|
|
108
|
+
for k in keys:
|
|
109
|
+
try:
|
|
110
|
+
self.shell.push({k: mdict[k]})
|
|
111
|
+
except KeyError:
|
|
112
|
+
msg = "'module' object has no attribute '%s'" % k
|
|
113
|
+
raise AttributeError(msg)
|
|
114
|
+
|
|
115
|
+
@cell_magic
|
|
116
|
+
def cython_inline(self, line, cell):
|
|
117
|
+
"""Compile and run a Cython code cell using Cython.inline.
|
|
118
|
+
|
|
119
|
+
This magic simply passes the body of the cell to Cython.inline
|
|
120
|
+
and returns the result. If the variables `a` and `b` are defined
|
|
121
|
+
in the user's namespace, here is a simple example that returns
|
|
122
|
+
their sum::
|
|
123
|
+
|
|
124
|
+
%%cython_inline
|
|
125
|
+
return a+b
|
|
126
|
+
|
|
127
|
+
For most purposes, we recommend the usage of the `%%cython` magic.
|
|
128
|
+
"""
|
|
129
|
+
locs = self.shell.user_global_ns
|
|
130
|
+
globs = self.shell.user_ns
|
|
131
|
+
return cython_inline(cell, locals=locs, globals=globs)
|
|
132
|
+
|
|
133
|
+
@cell_magic
|
|
134
|
+
def cython_pyximport(self, line, cell):
|
|
135
|
+
"""Compile and import a Cython code cell using pyximport.
|
|
136
|
+
|
|
137
|
+
The contents of the cell are written to a `.pyx` file in the current
|
|
138
|
+
working directory, which is then imported using `pyximport`. This
|
|
139
|
+
magic requires a module name to be passed::
|
|
140
|
+
|
|
141
|
+
%%cython_pyximport modulename
|
|
142
|
+
def f(x):
|
|
143
|
+
return 2.0*x
|
|
144
|
+
|
|
145
|
+
The compiled module is then imported and all of its symbols are
|
|
146
|
+
injected into the user's namespace. For most purposes, we recommend
|
|
147
|
+
the usage of the `%%cython` magic.
|
|
148
|
+
"""
|
|
149
|
+
module_name = line.strip()
|
|
150
|
+
if not module_name:
|
|
151
|
+
raise ValueError('module name must be given')
|
|
152
|
+
fname = module_name + '.pyx'
|
|
153
|
+
with open(fname, 'w', encoding='utf-8') as f:
|
|
154
|
+
f.write(cell)
|
|
155
|
+
if 'pyximport' not in sys.modules or not self._pyximport_installed:
|
|
156
|
+
import pyximport
|
|
157
|
+
pyximport.install()
|
|
158
|
+
self._pyximport_installed = True
|
|
159
|
+
if module_name in self._reloads:
|
|
160
|
+
module = self._reloads[module_name]
|
|
161
|
+
# Note: reloading extension modules is not actually supported
|
|
162
|
+
# (requires PEP-489 reinitialisation support).
|
|
163
|
+
# Don't know why this should ever have worked as it reads here.
|
|
164
|
+
# All we really need to do is to update the globals below.
|
|
165
|
+
#reload(module)
|
|
166
|
+
else:
|
|
167
|
+
__import__(module_name)
|
|
168
|
+
module = sys.modules[module_name]
|
|
169
|
+
self._reloads[module_name] = module
|
|
170
|
+
self._import_all(module)
|
|
171
|
+
|
|
172
|
+
@magic_arguments.magic_arguments()
|
|
173
|
+
@magic_arguments.argument(
|
|
174
|
+
'-a', '--annotate', action='store_const', const='default', dest='annotate',
|
|
175
|
+
help="Produce a colorized HTML version of the source."
|
|
176
|
+
)
|
|
177
|
+
@magic_arguments.argument(
|
|
178
|
+
'--annotate-fullc', action='store_const', const='fullc', dest='annotate',
|
|
179
|
+
help="Produce a colorized HTML version of the source "
|
|
180
|
+
"which includes entire generated C/C++-code."
|
|
181
|
+
)
|
|
182
|
+
@magic_arguments.argument(
|
|
183
|
+
'-+', '--cplus', action='store_true', default=False,
|
|
184
|
+
help="Output a C++ rather than C file."
|
|
185
|
+
)
|
|
186
|
+
@magic_arguments.argument(
|
|
187
|
+
'-3', dest='language_level', action='store_const', const=3, default=None,
|
|
188
|
+
help="Select Python 3 syntax."
|
|
189
|
+
)
|
|
190
|
+
@magic_arguments.argument(
|
|
191
|
+
'-2', dest='language_level', action='store_const', const=2, default=None,
|
|
192
|
+
help="Select Python 2 syntax."
|
|
193
|
+
)
|
|
194
|
+
@magic_arguments.argument(
|
|
195
|
+
'-f', '--force', action='store_true', default=False,
|
|
196
|
+
help="Force the compilation of a new module, even if the source has been "
|
|
197
|
+
"previously compiled."
|
|
198
|
+
)
|
|
199
|
+
@magic_arguments.argument(
|
|
200
|
+
'-c', '--compile-args', action='append', default=[],
|
|
201
|
+
help="Extra flags to pass to compiler via the `extra_compile_args` "
|
|
202
|
+
"Extension flag (can be specified multiple times)."
|
|
203
|
+
)
|
|
204
|
+
@magic_arguments.argument(
|
|
205
|
+
'--link-args', action='append', default=[],
|
|
206
|
+
help="Extra flags to pass to linker via the `extra_link_args` "
|
|
207
|
+
"Extension flag (can be specified multiple times)."
|
|
208
|
+
)
|
|
209
|
+
@magic_arguments.argument(
|
|
210
|
+
'-l', '--lib', action='append', default=[],
|
|
211
|
+
help="Add a library to link the extension against (can be specified "
|
|
212
|
+
"multiple times)."
|
|
213
|
+
)
|
|
214
|
+
@magic_arguments.argument(
|
|
215
|
+
'-n', '--name',
|
|
216
|
+
help="Specify a name for the Cython module."
|
|
217
|
+
)
|
|
218
|
+
@magic_arguments.argument(
|
|
219
|
+
'-L', dest='library_dirs', metavar='dir', action='append', default=[],
|
|
220
|
+
help="Add a path to the list of library directories (can be specified "
|
|
221
|
+
"multiple times)."
|
|
222
|
+
)
|
|
223
|
+
@magic_arguments.argument(
|
|
224
|
+
'-I', '--include', action='append', default=[],
|
|
225
|
+
help="Add a path to the list of include directories (can be specified "
|
|
226
|
+
"multiple times)."
|
|
227
|
+
)
|
|
228
|
+
@magic_arguments.argument(
|
|
229
|
+
'-S', '--src', action='append', default=[],
|
|
230
|
+
help="Add a path to the list of src files (can be specified "
|
|
231
|
+
"multiple times)."
|
|
232
|
+
)
|
|
233
|
+
@magic_arguments.argument(
|
|
234
|
+
'--pgo', dest='pgo', action='store_true', default=False,
|
|
235
|
+
help=("Enable profile guided optimisation in the C compiler. "
|
|
236
|
+
"Compiles the cell twice and executes it in between to generate a runtime profile.")
|
|
237
|
+
)
|
|
238
|
+
@magic_arguments.argument(
|
|
239
|
+
'--verbose', dest='quiet', action='store_false', default=True,
|
|
240
|
+
help=("Print debug information like generated .c/.cpp file location "
|
|
241
|
+
"and exact gcc/g++ command invoked.")
|
|
242
|
+
)
|
|
243
|
+
@cell_magic
|
|
244
|
+
def cython(self, line, cell):
|
|
245
|
+
"""Compile and import everything from a Cython code cell.
|
|
246
|
+
|
|
247
|
+
The contents of the cell are written to a `.pyx` file in the
|
|
248
|
+
directory returned by `get_ipython_cache_dir()/cython` using a filename
|
|
249
|
+
with the hash of the code. This file is then cythonized and compiled.
|
|
250
|
+
The resulting module is imported and all of its symbols are injected
|
|
251
|
+
into the user's namespace. The usage is similar to that of
|
|
252
|
+
`%%cython_pyximport` but you don't have to pass a module name::
|
|
253
|
+
|
|
254
|
+
%%cython
|
|
255
|
+
def f(x):
|
|
256
|
+
return 2.0*x
|
|
257
|
+
|
|
258
|
+
To compile OpenMP codes, pass the required `--compile-args`
|
|
259
|
+
and `--link-args`. For example with gcc::
|
|
260
|
+
|
|
261
|
+
%%cython --compile-args=-fopenmp --link-args=-fopenmp
|
|
262
|
+
...
|
|
263
|
+
|
|
264
|
+
To enable profile guided optimisation, pass the ``--pgo`` option.
|
|
265
|
+
Note that the cell itself needs to take care of establishing a suitable
|
|
266
|
+
profile when executed. This can be done by implementing the functions to
|
|
267
|
+
optimise, and then calling them directly in the same cell on some realistic
|
|
268
|
+
training data like this::
|
|
269
|
+
|
|
270
|
+
%%cython --pgo
|
|
271
|
+
def critical_function(data):
|
|
272
|
+
for item in data:
|
|
273
|
+
...
|
|
274
|
+
|
|
275
|
+
# execute function several times to build profile
|
|
276
|
+
from somewhere import some_typical_data
|
|
277
|
+
for _ in range(100):
|
|
278
|
+
critical_function(some_typical_data)
|
|
279
|
+
|
|
280
|
+
In Python 3.5 and later, you can distinguish between the profile and
|
|
281
|
+
non-profile runs as follows::
|
|
282
|
+
|
|
283
|
+
if "_pgo_" in __name__:
|
|
284
|
+
... # execute critical code here
|
|
285
|
+
"""
|
|
286
|
+
args = magic_arguments.parse_argstring(self.cython, line)
|
|
287
|
+
code = cell if cell.endswith('\n') else cell + '\n'
|
|
288
|
+
lib_dir = os.path.join(get_ipython_cache_dir(), 'cython')
|
|
289
|
+
key = (code, line, sys.version_info, sys.executable, cython_version)
|
|
290
|
+
|
|
291
|
+
if not os.path.exists(lib_dir):
|
|
292
|
+
os.makedirs(lib_dir)
|
|
293
|
+
|
|
294
|
+
if args.pgo:
|
|
295
|
+
key += ('pgo',)
|
|
296
|
+
if args.force:
|
|
297
|
+
# Force a new module name by adding the current time to the
|
|
298
|
+
# key which is hashed to determine the module name.
|
|
299
|
+
key += (time.time(),)
|
|
300
|
+
|
|
301
|
+
if args.name:
|
|
302
|
+
module_name = str(args.name) # no-op in Py3
|
|
303
|
+
else:
|
|
304
|
+
module_name = "_cython_magic_" + hashlib.sha256(str(key).encode('utf-8')).hexdigest()
|
|
305
|
+
html_file = os.path.join(lib_dir, module_name + '.html')
|
|
306
|
+
module_path = os.path.join(lib_dir, module_name + self.so_ext)
|
|
307
|
+
|
|
308
|
+
have_module = os.path.isfile(module_path)
|
|
309
|
+
need_cythonize = args.pgo or not have_module
|
|
310
|
+
|
|
311
|
+
if args.annotate:
|
|
312
|
+
if not os.path.isfile(html_file):
|
|
313
|
+
need_cythonize = True
|
|
314
|
+
|
|
315
|
+
extension = None
|
|
316
|
+
if need_cythonize:
|
|
317
|
+
extensions = self._cythonize(module_name, code, lib_dir, args, quiet=args.quiet)
|
|
318
|
+
if extensions is None:
|
|
319
|
+
# Compilation failed and printed error message
|
|
320
|
+
return None
|
|
321
|
+
assert len(extensions) == 1
|
|
322
|
+
extension = extensions[0]
|
|
323
|
+
self._code_cache[key] = module_name
|
|
324
|
+
|
|
325
|
+
if args.pgo:
|
|
326
|
+
self._profile_pgo_wrapper(extension, lib_dir)
|
|
327
|
+
|
|
328
|
+
def print_compiler_output(stdout, stderr, where):
|
|
329
|
+
# On windows, errors are printed to stdout, we redirect both to sys.stderr.
|
|
330
|
+
print_captured(stdout, where, "Content of stdout:\n")
|
|
331
|
+
print_captured(stderr, where, "Content of stderr:\n")
|
|
332
|
+
|
|
333
|
+
get_stderr = get_stdout = None
|
|
334
|
+
try:
|
|
335
|
+
with captured_fd(1) as get_stdout:
|
|
336
|
+
with captured_fd(2) as get_stderr:
|
|
337
|
+
self._build_extension(
|
|
338
|
+
extension, lib_dir, pgo_step_name='use' if args.pgo else None, quiet=args.quiet)
|
|
339
|
+
except (distutils.errors.CompileError, distutils.errors.LinkError):
|
|
340
|
+
# Build failed, print error message from compiler/linker
|
|
341
|
+
print_compiler_output(get_stdout(), get_stderr(), sys.stderr)
|
|
342
|
+
return None
|
|
343
|
+
|
|
344
|
+
# Build seems ok, but we might still want to show any warnings that occurred
|
|
345
|
+
print_compiler_output(get_stdout(), get_stderr(), sys.stdout)
|
|
346
|
+
|
|
347
|
+
module = load_dynamic(module_name, module_path)
|
|
348
|
+
self._import_all(module)
|
|
349
|
+
|
|
350
|
+
if args.annotate:
|
|
351
|
+
try:
|
|
352
|
+
with open(html_file, encoding='utf-8') as f:
|
|
353
|
+
annotated_html = f.read()
|
|
354
|
+
except OSError as e:
|
|
355
|
+
# File could not be opened. Most likely the user has a version
|
|
356
|
+
# of Cython before 0.15.1 (when `cythonize` learned the
|
|
357
|
+
# `force` keyword argument) and has already compiled this
|
|
358
|
+
# exact source without annotation.
|
|
359
|
+
print('Cython completed successfully but the annotated '
|
|
360
|
+
'source could not be read.', file=sys.stderr)
|
|
361
|
+
print(e, file=sys.stderr)
|
|
362
|
+
else:
|
|
363
|
+
return display.HTML(self.clean_annotated_html(annotated_html))
|
|
364
|
+
|
|
365
|
+
def _profile_pgo_wrapper(self, extension, lib_dir):
|
|
366
|
+
"""
|
|
367
|
+
Generate a .c file for a separate extension module that calls the
|
|
368
|
+
module init function of the original module. This makes sure that the
|
|
369
|
+
PGO profiler sees the correct .o file of the final module, but it still
|
|
370
|
+
allows us to import the module under a different name for profiling,
|
|
371
|
+
before recompiling it into the PGO optimised module. Overwriting and
|
|
372
|
+
reimporting the same shared library is not portable.
|
|
373
|
+
"""
|
|
374
|
+
extension = copy.copy(extension) # shallow copy, do not modify sources in place!
|
|
375
|
+
module_name = extension.name
|
|
376
|
+
pgo_module_name = '_pgo_' + module_name
|
|
377
|
+
pgo_wrapper_c_file = os.path.join(lib_dir, pgo_module_name + '.c')
|
|
378
|
+
with open(pgo_wrapper_c_file, 'w', encoding='utf-8') as f:
|
|
379
|
+
f.write(textwrap.dedent("""
|
|
380
|
+
#include "Python.h"
|
|
381
|
+
extern PyMODINIT_FUNC PyInit_%(module_name)s(void);
|
|
382
|
+
PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void); /*proto*/
|
|
383
|
+
PyMODINIT_FUNC PyInit_%(pgo_module_name)s(void) {
|
|
384
|
+
return PyInit_%(module_name)s();
|
|
385
|
+
}
|
|
386
|
+
""" % {'module_name': module_name, 'pgo_module_name': pgo_module_name}))
|
|
387
|
+
|
|
388
|
+
extension.sources = extension.sources + [pgo_wrapper_c_file] # do not modify in place!
|
|
389
|
+
extension.name = pgo_module_name
|
|
390
|
+
|
|
391
|
+
self._build_extension(extension, lib_dir, pgo_step_name='gen')
|
|
392
|
+
|
|
393
|
+
# import and execute module code to generate profile
|
|
394
|
+
so_module_path = os.path.join(lib_dir, pgo_module_name + self.so_ext)
|
|
395
|
+
load_dynamic(pgo_module_name, so_module_path)
|
|
396
|
+
|
|
397
|
+
def _cythonize(self, module_name, code, lib_dir, args, quiet=True):
|
|
398
|
+
pyx_file = os.path.join(lib_dir, module_name + '.pyx')
|
|
399
|
+
|
|
400
|
+
c_include_dirs = args.include
|
|
401
|
+
c_src_files = list(map(str, args.src))
|
|
402
|
+
if 'numpy' in code:
|
|
403
|
+
import numpy
|
|
404
|
+
c_include_dirs.append(numpy.get_include())
|
|
405
|
+
with open(pyx_file, 'w', encoding='utf-8') as f:
|
|
406
|
+
f.write(code)
|
|
407
|
+
extension = Extension(
|
|
408
|
+
name=module_name,
|
|
409
|
+
sources=[pyx_file] + c_src_files,
|
|
410
|
+
include_dirs=c_include_dirs,
|
|
411
|
+
library_dirs=args.library_dirs,
|
|
412
|
+
extra_compile_args=args.compile_args,
|
|
413
|
+
extra_link_args=args.link_args,
|
|
414
|
+
libraries=args.lib,
|
|
415
|
+
language='c++' if args.cplus else 'c',
|
|
416
|
+
)
|
|
417
|
+
try:
|
|
418
|
+
opts = dict(
|
|
419
|
+
quiet=quiet,
|
|
420
|
+
annotate=args.annotate,
|
|
421
|
+
force=True,
|
|
422
|
+
language_level=min(3, sys.version_info[0]),
|
|
423
|
+
)
|
|
424
|
+
if args.language_level is not None:
|
|
425
|
+
assert args.language_level in (2, 3)
|
|
426
|
+
opts['language_level'] = args.language_level
|
|
427
|
+
return cythonize([extension], **opts)
|
|
428
|
+
except CompileError:
|
|
429
|
+
return None
|
|
430
|
+
|
|
431
|
+
def _build_extension(self, extension, lib_dir, temp_dir=None, pgo_step_name=None, quiet=True):
|
|
432
|
+
build_extension = self._get_build_extension(
|
|
433
|
+
extension, lib_dir=lib_dir, temp_dir=temp_dir, pgo_step_name=pgo_step_name)
|
|
434
|
+
old_threshold = None
|
|
435
|
+
try:
|
|
436
|
+
if not quiet:
|
|
437
|
+
old_threshold = distutils.log.set_threshold(distutils.log.DEBUG)
|
|
438
|
+
build_extension.run()
|
|
439
|
+
finally:
|
|
440
|
+
if not quiet and old_threshold is not None:
|
|
441
|
+
distutils.log.set_threshold(old_threshold)
|
|
442
|
+
|
|
443
|
+
def _add_pgo_flags(self, build_extension, step_name, temp_dir):
|
|
444
|
+
compiler_type = build_extension.compiler.compiler_type
|
|
445
|
+
if compiler_type == 'unix':
|
|
446
|
+
compiler_cmd = build_extension.compiler.compiler_so
|
|
447
|
+
# TODO: we could try to call "[cmd] --version" for better insights
|
|
448
|
+
if not compiler_cmd:
|
|
449
|
+
pass
|
|
450
|
+
elif 'clang' in compiler_cmd or 'clang' in compiler_cmd[0]:
|
|
451
|
+
compiler_type = 'clang'
|
|
452
|
+
elif 'icc' in compiler_cmd or 'icc' in compiler_cmd[0]:
|
|
453
|
+
compiler_type = 'icc'
|
|
454
|
+
elif 'gcc' in compiler_cmd or 'gcc' in compiler_cmd[0]:
|
|
455
|
+
compiler_type = 'gcc'
|
|
456
|
+
elif 'g++' in compiler_cmd or 'g++' in compiler_cmd[0]:
|
|
457
|
+
compiler_type = 'gcc'
|
|
458
|
+
config = PGO_CONFIG.get(compiler_type)
|
|
459
|
+
orig_flags = []
|
|
460
|
+
if config and step_name in config:
|
|
461
|
+
flags = [f.format(TEMPDIR=temp_dir) for f in config[step_name]]
|
|
462
|
+
for extension in build_extension.extensions:
|
|
463
|
+
orig_flags.append((extension.extra_compile_args, extension.extra_link_args))
|
|
464
|
+
extension.extra_compile_args = extension.extra_compile_args + flags
|
|
465
|
+
extension.extra_link_args = extension.extra_link_args + flags
|
|
466
|
+
else:
|
|
467
|
+
print("No PGO %s configuration known for C compiler type '%s'" % (step_name, compiler_type),
|
|
468
|
+
file=sys.stderr)
|
|
469
|
+
return orig_flags
|
|
470
|
+
|
|
471
|
+
@property
|
|
472
|
+
def so_ext(self):
|
|
473
|
+
"""The extension suffix for compiled modules."""
|
|
474
|
+
try:
|
|
475
|
+
return self._so_ext
|
|
476
|
+
except AttributeError:
|
|
477
|
+
self._so_ext = self._get_build_extension().get_ext_filename('')
|
|
478
|
+
return self._so_ext
|
|
479
|
+
|
|
480
|
+
def _clear_distutils_mkpath_cache(self):
|
|
481
|
+
"""clear distutils mkpath cache
|
|
482
|
+
|
|
483
|
+
prevents distutils from skipping re-creation of dirs that have been removed
|
|
484
|
+
"""
|
|
485
|
+
try:
|
|
486
|
+
from distutils.dir_util import _path_created
|
|
487
|
+
except ImportError:
|
|
488
|
+
pass
|
|
489
|
+
else:
|
|
490
|
+
_path_created.clear()
|
|
491
|
+
|
|
492
|
+
def _get_build_extension(self, extension=None, lib_dir=None, temp_dir=None,
|
|
493
|
+
pgo_step_name=None, _build_ext=build_ext):
|
|
494
|
+
self._clear_distutils_mkpath_cache()
|
|
495
|
+
dist = Distribution()
|
|
496
|
+
config_files = dist.find_config_files()
|
|
497
|
+
try:
|
|
498
|
+
config_files.remove('setup.cfg')
|
|
499
|
+
except ValueError:
|
|
500
|
+
pass
|
|
501
|
+
dist.parse_config_files(config_files)
|
|
502
|
+
|
|
503
|
+
if not temp_dir:
|
|
504
|
+
temp_dir = lib_dir
|
|
505
|
+
add_pgo_flags = self._add_pgo_flags
|
|
506
|
+
|
|
507
|
+
if pgo_step_name:
|
|
508
|
+
base_build_ext = _build_ext
|
|
509
|
+
class _build_ext(_build_ext):
|
|
510
|
+
def build_extensions(self):
|
|
511
|
+
add_pgo_flags(self, pgo_step_name, temp_dir)
|
|
512
|
+
base_build_ext.build_extensions(self)
|
|
513
|
+
|
|
514
|
+
build_extension = _build_ext(dist)
|
|
515
|
+
build_extension.finalize_options()
|
|
516
|
+
if temp_dir:
|
|
517
|
+
build_extension.build_temp = temp_dir
|
|
518
|
+
if lib_dir:
|
|
519
|
+
build_extension.build_lib = lib_dir
|
|
520
|
+
if extension is not None:
|
|
521
|
+
build_extension.extensions = [extension]
|
|
522
|
+
return build_extension
|
|
523
|
+
|
|
524
|
+
@staticmethod
|
|
525
|
+
def clean_annotated_html(html, include_style=True):
|
|
526
|
+
"""Clean up the annotated HTML source.
|
|
527
|
+
|
|
528
|
+
Strips the link to the generated C or C++ file, which we do not
|
|
529
|
+
present to the user.
|
|
530
|
+
|
|
531
|
+
Returns an HTML snippet (no <html>, <head>, or <body>),
|
|
532
|
+
containing only the style tag(s) and _contents_ of the body,
|
|
533
|
+
appropriate for embedding multiple times in cell output.
|
|
534
|
+
"""
|
|
535
|
+
# extract CSS and body, rather than full HTML document
|
|
536
|
+
chunks = []
|
|
537
|
+
if include_style:
|
|
538
|
+
styles = re.findall("<style.*</style>", html, re.MULTILINE | re.DOTALL)
|
|
539
|
+
chunks.extend(styles)
|
|
540
|
+
# extract body
|
|
541
|
+
body = re.search(
|
|
542
|
+
r"<body[^>]*>(.+)</body>", html, re.MULTILINE | re.DOTALL
|
|
543
|
+
).group(1)
|
|
544
|
+
|
|
545
|
+
# exclude link to generated file
|
|
546
|
+
r = re.compile('<p>Raw output: <a href="(.*)">(.*)</a>')
|
|
547
|
+
for line in body.splitlines():
|
|
548
|
+
if not r.match(line):
|
|
549
|
+
chunks.append(line)
|
|
550
|
+
return "\n".join(chunks)
|
|
551
|
+
|
|
552
|
+
__doc__ = __doc__.format(
|
|
553
|
+
# rST doesn't see the -+ flag as part of an option list, so we
|
|
554
|
+
# hide it from the module-level docstring.
|
|
555
|
+
CYTHON_DOC=dedent(CythonMagics.cython.__doc__
|
|
556
|
+
.replace('-+, --cplus', '--cplus ')),
|
|
557
|
+
CYTHON_INLINE_DOC=dedent(CythonMagics.cython_inline.__doc__),
|
|
558
|
+
CYTHON_PYXIMPORT_DOC=dedent(CythonMagics.cython_pyximport.__doc__),
|
|
559
|
+
)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from Cython.Compiler import (
|
|
4
|
+
MemoryView, Code, Options, Pipeline, Errors, Main, Symtab
|
|
5
|
+
)
|
|
6
|
+
from Cython.Compiler.StringEncoding import EncodedString
|
|
7
|
+
from Cython.Compiler.Scanning import SharedUtilitySourceDescriptor
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def create_shared_library_pipeline(context, scope, options, result):
|
|
11
|
+
|
|
12
|
+
parse = Pipeline.parse_stage_factory(context)
|
|
13
|
+
|
|
14
|
+
def generate_tree_factory(context):
|
|
15
|
+
def generate_tree(compsrc):
|
|
16
|
+
tree = parse(compsrc)
|
|
17
|
+
|
|
18
|
+
tree.scope.use_utility_code(
|
|
19
|
+
MemoryView.get_view_utility_code(options.shared_utility_qualified_name))
|
|
20
|
+
|
|
21
|
+
tree.scope.use_utility_code(MemoryView._get_memviewslice_declare_code())
|
|
22
|
+
tree.scope.use_utility_code(MemoryView._get_typeinfo_to_format_code())
|
|
23
|
+
context.include_directories.append(Code.get_utility_dir())
|
|
24
|
+
return tree
|
|
25
|
+
|
|
26
|
+
return generate_tree
|
|
27
|
+
|
|
28
|
+
def generate_c_utilities(module_node):
|
|
29
|
+
UtilityCode = Code.UtilityCode
|
|
30
|
+
match_special = UtilityCode.get_special_comment_matcher('/')
|
|
31
|
+
for c_utility_file in os.listdir(Code.get_utility_dir()):
|
|
32
|
+
if not c_utility_file.endswith('.c'):
|
|
33
|
+
continue
|
|
34
|
+
for line in Code.read_utilities_hook(c_utility_file):
|
|
35
|
+
if not ((m := match_special(line)) and (name := m.group('name'))):
|
|
36
|
+
continue
|
|
37
|
+
if not (section_title := UtilityCode.match_section_title(name)):
|
|
38
|
+
continue
|
|
39
|
+
name, section_type = section_title.groups()
|
|
40
|
+
if section_type == 'export':
|
|
41
|
+
module_node.scope.use_utility_code(UtilityCode.load_cached(name, c_utility_file))
|
|
42
|
+
return module_node
|
|
43
|
+
|
|
44
|
+
orig_cimport_from_pyx = Options.cimport_from_pyx
|
|
45
|
+
|
|
46
|
+
def set_cimport_from_pyx(cimport_from_pyx):
|
|
47
|
+
def inner(node):
|
|
48
|
+
Options.cimport_from_pyx = cimport_from_pyx
|
|
49
|
+
return node
|
|
50
|
+
return inner
|
|
51
|
+
|
|
52
|
+
return [
|
|
53
|
+
# "cimport_from_pyx=True" to force generating __Pyx_ExportFunction
|
|
54
|
+
set_cimport_from_pyx(True),
|
|
55
|
+
generate_tree_factory(context),
|
|
56
|
+
*Pipeline.create_pipeline(context, 'pyx', exclude_classes=()),
|
|
57
|
+
generate_c_utilities,
|
|
58
|
+
Pipeline.inject_pxd_code_stage_factory(context),
|
|
59
|
+
Pipeline.inject_utility_code_stage_factory(context, internalise_c_class_entries=False),
|
|
60
|
+
Pipeline.inject_utility_pxd_code_stage_factory(context),
|
|
61
|
+
Pipeline.abort_on_errors,
|
|
62
|
+
Pipeline.generate_pyx_code_stage_factory(options, result),
|
|
63
|
+
set_cimport_from_pyx(orig_cimport_from_pyx),
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def generate_shared_module(options):
|
|
68
|
+
Errors.reset()
|
|
69
|
+
|
|
70
|
+
dest_c_file = options.shared_c_file_path
|
|
71
|
+
pyx_file = os.path.splitext(dest_c_file)[0] + '.pyx'
|
|
72
|
+
module_name = os.path.splitext(os.path.basename(dest_c_file))[0]
|
|
73
|
+
|
|
74
|
+
context = Main.Context.from_options(options)
|
|
75
|
+
scope = Symtab.ModuleScope('MemoryView', parent_module = None, context = context, is_package=False)
|
|
76
|
+
|
|
77
|
+
source_desc = SharedUtilitySourceDescriptor(pyx_file)
|
|
78
|
+
comp_src = Main.CompilationSource(source_desc, EncodedString(module_name), os.getcwd())
|
|
79
|
+
result = Main.create_default_resultobj(comp_src, options)
|
|
80
|
+
|
|
81
|
+
pipeline = create_shared_library_pipeline(context, scope, options, result)
|
|
82
|
+
err, enddata = Pipeline.run_pipeline(pipeline, comp_src)
|
|
83
|
+
|
|
84
|
+
return err, enddata
|