Handling Exception in Python3 [duplicate] - python-3.x

This question already has an answer here:
Disable exception chaining in python 3
(1 answer)
Closed 2 years ago.
I'm trying to run this:
try:
number = int(number)
except ValueError:
raise InvalidValueError("%s is not a valid base10 number." % number)
So, when I set number = '51651a'
I'm getting this:
Traceback (most recent call last):
File "test.py", line 16, in decbin
number = int(number)
ValueError: invalid literal for int() with base 10: '51651a'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 51, in <module>
print(decbin('51651a', True))
File "test.py", line 18, in decbin
raise InvalidValueError("%s is not a valid base10 number." % number)
__main__.InvalidValueError: 51651a is not a valid base10 number.
My question is that is there any way I don't see the line saying, "During handling of the above exception, another exception occurred:"
and all that's above it.

What you're looking for is disabling Exception Chaining with from None 1.
Change your raise statement to
raise InvalidValueError("%s is not a valid base10 number." % number) from None
and only your custom exception will be raised, without reference to the ValueError exception originally caught.

Related

Google PubSub - restarting subscription after exception raised

Good day,
I am running some long-running async jobs using PubSub to trigger a function. Occasionally, the task may fail. In such cases, I simply want to log the exception, acknowledge the message, and restart the subscription to ensure that the subscriber is still pulling new messages after the failure has occurred.
I have placed some simplified code to demonstrate my current set up below:
try:
while True:
streaming_pull_future = workers.subscriber.subscribe(
subscription_path, callback=worker_task <- includes logic to ack() the message if it's failed before
)
print(f'Listening for messages on {subscription_path}')
try:
streaming_pull_future.result()
except Exception as e:
print(streaming_pull_future.cancelled()) #<-- this evaluates to false
streaming_pull_future.cancel() #<-- this results in RunTimeError: set_result can only be called once.
print(e)
except KeyboardInterrupt: # seems to be an issue as per Github PubSub issue #17. No keyboard interrupt
streaming_pull_future.cancel()
I keep seeing a RuntimeError: set_result can only be called oncewhen I execute the streaming_pull_future.cancel() in the exception handler. I checked whether perhaps the subscriber had already been cancelled but when I logged out the status it evaluated to False. Yet when I then call the cancel() method I get the error. I want to ensure that any threads are cleaned up before making a new subscription in the case where I could have several errors. Does anyone know why this is happening and a way around it?
I am running Python 3.7.4 with PubSub 1.2.0 and grpcio 1.27.1.
Update:
As per comments, please see a reproducible example. The stack trace raised is included:
Listening for messages on projects/trigger-web-app/subscriptions/load-job-sub
968432700946405
Top-level exception occurred in callback while processing a message
Traceback (most recent call last):
File "C:\..\lib\site-packages\google\cloud\pubsub_v1\subscriber\_protocol\streaming_pull_manager.py", line
71, in _wrap_callback_errors
callback(message)
File "test.py", line 19, in worker_task
a = 1/0 # cause an exception to be raised
ZeroDivisionError: division by zero
968424309156485
Top-level exception occurred in callback while processing a message
Traceback (most recent call last):
File "C:\...\lib\site-packages\google\cloud\pubsub_v1\subscriber\_protocol\streaming_pull_manager.py", line
71, in _wrap_callback_errors
callback(message)
File "test.py", line 19, in worker_task
a = 1/0 # cause an exception to be raised
ZeroDivisionError: division by zero
Traceback (most recent call last):
File "test.py", line 29, in main
streaming_pull_future.result()
File "C:...\lib\site-packages\google\cloud\pubsub_v1\futures.py", line 105, in result
raise err
File "C:\...\lib\site-packages\google\cloud\pubsub_v1\subscriber\_protocol\streaming_pull_manager.py", line
71, in _wrap_callback_errors
callback(message)
File "test.py", line 19, in worker_task
a = 1/0 # cause an exception to be raised
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 35, in <module>
main()
File "test.py", line 31, in main
streaming_pull_future.cancel()
File "C:\...\lib\site-packages\google\cloud\pubsub_v1\subscriber\futures.py", line 46, in cancel
return self._manager.close()
File "C:\...\lib\site-packages\google\cloud\pubsub_v1\subscriber\_protocol\streaming_pull_manager.py", line
496, in close
callback(self, reason)
File "C:\...\lib\site-packages\google\cloud\pubsub_v1\subscriber\futures.py", line 37, in _on_close_callback
self.set_result(True)
File "C:\...\lib\site-packages\google\cloud\pubsub_v1\futures.py", line 155, in set_result
raise RuntimeError("set_result can only be called once.")
RuntimeError: set_result can only be called once.
import os
from google.cloud import pubsub_v1
subscriber = pubsub_v1.SubscriberClient()
project_id=os.environ['GOOGLE_CLOUD_PROJECT']
subscription_name=os.environ['GOOGLE_CLOUD_PUBSUB_SUBSCRIPTION_NAME']
subscription_path = f'projects/{project_id}/subscriptions/{subscription_name}'
def worker_task( message ):
job_id = message.message_id
print(job_id)
a = 1/0 # cause an exception to be raised
message.ack()
def main():
streaming_pull_future = subscriber.subscribe(
subscription_path, callback=worker_task
)
print(f'Listening for messages on {subscription_path}')
try:
streaming_pull_future.result()
except Exception as e:
streaming_pull_future.cancel() # if exception in callback handler, this will raise a RunTimError
print(e)
if __name__ == '__main__':
main()
Thank you.

How to get a useful exception message from decimal in python 3?

With Python 2, creating a Decimal with an invalid string produces a useful error message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
"Invalid literal for Decimal: %r" % value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'spam'
While Python 3 produces a not-so-helpful message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?
I'm using Python 2.7.15 and Python 3.7.2, both on darwin.
Addenda:
It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009
This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227
You could monkey-patch the decimal module.
import decimal
def safe_decimal(something):
try:
funct_holder(something)
except Exception as e:
new_errror = Exception("Hey silly that's not a decimal, what should I do with this? {}".format(something))
raise new_errror from None
funct_holder = decimal.Decimal
decimal.Decimal = safe_decimal
Then you could use the monkey patched version as so
>>> decimal.Decimal('hello')
Traceback (most recent call last):
File "<input>", line 12, in <module>
File "<input>", line 6, in safe_decimal
Exception: Hey silly that's not a decimal, what should I do with this? hello

Python 3.X pycomm.ab_comm.clx CommError: must be str, not bytes

Im trying to get "PYCOMM" to connect to my CLX 5000 processor.
Every time I run my code I get:CommError: must be str, not bytes.
I have looked all over the code and I cant find where the issue is. Everything that is supposed to be in a string format is.
I am using python3.6
Here is the code:
import sys
from pycomm.ab_comm.clx import Driver as ClxDriver
c = ClxDriver()
if c.open('172.16.2.161'):
print(c.read_tag('Start'))
# Prints (1,'BOOL') if true; (0,'BOOL') if false
c.close()
Here is the error:
C:\Users\shirley\Miniconda3\python.exe C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py
Traceback (most recent call last):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 617, in build_header
h += pack_uint(length) # Length UINT
TypeError: must be str, not bytes
The header is 24 bytes fixed length, and includes the command and the length of the optional data portion.
:return: the headre
"""
try:
h = command # Command UINT
**h += pack_uint(length) # Length UINT**
h += pack_dint(self._session) # Session Handle UDINT
h += pack_dint(0) # Status UDINT
h += self.attribs['context'] # Sender Context 8 bytes
h += pack_dint(self.attribs['option']) # Option UDINT
return h
except Exception as e:
raise CommError(e)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 786, in open
if self.register_session() is None:
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 635, in register_session
self._message = self.build_header(ENCAPSULATION_COMMAND['register_session'], 4)
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 624, in build_header
raise CommError(e)
pycomm.cip.cip_base.CommError: must be str, not bytes
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/shirley/Downloads/pycomm-pycomm3/pycomm-pycomm3/examples/test_clx_comm.py", line 5, in
if c.open('172.16.2.161'):
File "C:\Users\shirley\Miniconda3\lib\site-packages\pycomm\cip\cip_base.py", line 793, in open
raise CommError(e)
pycomm.cip.cip_base.CommError: must be str, not bytes
Process finished with exit code 1
After banging my head against wall trying to figure this out on my own.
I just switched to using PYLOGIX
https://github.com/dmroeder/pylogix
It worked the first time I ran it
and its reasonably fast.
I had the same problem:
TypeError: must be str, not bytes.
The error is because you are using Python 3.x, and I did the same thing: I used Python 3.6 instead of Python 2.6 or 2.7.
Change Python 3.x to 2.6 or 2.7 (I used 2.7).
This was a bug that was fixed recently
I have it working with pycomm#1.0.8 and python#3.7.3

New line on error message in KeyError - Python 3.3

I am using Python 3.3 through the IDLE. While running a code that looks like:
raise KeyError('This is a \n Line break')
it outputs:
Traceback (most recent call last):
File "test.py", line 4, in <module>
raise KeyError('This is a \n Line break')
KeyError: 'This is a \n Line break'
I would like it to output the message with the line break like this:
This is a
Line Break
I have tried to convert it to a string before or using os.linesep but nothing seems to work. Is there any way I can force the message to be correctly shown on the IDLE?
If I raise an Exception (instead of KeyError) then the output is what I want, but I would like to still raise a KeyError if possible.
You problem has nothing to do with IDLE. The behavior you see is all from Python. Running current repository CPython interactively, from a command line, we see the behavior you reported.
Python 3.7.0a2+ (heads/pr_3947:01eae2f721, Oct 22 2017, 14:06:43)
[MSC v.1900 32 bit (Intel)] on win32
>>> raise KeyError('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'This is a \n Line break'
>>> s = 'This is a \n Line break'
>>> s
'This is a \n Line break'
>>> print(s)
This is a
Line break
>>> raise Exception('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: This is a
Line break
>>> raise IndexError(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: This is a
Line break
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e)
'This is a \n Line break'
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e.args[0])
This is a
Line break
I don't know why KeyError acts differently from even IndexError, but printing e.args[0] should work for all exceptions.
EDIT
The reason for the difference is given in this old tracker issue, which quotes a comment in the KeyError source code:
/* If args is a tuple of exactly one item, apply repr to args[0].
This is done so that e.g. the exception raised by {}[''] prints
KeyError: ''
rather than the confusing
KeyError
alone. The downside is that if KeyError is raised with an
explanatory
string, that string will be displayed in quotes. Too bad.
If args is anything else, use the default BaseException__str__().
*/
This section appears in the KeyError_str object definition in Objects/exceptions.c of the Python source code.
I will mention your issue as another manifestation of this difference.
There is a way to get the behavior you want: Simply subclass str and override __repr__:
class KeyErrorMessage(str):
def __repr__(self): return str(self)
msg = KeyErrorMessage('Newline\nin\nkey\nerror')
raise KeyError(msg)
Prints:
Traceback (most recent call last):
...
File "", line 5, in
raise KeyError(msg)
KeyError: Newline
in
key
error

Python http.client.Incomplete Read(0 bytes read) error

I have seen this error on the forum and read through the responses yet I still don't understand what it is or how to address it. I'm scraping data from the internet from 16k links, my script scrapes similar information from each link and writes it to a .csv some of the date gets written before this error.
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 541, in _get_chunk_left
chunk_left = self._read_next_chunk_size()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 508, in _read_next_chunk_size
return int(line, 16)
ValueError: invalid literal for int() with base 16: b''
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 558, in _readall_chunked
chunk_left = self._get_chunk_left()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 543, in _get_chunk_left
raise IncompleteRead(b'')
http.client.IncompleteRead: IncompleteRead(0 bytes read)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "MoviesToDb.py", line 91, in <module>
html = r.read()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 455, in read
return self._readall_chunked()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 565, in _readall_chunked
raise IncompleteRead(b''.join(value))
http.client.IncompleteRead: IncompleteRead(17891 bytes read)
I would like to know:1) What does this error mean? 2) How do I prevent it?
try to import :
from http.client import IncompleteRead
and add this in your script :
except IncompleteRead:
# Oh well, reconnect and keep trucking
continue
requests.exceptions.ChunkedEncodingError: (‘Connection broken: IncompleteRead(0 bytes read)’, IncompleteRead(0 bytes read)).
It is because the server of http protocal is 1.0 version,while python use 1.1 version. The solution is to assign the protocal version of client, like this
Python3 Version please add:
> import http.client
> http.client.HTTPConnection._http_vsn = 10
> http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
Python2 Version please add:
> import http.client
> http.client.HTTPConnection._http_vsn = 10
> http.client.HTTPConnection._http_vsn_str = 'HTTP/1.0'
See the reference How to deal with "http.client.IncompleteRead: IncompleteRead(0 bytes read)" problem

Resources