Skip to content

Fix parse_log.py Python 2 print statement breaking Python 3#7103

Open
Chessing234 wants to merge 2 commits intoBVLC:masterfrom
Chessing234:fix/parse-log-python3-print
Open

Fix parse_log.py Python 2 print statement breaking Python 3#7103
Chessing234 wants to merge 2 commits intoBVLC:masterfrom
Chessing234:fix/parse-log-python3-print

Conversation

@Chessing234
Copy link
Copy Markdown

Bug

`tools/extra/parse_log.py` is a ships-with-caffe utility with shebang `#!/usr/bin/env python`. The rest of the module is Python 3 compatible — the earlier `write_csv` branch even uses function-style `print('Not writing %s; no lines to write' % output_filename)`. But a single line later in the same function still uses the Python 2 `print` statement:

```python
if verbose:
print 'Wrote %s' % output_filename
```

Root cause

Running the script with Python 3 fails immediately with

SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

at import/parse time, before any of the actual log-parsing logic can execute. The script is therefore unusable on Python 3 interpreters despite otherwise being compatible.

Fix

Wrap the format expression in parentheses so the line is valid under both Python 2.7 and Python 3.x, matching the function-style `print` already used elsewhere in the same file.

Blob::offset(n, c, h, w) guards the inputs with CHECK_LE(n, num()),
CHECK_LE(c, channels()), CHECK_LE(h, height()), CHECK_LE(w, width()).
A valid index is in [0, dim), so these allow n == num(), c == channels()
etc. to pass — one past the end of the buffer, producing a stale/OOB
linear offset from `((n * channels() + c) * height() + h) * width() + w`.

The vector overload of offset() already uses the correct pattern
(CHECK_LT(indices[i], shape(i))). Switch the four legacy-accessor checks
to CHECK_LT to match. Fixes BVLC#6391.
tools/extra/parse_log.py is a shipped utility (shebang `#!/usr/bin/env
python`) that otherwise uses Python 3 compatible syntax — earlier in the
file it already calls `print('Not writing %s; no lines to write' % ...)`
using function-style print.

Line 175 is the odd one:

    print 'Wrote %s' % output_filename

which is the Python 2 `print` statement. Running the script with
Python 3 fails at parse time with
"SyntaxError: Missing parentheses in call to 'print'" before any
parsing can take place, even though the rest of the file runs fine on
Python 3.

Wrap the expression in parentheses so the module imports and runs on
both Python 2.7 and Python 3.x.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant