(filename)
| 18 | |
| 19 | |
| 20 | def get_cython_contents(filename): |
| 21 | if filename.endswith(".pyc"): |
| 22 | filename = filename[:-1] |
| 23 | |
| 24 | state = "regular" |
| 25 | |
| 26 | replacements = [] |
| 27 | |
| 28 | new_contents = [] |
| 29 | with open(filename, "r") as stream: |
| 30 | for line in stream: |
| 31 | strip = line.strip() |
| 32 | if state == "regular": |
| 33 | if strip == "# IFDEF CYTHON": |
| 34 | state = "cython" |
| 35 | |
| 36 | new_contents.append( |
| 37 | "%s -- DONT EDIT THIS FILE (it is automatically generated)\n" % line.replace("\n", "").replace("\r", "") |
| 38 | ) |
| 39 | continue |
| 40 | |
| 41 | new_contents.append(line) |
| 42 | |
| 43 | elif state == "cython": |
| 44 | if strip == "# ELSE": |
| 45 | state = "nocython" |
| 46 | new_contents.append(line) |
| 47 | continue |
| 48 | |
| 49 | elif strip == "# ENDIF": |
| 50 | state = "regular" |
| 51 | new_contents.append(line) |
| 52 | continue |
| 53 | |
| 54 | if strip == "#": |
| 55 | continue |
| 56 | |
| 57 | assert strip.startswith("# "), 'Line inside # IFDEF CYTHON must start with "# ". Found: %s' % (strip,) |
| 58 | strip = strip.replace("# ", "", 1).strip() |
| 59 | |
| 60 | if strip.startswith("cython_inline_constant:"): |
| 61 | strip = strip.replace("cython_inline_constant:", "") |
| 62 | word_to_replace, replacement = strip.split("=") |
| 63 | replacements.append((word_to_replace.strip(), replacement.strip())) |
| 64 | continue |
| 65 | |
| 66 | line = line.replace("# ", "", 1) |
| 67 | new_contents.append(line) |
| 68 | |
| 69 | elif state == "nocython": |
| 70 | if strip == "# ENDIF": |
| 71 | state = "regular" |
| 72 | new_contents.append(line) |
| 73 | continue |
| 74 | new_contents.append("# %s" % line) |
| 75 | |
| 76 | assert state == "regular", "Error: # IFDEF CYTHON found without # ENDIF" |
| 77 |
no test coverage detected