From 0ec1de6f21183b48a0cd27117260a6e986ed2158 Mon Sep 17 00:00:00 2001 From: Anatoly Artamonov Date: Fri, 9 Dec 2022 15:48:33 +0200 Subject: [PATCH 1/2] Revert "RPS-6454. Python API SDK stack overflow on parsing error message" This reverts commit f65d52b2361dbc29730ad82c85e7bbe7df644043. --- changes.txt | 3 --- smartlingApiSdk/ApiResponse.py | 8 +++---- smartlingApiSdk/version.py | 40 +++++++++++++++++----------------- test/test_ApiResponse.py | 9 -------- 4 files changed, 24 insertions(+), 36 deletions(-) diff --git a/changes.txt b/changes.txt index 7030da8..e2dc9be 100644 --- a/changes.txt +++ b/changes.txt @@ -1,6 +1,3 @@ -Dec 09, 2022 - 3.1.5 - - fix of ApiResponse get attribute may cause stack overflow on requesting non-existng attribute - May 24, 2022 - 3.1.4 - old tests updated to handle 423 errors for FilesAPI diff --git a/smartlingApiSdk/ApiResponse.py b/smartlingApiSdk/ApiResponse.py index 87f5d15..0375f56 100644 --- a/smartlingApiSdk/ApiResponse.py +++ b/smartlingApiSdk/ApiResponse.py @@ -42,9 +42,9 @@ def __init__(self, responseString, statusCode, headers): self.isApiResonse = self.responseDict.get('response', None) and dict == type(self.responseDict['response']) self.headers = headers if self.isApiResonse: - self.parseResponse() + self.parseResponse(responseString) - def parseResponse(self): + def parseResponse(self, responseString): """ parses json and fills object attributes according json attributes """ for k, v in list(self.responseDict['response'].items()): @@ -59,9 +59,9 @@ def __getattr__(self, key): return getattr(self.responseDict, key) try: - return self.__dict__[key] + return getattr(self, key) except: - raise AttributeError("ApiResponse has no attribute %r" % ( key )) + pass def __str__(self): return str(self.responseString) diff --git a/smartlingApiSdk/version.py b/smartlingApiSdk/version.py index ed0ec99..721146a 100644 --- a/smartlingApiSdk/version.py +++ b/smartlingApiSdk/version.py @@ -1,20 +1,20 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - - -""" Copyright 2012-2021 Smartling, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this work except in compliance with the License. - * You may obtain a copy of the License in the LICENSE file, or at: - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. -""" - -version = "3.1.5" +#!/usr/bin/python +# -*- coding: utf-8 -*- + + +""" Copyright 2012-2021 Smartling, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this work except in compliance with the License. + * You may obtain a copy of the License in the LICENSE file, or at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +""" + +version = "3.1.4" diff --git a/test/test_ApiResponse.py b/test/test_ApiResponse.py index a1cdb19..79d72ec 100644 --- a/test/test_ApiResponse.py +++ b/test/test_ApiResponse.py @@ -48,12 +48,3 @@ def test_ApiResponse_status(self): assert_equal(ar.data.fileUri, 'javaUTF16.properties') assert_equal(ar.data.completedStringCount, 0) assert_equal(ar.data.callbackUrl, 'http://yourdomain.com/callback') - - def test_ApiResponse_failed(self): - failed_json = '{"response":{"code":"VALIDATION_ERROR","errors":[{"key":null,"message":"File not found: test_import.xml_2.2.4_1629202583.584802","details":null}]}}' - ar = ApiResponse(failed_json, "404", {"Content-Type":"application-json"}) - try: - ua = ar.unexisting - except AttributeError as e: - assert_equal(str(e), "ApiResponse has no attribute 'unexisting'") - assert_equal(ar.code, "VALIDATION_ERROR") From c961e20c4b2b93ac900d74aade13e582f5c8aea7 Mon Sep 17 00:00:00 2001 From: Anatoly Artamonov Date: Fri, 9 Dec 2022 15:51:13 +0200 Subject: [PATCH 2/2] RPS-6454. Python API SDK stack overflow on parsing error message --- changes.txt | 3 +++ smartlingApiSdk/ApiResponse.py | 8 ++++---- smartlingApiSdk/version.py | 2 +- test/test_ApiResponse.py | 9 +++++++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/changes.txt b/changes.txt index e2dc9be..7030da8 100644 --- a/changes.txt +++ b/changes.txt @@ -1,3 +1,6 @@ +Dec 09, 2022 - 3.1.5 + - fix of ApiResponse get attribute may cause stack overflow on requesting non-existng attribute + May 24, 2022 - 3.1.4 - old tests updated to handle 423 errors for FilesAPI diff --git a/smartlingApiSdk/ApiResponse.py b/smartlingApiSdk/ApiResponse.py index 0375f56..87f5d15 100644 --- a/smartlingApiSdk/ApiResponse.py +++ b/smartlingApiSdk/ApiResponse.py @@ -42,9 +42,9 @@ def __init__(self, responseString, statusCode, headers): self.isApiResonse = self.responseDict.get('response', None) and dict == type(self.responseDict['response']) self.headers = headers if self.isApiResonse: - self.parseResponse(responseString) + self.parseResponse() - def parseResponse(self, responseString): + def parseResponse(self): """ parses json and fills object attributes according json attributes """ for k, v in list(self.responseDict['response'].items()): @@ -59,9 +59,9 @@ def __getattr__(self, key): return getattr(self.responseDict, key) try: - return getattr(self, key) + return self.__dict__[key] except: - pass + raise AttributeError("ApiResponse has no attribute %r" % ( key )) def __str__(self): return str(self.responseString) diff --git a/smartlingApiSdk/version.py b/smartlingApiSdk/version.py index 721146a..5648487 100644 --- a/smartlingApiSdk/version.py +++ b/smartlingApiSdk/version.py @@ -17,4 +17,4 @@ * limitations under the License. """ -version = "3.1.4" +version = "3.1.5" diff --git a/test/test_ApiResponse.py b/test/test_ApiResponse.py index 79d72ec..a1cdb19 100644 --- a/test/test_ApiResponse.py +++ b/test/test_ApiResponse.py @@ -48,3 +48,12 @@ def test_ApiResponse_status(self): assert_equal(ar.data.fileUri, 'javaUTF16.properties') assert_equal(ar.data.completedStringCount, 0) assert_equal(ar.data.callbackUrl, 'http://yourdomain.com/callback') + + def test_ApiResponse_failed(self): + failed_json = '{"response":{"code":"VALIDATION_ERROR","errors":[{"key":null,"message":"File not found: test_import.xml_2.2.4_1629202583.584802","details":null}]}}' + ar = ApiResponse(failed_json, "404", {"Content-Type":"application-json"}) + try: + ua = ar.unexisting + except AttributeError as e: + assert_equal(str(e), "ApiResponse has no attribute 'unexisting'") + assert_equal(ar.code, "VALIDATION_ERROR")