How it works...

In step 1, we hardcoded a breakpoint in the source code of the method by calling the set_trace() method of the pdb module from the Python standard library. When this method is executed, the normal flow of the program stops, and you get a (Pdb) prompt in which you can enter pdb commands.

Step 2 calls the stock_level_export() method using shell mode. It is also possible to restart the server normally and use the web interface to generate a call to the method you need to trace by clicking on the appropriate elements of the user interface.

When you need to manually step through some code using the Python debugger, here are a few tips that will make your life easier:

  • Reduce the logging level to avoid having too many log lines, which pollutes the output of the debugger. Starting at the ERROR level is generally fine. You may want to enable some specific loggers with a higher verbosity, which you can do using the --log-handler command-line option (refer to the Producing server logs to help debug methods recipe).
  • Run the server with --workers=0 to avoid any multiprocessing issues that can cause the same breakpoint to be reached twice in two different processes.
  • Run the server with --max-cron-threads=0 to disable the processing of the ir.cron periodic tasks, which may otherwise trigger while you are stepping through the method, which produces unwanted logs and side effects.

Steps 3 to 8 use several pdb commands to step through the execution of the method. Here's a summary of the main commands of pdb. Most of these are also available using the first letter as a shortcut. We indicate this here by having the optional letters between parentheses:

  • h(elp): This displays help about the pdb commands.
  • a(rgs): This shows the value of the arguments of the current function/methods.
  • l(ist): This displays the source code being executed in chunks of 11 lines, initially centered on the current line. Successive calls will move further in the source code file. Optionally, you can pass two integers at the start and end, which specify the region to display.
  • p: This prints a variable.
  • pp: This pretty-prints a variable (useful with lists and dictionaries).
  • w(here): This shows the call stack, with the current line at the bottom and the Python interpreter at the top.
  • u(p): This moves up one level in the call stack.
  • d(own): This moves down one level in the call stack.
  • n(ext): This executes the current line of code and then stops.
  • s(tep): This is to step inside the execution of a method call.
  • r(eturn): This resumes the execution of the current method until it returns.
  • c(ont(inue)): This resumes the execution of the program until the next breakpoint is hit.
  • b(reak) <args>: This creates a new breakpoint and displays its identifier; args can be one of the following:
    • <empty>: This lists all breakpoints.
    • line_number: This breaks at the specified line in the current file.
    • filename:line_number: This breaks at the specified line of the specified file (which is searched for in the directories of sys.path).
    • function_name: This breaks at the first line of the specified function.
  • tbreak <args>: This is similar to break, but the breakpoint will be canceled after it has been reached, so successive execution of the line won't trigger it twice.
  • disable bp_id: This disables a breakpoint by ID.
  • enable bl_id: This enables a disabled breakpoint by ID.
  • j(ump) lineno: The next line to execute will be the one specified. This can be used to rerun or skip some lines.
  • (!) statement: This executes a Python statement. The ! character can be omitted if the command does not look like a pdb command. For instance, you need it if you want to set the value of a variable named a, because a is the shortcut for the args command.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset