From abfc7177129d93de1153dd1642210e1499964844 Mon Sep 17 00:00:00 2001 From: tmaeno Date: Wed, 19 Apr 2023 17:27:52 +0200 Subject: [PATCH] for Decimal in getJobStatisticsPerSite's return --- PandaPkgInfo.py | 2 +- pandaserver/taskbuffer/WrappedPickle.py | 19 ++++++++++++++++++- pandaserver/userinterface/UserIF.py | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/PandaPkgInfo.py b/PandaPkgInfo.py index f9f0e35dd..424a8c83d 100644 --- a/PandaPkgInfo.py +++ b/PandaPkgInfo.py @@ -1 +1 @@ -release_version = "0.0.50" +release_version = "0.0.51" diff --git a/pandaserver/taskbuffer/WrappedPickle.py b/pandaserver/taskbuffer/WrappedPickle.py index bea3a75cf..b60cb7003 100644 --- a/pandaserver/taskbuffer/WrappedPickle.py +++ b/pandaserver/taskbuffer/WrappedPickle.py @@ -1,4 +1,5 @@ import sys +import decimal from io import BytesIO try: # python 2 @@ -23,6 +24,20 @@ def __setattr__(self, key, value): pickle.Unpickler.__setattr__(self, key, value) +# conversion for unserializable values +def conversion_func(item): + if isinstance(item, list): + return [conversion_func(i) for i in item] + if isinstance(item, dict): + return {k: conversion_func(item[k]) for k in item} + if isinstance(item, decimal.Decimal): + if item == item.to_integral_value(): + item = int(item) + else: + item = float(item) + return item + + # wrapper to avoid de-serializing unsafe objects class WrappedPickle(object): # allowed modules and classes @@ -76,5 +91,7 @@ def loads(cls,pickle_string): # dumps @classmethod - def dumps(cls, obj): + def dumps(cls, obj, convert_to_safe=False): + if convert_to_safe: + obj = conversion_func(obj) return pickle.dumps(obj, protocol=0) diff --git a/pandaserver/userinterface/UserIF.py b/pandaserver/userinterface/UserIF.py index a823d59b0..8b560b7ed 100644 --- a/pandaserver/userinterface/UserIF.py +++ b/pandaserver/userinterface/UserIF.py @@ -350,7 +350,7 @@ def getJobStatisticsPerSite(self,predefined=False,workingGroup='',countryGroup=' ret = self.taskBuffer.getJobStatistics(readArchived,predefined,workingGroup,countryGroup,jobType, minPriority=minPriority) # serialize - return WrappedPickle.dumps(ret) + return WrappedPickle.dumps(ret, convert_to_safe=True) # get job statistics per site and resource