Main program, used when run as a script. The optional 'args' argument specifies the command line to be parsed, defaulting to sys.argv[1:]. The return value is an exit code to be passed to sys.exit(); it may be None to indicate success. When an exception happens during timing,
(args=None, *, _wrap_timer=None)
| 252 | |
| 253 | |
| 254 | def main(args=None, *, _wrap_timer=None): |
| 255 | """Main program, used when run as a script. |
| 256 | |
| 257 | The optional 'args' argument specifies the command line to be parsed, |
| 258 | defaulting to sys.argv[1:]. |
| 259 | |
| 260 | The return value is an exit code to be passed to sys.exit(); it |
| 261 | may be None to indicate success. |
| 262 | |
| 263 | When an exception happens during timing, a traceback is printed to |
| 264 | stderr and the return value is 1. Exceptions at other times |
| 265 | (including the template compilation) are not caught. |
| 266 | |
| 267 | '_wrap_timer' is an internal interface used for unit testing. If it |
| 268 | is not None, it must be a callable that accepts a timer function |
| 269 | and returns another timer function (used for unit testing). |
| 270 | """ |
| 271 | import getopt |
| 272 | if args is None: |
| 273 | args = sys.argv[1:] |
| 274 | import _colorize |
| 275 | colorize = _colorize.can_colorize() |
| 276 | |
| 277 | try: |
| 278 | opts, args = getopt.getopt(args, "n:u:s:r:pt:vh", |
| 279 | ["number=", "setup=", "repeat=", |
| 280 | "process", "target-time=", |
| 281 | "verbose", "unit=", "help"]) |
| 282 | except getopt.error as err: |
| 283 | print(err) |
| 284 | print("use -h/--help for command line help") |
| 285 | return 2 |
| 286 | |
| 287 | timer = default_timer |
| 288 | stmt = "\n".join(args) or "pass" |
| 289 | number = 0 # auto-determine |
| 290 | target_time = default_target_time |
| 291 | setup = [] |
| 292 | repeat = default_repeat |
| 293 | verbose = 0 |
| 294 | time_unit = None |
| 295 | units = {"nsec": 1e-9, "usec": 1e-6, "msec": 1e-3, "sec": 1.0} |
| 296 | precision = 3 |
| 297 | for o, a in opts: |
| 298 | if o in ("-n", "--number"): |
| 299 | number = int(a) |
| 300 | if o in ("-s", "--setup"): |
| 301 | setup.append(a) |
| 302 | if o in ("-u", "--unit"): |
| 303 | if a in units: |
| 304 | time_unit = a |
| 305 | else: |
| 306 | print("Unrecognized unit. Please select nsec, usec, msec, or sec.", |
| 307 | file=sys.stderr) |
| 308 | return 2 |
| 309 | if o in ("-r", "--repeat"): |
| 310 | repeat = int(a) |
| 311 | if repeat <= 0: |
no test coverage detected
searching dependent graphs…