-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-21045][PYTHON] Allow non-ascii string as an exception message from python execution in Python 2 #25847
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
90559c0
fb72447
ff7f248
42a9eb0
0652966
ffb4d29
d6ec7ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # -*- encoding: utf-8 -*- | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
|
|
@@ -28,7 +29,7 @@ | |
|
|
||
| from py4j.protocol import Py4JJavaError | ||
|
|
||
| from pyspark.testing.utils import ReusedPySparkTestCase, PySparkTestCase, QuietTest | ||
| from pyspark.testing.utils import ExecThread, ReusedPySparkTestCase, PySparkTestCase, QuietTest | ||
|
|
||
| if sys.version_info[0] >= 3: | ||
| xrange = range | ||
|
|
@@ -150,6 +151,28 @@ def test_with_different_versions_of_python(self): | |
| finally: | ||
| self.sc.pythonVer = version | ||
|
|
||
| def test_python_exception_non_hanging(self): | ||
| """ | ||
| SPARK-21045: exceptions with no ascii encoding shall not hanging PySpark. | ||
|
advancedxy marked this conversation as resolved.
Outdated
|
||
| """ | ||
| def f(): | ||
| raise Exception("exception with 中 and \xd6\xd0") | ||
|
|
||
| def run(): | ||
| self.sc.parallelize([1]).map(lambda x: f()).count() | ||
|
advancedxy marked this conversation as resolved.
|
||
|
|
||
| t = ExecThread(target=run) | ||
| t.daemon = True | ||
| t.start() | ||
| t.join(10) | ||
| self.assertFalse(t.isAlive(), "Spark should not be blocked") | ||
| self.assertIsInstance(t.exception, Py4JJavaError) | ||
| if sys.version_info.major < 3: | ||
| # we have to use unicode here to avoid UnicodeDecodeError | ||
| self.assertRegexpMatches(unicode(t.exception).encode("utf-8"), "exception with 中") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||
| else: | ||
| self.assertRegexpMatches(str(t.exception), "exception with 中") | ||
|
|
||
|
|
||
| class WorkerReuseTest(PySparkTestCase): | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,7 +44,7 @@ | |
| from pyspark.util import _get_argspec, fail_on_stopiteration | ||
| from pyspark import shuffle | ||
|
|
||
| if sys.version >= '3': | ||
| if sys.version_info.major >= 3: | ||
|
advancedxy marked this conversation as resolved.
Outdated
advancedxy marked this conversation as resolved.
Outdated
|
||
| basestring = str | ||
| else: | ||
| from itertools import imap as map # use iterator map by default | ||
|
|
@@ -598,8 +598,18 @@ def process(): | |
| process() | ||
| except Exception: | ||
| try: | ||
| exc_info = traceback.format_exc() | ||
| if sys.version_info.major < 3: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise, let's drop this right after we drop Python 2, which I will do right after Spark 3. |
||
| if isinstance(exc_info, unicode): | ||
| exc_info = exc_info.encode("utf-8") | ||
| else: | ||
|
advancedxy marked this conversation as resolved.
Outdated
|
||
| # exc_info may contains other encoding bytes, replace the invalid byte and | ||
| # convert it back to utf-8 again | ||
| exc_info = exc_info.decode("utf-8", "replace").encode("utf-8") | ||
| else: | ||
| exc_info = exc_info.encode("utf-8") | ||
| write_int(SpecialLengths.PYTHON_EXCEPTION_THROWN, outfile) | ||
| write_with_length(traceback.format_exc().encode("utf-8"), outfile) | ||
| write_with_length(exc_info, outfile) | ||
| except IOError: | ||
| # JVM close the socket | ||
| pass | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.