Skip to content

Commit

Permalink
adding annotate=fullc/default to cythonize-options
Browse files Browse the repository at this point in the history
  • Loading branch information
realead committed May 24, 2019
1 parent 0e38465 commit 2a81e45
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Cython/Build/Cythonize.py
Expand Up @@ -168,9 +168,10 @@ def __call__(self, parser, namespace, values, option_string=None):
help='use Python 3 syntax mode by default')
parser.add_argument('--3str', dest='language_level', action='store_const', const='3str',
help='use Python 3 syntax mode by default')
parser.add_argument('-a', '--annotate', dest='annotate', action='store_true', default=None,
help='generate annotated HTML page for source files')

parser.add_argument('-a', '--annotate', nargs='?', const='default', type=str, choices={'default','fullc'},
help='Produce a colorized HTML version of the source. '
'Use --annotate=fullc to include entire '
'generated C/C++-code.')
parser.add_argument('-x', '--exclude', metavar='PATTERN', dest='excludes',
action='append', default=[],
help='exclude certain file patterns from the compilation')
Expand Down
19 changes: 16 additions & 3 deletions Cython/Build/Tests/TestCythonizeArgsParser.py
@@ -1,6 +1,7 @@
from Cython.Build.Cythonize import create_args_parser, parse_args_raw, parse_args
from unittest import TestCase

import argparse

class TestCythonizeArgsParser(TestCase):

Expand Down Expand Up @@ -233,13 +234,25 @@ def test_annotate_short(self):
options, args = self.parse_args(['-a'])
self.assertFalse(args)
self.assertTrue(self.are_default(options, ['annotate']))
self.assertEqual(options.annotate, True)
self.assertEqual(options.annotate, 'default')

def test_annotate_long(self):
options, args = self.parse_args(['--annotate'])
self.assertFalse(args)
self.assertTrue(self.are_default(options, ['annotate']))
self.assertEqual(options.annotate, True)
self.assertEqual(options.annotate, 'default')

def test_annotate_fullc(self):
options, args = self.parse_args(['--annotate=fullc'])
self.assertFalse(args)
self.assertTrue(self.are_default(options, ['annotate']))
self.assertEqual(options.annotate, 'fullc')

def test_annotate_fullc(self):
options, args = self.parse_args(['-a=default'])
self.assertFalse(args)
self.assertTrue(self.are_default(options, ['annotate']))
self.assertEqual(options.annotate, 'default')

def test_exclude_short(self):
options, args = self.parse_args(['-x', '*.pyx'])
Expand Down Expand Up @@ -360,7 +373,7 @@ def test_file_inbetween(self):
options, args = self.parse_args(['-i', 'file.pyx', '-a'])
self.assertEqual(args, ['file.pyx'])
self.assertEqual(options.build_inplace, True)
self.assertEqual(options.annotate, True)
self.assertEqual(options.annotate, 'default')
self.assertTrue(self.are_default(options, ['build_inplace', 'annotate']))

def test_option_trailing(self):
Expand Down
50 changes: 50 additions & 0 deletions tests/build/cythonize_with_annotate.srctree
@@ -0,0 +1,50 @@
PYTHON setup.py build_ext --inplace
PYTHON -c "import not_annotated"
PYTHON -c "import default_annotated"
PYTHON -c "import fullc_annotated"
######## setup.py ########

from Cython.Build.Dependencies import cythonize

from distutils.core import setup

setup(
ext_modules = cythonize(["not_annotated.pyx"], language_level=3) +
cythonize(["default_annotated.pyx"], annotate=True, language_level=3) +
cythonize(["fullc_annotated.pyx"], annotate='fullc', language_level=3)
)
######## not_annotated.pyx ########
# check that html-file doesn't exist:

import os.path as os_path
module_path = os_path.join(os_path.dirname(__file__), os_path.basename(__file__).split('.', 1)[0])
assert not os_path.isfile(module_path+'.html')



######## default_annotated.pyx ########
# load html-site and check that the marker isn't there:

from codecs import open
import os.path as os_path
module_path = os_path.join(os_path.dirname(__file__), os_path.basename(__file__).split('.', 1)[0])
with open(module_path + '.html', 'r', 'utf8') as html_file:
html = html_file.read()

from Cython.Compiler.Annotate import AnnotationCCodeWriter
assert (AnnotationCCodeWriter.COMPLETE_CODE_TITLE not in html) # per default no complete c code



######## fullc_annotated.pyx ########
# load html-site and check that the marker is there:

from codecs import open
import os.path as os_path
module_path = os_path.join(os_path.dirname(__file__), os_path.basename(__file__).split('.', 1)[0])
with open(module_path + '.html', 'r', 'utf8') as html_file:
html = html_file.read()

from Cython.Compiler.Annotate import AnnotationCCodeWriter
assert (AnnotationCCodeWriter.COMPLETE_CODE_TITLE in html)

0 comments on commit 2a81e45

Please sign in to comment.