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