Skip to content

Commit 38efbf7

Browse files
authored
GH-50431: [Python] Make FlightError a non-cdef class for abi3 wheel precursor (#50427)
### Rationale for this change Towards #50398 closes #50431 ### What changes are included in this PR? This is an agent generated PR that makes `FlightError` a non-cdef subclass of `Exception` as Cython does not support building with the Limited API and cdef classes that inherit from Python classes. It appears before a `cdef CStatus to_status` was defined on each subclass to return a `MakeFlightError` per flight error flavor. This was pull out to a dedicated `_flight_error_to_status` function instead. ### Are these changes tested? Yes (via existing tests?) ### Are there any user-facing changes? No * GitHub Issue: #50398 * GitHub Issue: #50431 Authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Signed-off-by: David Li <li.davidm96@gmail.com>
1 parent 32562bd commit 38efbf7

1 file changed

Lines changed: 52 additions & 60 deletions

File tree

python/pyarrow/_flight.pyx

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class CertKeyPair(_CertKeyPair):
181181
"""A TLS certificate and key for use in Flight."""
182182

183183

184-
cdef class FlightError(Exception):
184+
class FlightError(Exception):
185185
"""
186186
The base class for Flight-specific errors.
187187
@@ -201,73 +201,65 @@ cdef class FlightError(Exception):
201201
extra_info : bytes
202202
Extra binary error details that were provided by the
203203
server/will be sent to the client.
204-
"""
205-
206-
cdef dict __dict__
204+
"""
207205

208206
def __init__(self, message='', extra_info=b''):
209207
super().__init__(message)
210208
self.extra_info = tobytes(extra_info)
211209

212-
cdef CStatus to_status(self):
213-
message = tobytes(f"Flight error: {self}")
214-
return CStatus_UnknownError(message)
215210

216-
217-
cdef class FlightInternalError(FlightError, ArrowException):
211+
class FlightInternalError(FlightError, ArrowException):
218212
"""An error internal to the Flight server occurred."""
219213

220-
cdef CStatus to_status(self):
221-
return MakeFlightError(CFlightStatusInternal,
222-
tobytes(str(self)), self.extra_info)
223-
224214

225-
cdef class FlightTimedOutError(FlightError, ArrowException):
215+
class FlightTimedOutError(FlightError, ArrowException):
226216
"""The Flight RPC call timed out."""
227217

228-
cdef CStatus to_status(self):
229-
return MakeFlightError(CFlightStatusTimedOut,
230-
tobytes(str(self)), self.extra_info)
231-
232218

233-
cdef class FlightCancelledError(FlightError, ArrowCancelled):
219+
class FlightCancelledError(FlightError, ArrowCancelled):
234220
"""The operation was cancelled."""
235221

236-
cdef CStatus to_status(self):
237-
return MakeFlightError(CFlightStatusCancelled, tobytes(str(self)),
238-
self.extra_info)
239222

240-
241-
cdef class FlightServerError(FlightError, ArrowException):
223+
class FlightServerError(FlightError, ArrowException):
242224
"""A server error occurred."""
243225

244-
cdef CStatus to_status(self):
245-
return MakeFlightError(CFlightStatusFailed, tobytes(str(self)),
246-
self.extra_info)
247-
248226

249-
cdef class FlightUnauthenticatedError(FlightError, ArrowException):
227+
class FlightUnauthenticatedError(FlightError, ArrowException):
250228
"""The client is not authenticated."""
251229

252-
cdef CStatus to_status(self):
253-
return MakeFlightError(
254-
CFlightStatusUnauthenticated, tobytes(str(self)), self.extra_info)
255-
256230

257-
cdef class FlightUnauthorizedError(FlightError, ArrowException):
231+
class FlightUnauthorizedError(FlightError, ArrowException):
258232
"""The client is not authorized to perform the given operation."""
259233

260-
cdef CStatus to_status(self):
261-
return MakeFlightError(CFlightStatusUnauthorized, tobytes(str(self)),
262-
self.extra_info)
263234

264-
265-
cdef class FlightUnavailableError(FlightError, ArrowException):
235+
class FlightUnavailableError(FlightError, ArrowException):
266236
"""The server is not reachable or available."""
267237

268-
cdef CStatus to_status(self):
269-
return MakeFlightError(CFlightStatusUnavailable, tobytes(str(self)),
270-
self.extra_info)
238+
239+
cdef CStatus _flight_error_to_status(error) except *:
240+
extra_info = tobytes(getattr(error, "extra_info", b""))
241+
if isinstance(error, FlightInternalError):
242+
return MakeFlightError(CFlightStatusInternal, tobytes(str(error)),
243+
extra_info)
244+
elif isinstance(error, FlightTimedOutError):
245+
return MakeFlightError(CFlightStatusTimedOut, tobytes(str(error)),
246+
extra_info)
247+
elif isinstance(error, FlightCancelledError):
248+
return MakeFlightError(CFlightStatusCancelled, tobytes(str(error)),
249+
extra_info)
250+
elif isinstance(error, FlightServerError):
251+
return MakeFlightError(CFlightStatusFailed, tobytes(str(error)),
252+
extra_info)
253+
elif isinstance(error, FlightUnauthenticatedError):
254+
return MakeFlightError(CFlightStatusUnauthenticated, tobytes(str(error)),
255+
extra_info)
256+
elif isinstance(error, FlightUnauthorizedError):
257+
return MakeFlightError(CFlightStatusUnauthorized, tobytes(str(error)),
258+
extra_info)
259+
elif isinstance(error, FlightUnavailableError):
260+
return MakeFlightError(CFlightStatusUnavailable, tobytes(str(error)),
261+
extra_info)
262+
return CStatus_UnknownError(tobytes(f"Flight error: {error}"))
271263

272264

273265
class FlightWriteSizeExceededError(ArrowInvalid):
@@ -2140,7 +2132,7 @@ cdef CStatus _data_stream_next(void* self, CFlightPayload* payload) except *:
21402132
payload.ipc_message.metadata.reset(<CBuffer*> nullptr)
21412133
return CStatus_OK()
21422134
except FlightError as flight_error:
2143-
return (<FlightError> flight_error).to_status()
2135+
return _flight_error_to_status(flight_error)
21442136

21452137
if isinstance(result, (list, tuple)):
21462138
result, metadata = result
@@ -2214,7 +2206,7 @@ cdef CStatus _list_flights(void* self, const CServerCallContext& context,
22142206
flights.push_back(deref((<FlightInfo> info).info.get()))
22152207
listing.reset(new CSimpleFlightListing(flights))
22162208
except FlightError as flight_error:
2217-
return (<FlightError> flight_error).to_status()
2209+
return _flight_error_to_status(flight_error)
22182210
return CStatus_OK()
22192211

22202212

@@ -2231,7 +2223,7 @@ cdef CStatus _get_flight_info(void* self, const CServerCallContext& context,
22312223
ServerCallContext.wrap(context),
22322224
py_descriptor)
22332225
except FlightError as flight_error:
2234-
return (<FlightError> flight_error).to_status()
2226+
return _flight_error_to_status(flight_error)
22352227
if not isinstance(result, FlightInfo):
22362228
raise TypeError("FlightServerBase.get_flight_info must return "
22372229
f"a FlightInfo instance, but got {type(result)}")
@@ -2272,7 +2264,7 @@ cdef CStatus _do_put(void* self, const CServerCallContext& context,
22722264
py_reader, py_writer)
22732265
return CStatus_OK()
22742266
except FlightError as flight_error:
2275-
return (<FlightError> flight_error).to_status()
2267+
return _flight_error_to_status(flight_error)
22762268

22772269

22782270
cdef CStatus _do_get(void* self, const CServerCallContext& context,
@@ -2287,7 +2279,7 @@ cdef CStatus _do_get(void* self, const CServerCallContext& context,
22872279
result = (<object> self).do_get(ServerCallContext.wrap(context),
22882280
py_ticket)
22892281
except FlightError as flight_error:
2290-
return (<FlightError> flight_error).to_status()
2282+
return _flight_error_to_status(flight_error)
22912283
if not isinstance(result, FlightDataStream):
22922284
raise TypeError("FlightServerBase.do_get must return "
22932285
"a FlightDataStream")
@@ -2316,7 +2308,7 @@ cdef CStatus _do_exchange(void* self, const CServerCallContext& context,
23162308
descriptor, py_reader, py_writer)
23172309
return CStatus_OK()
23182310
except FlightError as flight_error:
2319-
return (<FlightError> flight_error).to_status()
2311+
return _flight_error_to_status(flight_error)
23202312

23212313

23222314
cdef CStatus _do_action_result_next(
@@ -2336,7 +2328,7 @@ cdef CStatus _do_action_result_next(
23362328
except StopIteration:
23372329
result.reset(nullptr)
23382330
except FlightError as flight_error:
2339-
return (<FlightError> flight_error).to_status()
2331+
return _flight_error_to_status(flight_error)
23402332
return CStatus_OK()
23412333

23422334

@@ -2351,7 +2343,7 @@ cdef CStatus _do_action(void* self, const CServerCallContext& context,
23512343
responses = (<object> self).do_action(ServerCallContext.wrap(context),
23522344
py_action)
23532345
except FlightError as flight_error:
2354-
return (<FlightError> flight_error).to_status()
2346+
return _flight_error_to_status(flight_error)
23552347
# Let the application return an iterator or anything convertible
23562348
# into one
23572349
if responses is None:
@@ -2377,7 +2369,7 @@ cdef CStatus _list_actions(void* self, const CServerCallContext& context,
23772369
action_type.description = tobytes(action[1])
23782370
actions.push_back(action_type)
23792371
except FlightError as flight_error:
2380-
return (<FlightError> flight_error).to_status()
2372+
return _flight_error_to_status(flight_error)
23812373
return CStatus_OK()
23822374

23832375

@@ -2389,7 +2381,7 @@ cdef CStatus _server_authenticate(void* self, CServerAuthSender* outgoing,
23892381
try:
23902382
(<object> self).authenticate(sender, reader)
23912383
except FlightError as flight_error:
2392-
return (<FlightError> flight_error).to_status()
2384+
return _flight_error_to_status(flight_error)
23932385
finally:
23942386
sender.poison()
23952387
reader.poison()
@@ -2403,7 +2395,7 @@ cdef CStatus _is_valid(void* self, const c_string& token,
24032395
c_result = tobytes((<object> self).is_valid(token))
24042396
peer_identity[0] = c_result
24052397
except FlightError as flight_error:
2406-
return (<FlightError> flight_error).to_status()
2398+
return _flight_error_to_status(flight_error)
24072399
return CStatus_OK()
24082400

24092401

@@ -2415,7 +2407,7 @@ cdef CStatus _client_authenticate(void* self, CClientAuthSender* outgoing,
24152407
try:
24162408
(<object> self).authenticate(sender, reader)
24172409
except FlightError as flight_error:
2418-
return (<FlightError> flight_error).to_status()
2410+
return _flight_error_to_status(flight_error)
24192411
finally:
24202412
sender.poison()
24212413
reader.poison()
@@ -2429,7 +2421,7 @@ cdef CStatus _get_token(void* self, c_string* token) except *:
24292421
c_result = tobytes((<object> self).get_token())
24302422
token[0] = c_result
24312423
except FlightError as flight_error:
2432-
return (<FlightError> flight_error).to_status()
2424+
return _flight_error_to_status(flight_error)
24332425
return CStatus_OK()
24342426

24352427

@@ -2439,7 +2431,7 @@ cdef CStatus _middleware_sending_headers(
24392431
try:
24402432
headers = (<object> self).sending_headers()
24412433
except FlightError as flight_error:
2442-
return (<FlightError> flight_error).to_status()
2434+
return _flight_error_to_status(flight_error)
24432435

24442436
if headers:
24452437
for header, values in headers.items():
@@ -2471,7 +2463,7 @@ cdef CStatus _middleware_call_completed(
24712463
else:
24722464
(<object> self).call_completed(None)
24732465
except FlightError as flight_error:
2474-
return (<FlightError> flight_error).to_status()
2466+
return _flight_error_to_status(flight_error)
24752467
return CStatus_OK()
24762468

24772469

@@ -2483,7 +2475,7 @@ cdef CStatus _middleware_received_headers(
24832475
headers = convert_headers(c_headers)
24842476
(<object> self).received_headers(headers)
24852477
except FlightError as flight_error:
2486-
return (<FlightError> flight_error).to_status()
2478+
return _flight_error_to_status(flight_error)
24872479
return CStatus_OK()
24882480

24892481

@@ -2516,7 +2508,7 @@ cdef CStatus _server_middleware_start_call(
25162508
headers = convert_headers(c_headers)
25172509
instance = (<object> self).start_call(call_info, headers)
25182510
except FlightError as flight_error:
2519-
return (<FlightError> flight_error).to_status()
2511+
return _flight_error_to_status(flight_error)
25202512

25212513
if instance:
25222514
ServerMiddleware.wrap(instance, c_instance)
@@ -2534,7 +2526,7 @@ cdef CStatus _client_middleware_start_call(
25342526
call_info = wrap_call_info(c_info)
25352527
instance = (<object> self).start_call(call_info)
25362528
except FlightError as flight_error:
2537-
return (<FlightError> flight_error).to_status()
2529+
return _flight_error_to_status(flight_error)
25382530

25392531
if instance:
25402532
ClientMiddleware.wrap(instance, c_instance)

0 commit comments

Comments
 (0)