Skip to content

adding complete code to the annotated html-file#2858

Merged
scoder merged 17 commits intocython:masterfrom
realead:complete_code_in_annotate
May 30, 2019
Merged

adding complete code to the annotated html-file#2858
scoder merged 17 commits intocython:masterfrom
realead:complete_code_in_annotate

Conversation

@realead
Copy link
Contributor

@realead realead commented Feb 21, 2019

Another alternative to close #2855 by adding the complete cythonized code to the annotated html.

Question: Maybe there should be an option, that the whole code is added only if asked (i.e. from IPython with --show-cythonized-code-option)?

@realead
Copy link
Contributor Author

realead commented Mar 27, 2019

@scoder sorry for pestering you: do you think this PR could be a solution for #2855? Should it be improved? If you think that #2855 is not worth bothering - please also tell - I'm totally Ok with that.

Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with the feature in general. However, the C code can be very large and the HTML-ification time consuming, so yes, I think this should use a new directive (similar to annotate=True). Not sure what a good name would be…

group_name, match.group(group_name))

lines = self._htmlify_code(cython_code).splitlines()
lines = self._htmlify_code(cython_code, True).splitlines()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing things like True and False into a function without further context screams for a named keyword argument to make it clear what the value means. However, in this case, it should actually use a language name: "cython" vs. "c" makes it even clearer what is intended.

@realead realead force-pushed the complete_code_in_annotate branch from c244f50 to 1007471 Compare March 27, 2019 22:37
@realead
Copy link
Contributor Author

realead commented Mar 28, 2019

@scoder Maybe it would make sense to introduce different levels of "annotate":

level 0 (also False, no annotation)
level 1 (also True, annotation without whole c-code, as it is the case now)
level 2 (additonal emission of the whole c-code)

See for example the current state of this PR. Or is this too hacky?

@realead realead force-pushed the complete_code_in_annotate branch from 1007471 to 0054e74 Compare April 4, 2019 19:59
@realead realead force-pushed the complete_code_in_annotate branch from 0054e74 to 63c1008 Compare April 13, 2019 20:50
Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with adding this, but would be happy about a more obvious interface.
A test is also missing, see TestIPythonMagic.py.

'-a', '--annotate', action='store_true', default=False,
help="Produce a colorized HTML version of the source."
'-a', '--annotate', nargs='?', const=1, type=int,
help="Produce a colorized HTML version of the source. Use --annotate=2 to include complete generated C/C++-code."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to at least restrict the option values to 1 or 2 and nothing else, but actually, this option still doesn't feel right to me. When I see --annotate=2, I wouldn't know what that does.

@realead realead force-pushed the complete_code_in_annotate branch from 63c1008 to 4caa56a Compare May 9, 2019 05:22
Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a regex check in tests/run/annotate_html.pyx that this is disabled by default.

I think it's then also worth adding a new test module with a directive # cython: annotate=fullc at the top that tests this new feature.

@scoder
Copy link
Contributor

scoder commented May 9, 2019

Oh, and there is probably also a way to test the interaction with IPython in TestIpythonMagic.py

@realead realead force-pushed the complete_code_in_annotate branch from c66122c to a2ab67f Compare May 9, 2019 21:58
@realead
Copy link
Contributor Author

realead commented May 10, 2019

@scoder

Let's keep the variable names as they are now and just change this to --annotate=fullc to keep it typeable.

Done.

Please also add a regex check in tests/run/annotate_html.pyx that this is disabled by default.

Done

Oh, and there is probably also a way to test the interaction with IPython in TestIpythonMagic.py

I'm not sure I understand. I have already added test cases in TestIpythonMagic.py (4caa56a) but this is not what you mean?

I think it's then also worth adding a new test module with a directive # cython: annotate=fullc at the top that tests this new feature.

IIRC for that to work "annotate" must be a compiler-directive but right now it is an option.

@scoder
Copy link
Contributor

scoder commented May 10, 2019

already added test cases

Sorry, my fault. They are ok. I somehow missed them in the last review, probably just looking at selected changes.

for that to work "annotate" must be a compiler-directive but right now it is an option.

It's … both. And it would be weird if this feature worked in one place but not the others.

I think it should already work as a directive (just needs a test) but not as option to the cythonize frontend script. See Cythonize.py for the argument parser.

Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. I'll stop bike-shedding on names from now on. :)


>>> import re
>>> assert re.search('<pre .*def.* .*mixed_test.*</pre>', html)
>>> assert not re.search('Complete\scythonized\scode', html) # per default no complete c code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, if we ever change that text line in the output, we will almost certainly forget to adapt this test. Is there some structural element (or something in the C code) that we could look for instead? Maybe the (exact) C file version header line /* Generated by Cython , that shouldn't normally appear in the annotated user output. We use the version header as user detectable Cython file header, so that's not going to change (ever ;-) ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first line in every annotated html is "Generated by Cython XXX" - quite close to "/* Generated by Cython ";)

I have added an explicit marker rather than using a magic value - not sure it is much better though...

@realead
Copy link
Contributor Author

realead commented May 10, 2019

I think it should already work as a directive (just needs a test) but not as option to the cythonize frontend script. See Cythonize.py for the argument parser.

Right now, parsing directives from comment line is called via Options.parse_directive_list(directives_string, ignore_unknown=True) https://github.com/cython/cython/blob/master/Cython/Compiler/Parsing.py#L3682 and because annotate isn't part of _directive_defaults (https://github.com/cython/cython/blob/master/Cython/Compiler/Options.py#L172) it is ignored.

I was unsure about adding it "annotate" to directive_defaults because it is already in Options, but also setting ignore_unknown to False doesn't feel right.

Maybe it is best to adjust the argument parser of cythonize?

@realead
Copy link
Contributor Author

realead commented May 12, 2019

I also have added --annotate=fullc to cython. However, in cythonize, optparse is used - need to figure out how '-a', '--annotate', nargs='?', const="default", type=str, choices={"default","fullc"} from argparse can be mapped to optparse.

But as support for Python2.6 is (soon) dropped, maybe it is time to replace optparse through argparse?



def parse_command_line(args):
print("parsing line!")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

@scoder
Copy link
Contributor

scoder commented May 14, 2019

setting ignore_unknown to False doesn't feel right

Yeah, seems worth a warning at some point…

because annotate isn't part of _directive_defaults, it is ignored.

Adding it to directive_defaults seems ok to me.

maybe it is time to replace optparse through argparse?

Feel free to write a PR for that. :)

@scoder
Copy link
Contributor

scoder commented May 14, 2019

Regarding argparse, BTW, there's a hugely old PR that is rather outdated but tried to implement a cross-Python-version CLI at the time. Probably not worth reviving but might still serve as a source of some inspiration.

@realead realead force-pushed the complete_code_in_annotate branch from 7945167 to 29adcba Compare May 24, 2019 19:54
@realead
Copy link
Contributor Author

realead commented May 25, 2019

@scoder I've added tests for Options.annotate (29adcba) and also for cythonize (2a81e45#diff-6210f720b78ff675f6f1c6a6be51de98).

I didn't make a test for # cython: annotate=fullc because in the current version # cython: annotate=True isn't possible as well (annotate isn't a compiler directive). If one whish to make annotate a compiler directive - it is probably better done in another PR, but from my point of view there is no need for that ( even if the whole thing with Compiler.Options, options, compiler_directives is at least a little bit confusing - moving only annotate to compiler_directives would rather make it more complicated).

Not sure, why some tests are failing though.


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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having __file__ available during module initialisation is a Py3.5+ feature based on PEP-489.
Instead, you can define a test function in this module and call it from the command section above.
Or, just use the plain file name without directory. The .html file ends up right next to the source file.

@realead realead force-pushed the complete_code_in_annotate branch from 6606a32 to 34599c1 Compare May 28, 2019 21:02
@realead
Copy link
Contributor Author

realead commented May 29, 2019

@scoder Thanks. I've updated the test cases, now it is green.

@scoder scoder added this to the 3.0 milestone May 30, 2019
@scoder scoder merged commit 55dcc92 into cython:master May 30, 2019
@scoder
Copy link
Contributor

scoder commented May 30, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cosider adding an option to show the cythonized code in ipython-magic

2 participants