Trace Bug which happends only sometimes in CI

2024/10/11 18:24:00

I have a strange bug in python code which only happens sometimes in CI.

We can't reproduce it.

Where is the test code:

response=self.admin_client.post(url, post)
self.assertEqual(200, response.status_code, response)

Sometimes we get a 302 which happens since the form gets saved.

My idea to debug this:

with some_magic_trace.trace() as trace:response=self.admin_client.post(url, post)self.assertEqual(200, response.status_code, trace)

The trace should contain the python lines (filename, line offset, line as string) executed by the interpreter.

How to implement some_magic_trace.trace()?

Answer

The trace module gives you a very simple solution (different from what you are asking for, but simple enough to have a try.)

from trace import Tracetracer = Trace()
response = tracer.runfunc(self.admin_client.post, url, post)
self.assertEqual(200, response.status_code, response)

A more complex solution that entails creating a context manager that saves the trace and prints it only on exceptions, requires the use of sys.settrace. Just a template for your own implementation could be:

class MyTracer():def __init__(self):self.trace = Nonedef newscope(self, frame, event, arg):## real work should be done here, just minimal exampleself.trace.append((frame, event, arg))return Nonedef pprint(self):## real pretty printing of trace info should be done hereprint(self.trace)def __enter__(self):self.trace = []sys.settrace(self.newscope)return selfdef __exit__(self, exc_type, exc_val, exc_tb):sys.settrace(None)if exc_type is not None:self.pprint()## print some info gathered from exc_type, exc_val, exc_tb

Then you can:

with MyTracer():response=self.admin_client.post(url, post)self.assertEqual(200, response.status_code, response)

The idea is that a MyTracer instance has a tracer method newscope that saves some useful info in self.trace. On an abnormal exit from the context the pprint method is called; on a normal exit the trace info is discarded.

Most of the work has to be done in the tracing method newscope. Some concrete examples of tracing functions can be found here.

https://en.xdnf.cn/q/69741.html

Related Q&A

Limit neural network output to subset of trained classes

Is it possible to pass a vector to a trained neural network so it only chooses from a subset of the classes it was trained to recognize. For example, I have a network trained to recognize numbers and l…

Unable to fully remove border of PyQt QGraphicsView

I have tried calling self.setStyleSheet("background: transparent; border: transparent;") on a QGraphicsView, but it still leaves a 1 pixel border on the top edge. I have also tried replacing …

SqlAlchemy non persistent column

I am trying to define a model using sqlalchemy such that one of the columns i only need in memory for processing, i do not have a corresponding database column for that. I need it such that when i save…

Creating command line alias with python

I want to create command line aliases in one of my python scripts. Ive tried os.system(), subprocess.call() (with and without shell=True), and subprocess.Popen() but I had no luck with any of these me…

Remove unwanted lines in captcha text - opencv - python

I trying to get text from captcha image using opencv. Problem is text are masked with noise and it is complex to process with those horizontal line/noise.Original imageMy processed image :not sure how …

Double Summation in Python

I am trying to write a code to conduct a double summation (see pic) in which; M is the subjects, N is the Trials, Yijt is the measured wave form data (3d array)so far I have; Given Y is the data arra…

psycopg, double and single quotes insert

I ran into problems, while trying to insert to database:ur_psql.execute("""insert into smth(data, filedate, filedby)"""""" values(%s, NOW(), %s)""…

How to debug Python 2.7 code with VS Code?

For work I have to use Python 2.7. But when I use the "debug my python file" function in VS Code, I get an error. Even with a simple program, like : print()

Python Sequence of Numbers

Ive decided not to waste my summer and start learning python. I figured Id start learning looping techniques so I wanted to start with a basic list of numbers, aka, write a for loop that will generate…

Object initializer syntax (c#) in python?

I was wondering if there is a quick way to initialise an object in python. For example in c# you can instantiate an object and set the fields/properties like...SomeClass myObject = new SomeClass() { va…