66#include " duckdb/common/string_util.hpp"
77#include " duckdb_python/pybind11/pybind_wrapper.hpp"
88
9- namespace py = nanobind;
9+ namespace nb = nanobind;
1010
1111namespace duckdb {
1212
@@ -242,9 +242,9 @@ void PyThrowException(ErrorData &error, PyObject *http_exception) {
242242 case ExceptionType::HTTP : {
243243 // construct exception object
244244 auto exc_msg = error.Message ();
245- auto e = py ::handle (http_exception)(py ::str (exc_msg.c_str (), exc_msg.size ()));
245+ auto e = nb ::handle (http_exception)(nb ::str (exc_msg.c_str (), exc_msg.size ()));
246246
247- auto headers = py ::dict ();
247+ auto headers = nb ::dict ();
248248 for (auto &entry : error.ExtraInfo ()) {
249249 if (entry.first == " status_code" ) {
250250 e.attr (" status_code" ) = std::stoi (entry.second );
@@ -254,7 +254,7 @@ void PyThrowException(ErrorData &error, PyObject *http_exception) {
254254 e.attr (" reason" ) = entry.second ;
255255 } else if (StringUtil::StartsWith (entry.first , " header_" )) {
256256 auto header_name = entry.first .substr (7 );
257- headers[py ::str (header_name.c_str (), header_name.size ())] = entry.second ;
257+ headers[nb ::str (header_name.c_str (), header_name.size ())] = entry.second ;
258258 }
259259 }
260260 e.attr (" headers" ) = std::move (headers);
@@ -321,78 +321,78 @@ static void UnsetPythonException() {
321321/* *
322322 * @see https://peps.python.org/pep-0249/#exceptions
323323 */
324- void RegisterExceptions (const py ::module_ &m) {
324+ void RegisterExceptions (const nb ::module_ &m) {
325325 // The base class is mapped to Error in python to somewhat match the DBAPI 2.0 specifications
326- py::register_exception <Warning>(m, " Warning" );
327- auto error = py::register_exception <PyError>(m, " Error" ).ptr ();
328- auto db_error = py::register_exception <DatabaseError>(m, " DatabaseError" , error).ptr ();
326+ nb::exception <Warning>(m, " Warning" );
327+ auto error = nb::exception <PyError>(m, " Error" ).ptr ();
328+ auto db_error = nb::exception <DatabaseError>(m, " DatabaseError" , error).ptr ();
329329
330330 // order of declaration matters, and this needs to be checked last
331331 // Unknown
332- py::register_exception <PyFatalException>(m, " FatalException" , db_error);
333- py::register_exception <PyInterruptException>(m, " InterruptException" , db_error);
334- py::register_exception <PyPermissionException>(m, " PermissionException" , db_error);
335- py::register_exception <PySequenceException>(m, " SequenceException" , db_error);
336- py::register_exception <PyDependencyException>(m, " DependencyException" , db_error);
332+ nb::exception <PyFatalException>(m, " FatalException" , db_error);
333+ nb::exception <PyInterruptException>(m, " InterruptException" , db_error);
334+ nb::exception <PyPermissionException>(m, " PermissionException" , db_error);
335+ nb::exception <PySequenceException>(m, " SequenceException" , db_error);
336+ nb::exception <PyDependencyException>(m, " DependencyException" , db_error);
337337
338338 // DataError
339- auto data_error = py::register_exception <DataError>(m, " DataError" , db_error).ptr ();
340- py::register_exception <PyOutOfRangeException>(m, " OutOfRangeException" , data_error);
341- py::register_exception <PyConversionException>(m, " ConversionException" , data_error);
339+ auto data_error = nb::exception <DataError>(m, " DataError" , db_error).ptr ();
340+ nb::exception <PyOutOfRangeException>(m, " OutOfRangeException" , data_error);
341+ nb::exception <PyConversionException>(m, " ConversionException" , data_error);
342342 // no unknown type error, or decimal type
343- py::register_exception <PyTypeMismatchException>(m, " TypeMismatchException" , data_error);
343+ nb::exception <PyTypeMismatchException>(m, " TypeMismatchException" , data_error);
344344
345345 // OperationalError
346- auto operational_error = py::register_exception <OperationalError>(m, " OperationalError" , db_error).ptr ();
347- py::register_exception <PyTransactionException>(m, " TransactionException" , operational_error);
348- py::register_exception <PyOutOfMemoryException>(m, " OutOfMemoryException" , operational_error);
349- py::register_exception <PyConnectionException>(m, " ConnectionException" , operational_error);
346+ auto operational_error = nb::exception <OperationalError>(m, " OperationalError" , db_error).ptr ();
347+ nb::exception <PyTransactionException>(m, " TransactionException" , operational_error);
348+ nb::exception <PyOutOfMemoryException>(m, " OutOfMemoryException" , operational_error);
349+ nb::exception <PyConnectionException>(m, " ConnectionException" , operational_error);
350350 // no object size error
351351 // no null pointer errors
352- auto io_exception = py::register_exception <PyIOException>(m, " IOException" , operational_error).ptr ();
353- py::register_exception <PySerializationException>(m, " SerializationException" , operational_error);
352+ auto io_exception = nb::exception <PyIOException>(m, " IOException" , operational_error).ptr ();
353+ nb::exception <PySerializationException>(m, " SerializationException" , operational_error);
354354
355355 // Use a raw pointer to avoid destructor running after Python finalization.
356356 // The module holds a reference to the exception type, keeping it alive.
357357 static PyObject *HTTP_EXCEPTION = nullptr ;
358358 {
359- auto http_exc = py::register_exception <PyHTTPException>(m, " HTTPException" , io_exception);
359+ auto http_exc = nb::exception <PyHTTPException>(m, " HTTPException" , io_exception);
360360 HTTP_EXCEPTION = http_exc.ptr ();
361- const auto string_type = (py ::str (" " )).type ();
362- const auto Dict = py ::module_::import_ (" typing" ).attr (" Dict" );
363- // nanobind py ::dict has no kwargs constructor; build the annotations dict explicitly.
364- py ::dict annotations;
365- annotations[" status_code" ] = (py ::int_ (0 )).type ();
361+ const auto string_type = (nb ::str (" " )).type ();
362+ const auto Dict = nb ::module_::import_ (" typing" ).attr (" Dict" );
363+ // nanobind nb ::dict has no kwargs constructor; build the annotations dict explicitly.
364+ nb ::dict annotations;
365+ annotations[" status_code" ] = (nb ::int_ (0 )).type ();
366366 annotations[" body" ] = string_type;
367367 annotations[" reason" ] = string_type;
368- annotations[" headers" ] = Dict[py ::make_tuple (string_type, string_type)];
368+ annotations[" headers" ] = Dict[nb ::make_tuple (string_type, string_type)];
369369 http_exc.attr (" __annotations__" ) = annotations;
370370 http_exc.doc () = " Thrown when an error occurs in the httpfs extension, or whilst downloading an extension." ;
371371 }
372372
373373 // IntegrityError
374- auto integrity_error = py::register_exception <IntegrityError>(m, " IntegrityError" , db_error).ptr ();
375- py::register_exception <PyConstraintException>(m, " ConstraintException" , integrity_error);
374+ auto integrity_error = nb::exception <IntegrityError>(m, " IntegrityError" , db_error).ptr ();
375+ nb::exception <PyConstraintException>(m, " ConstraintException" , integrity_error);
376376
377377 // InternalError
378- auto internal_error = py::register_exception <InternalError>(m, " InternalError" , db_error).ptr ();
379- py::register_exception <PyInternalException>(m, " InternalException" , internal_error);
378+ auto internal_error = nb::exception <InternalError>(m, " InternalError" , db_error).ptr ();
379+ nb::exception <PyInternalException>(m, " InternalException" , internal_error);
380380
381381 // // ProgrammingError
382- auto programming_error = py::register_exception <ProgrammingError>(m, " ProgrammingError" , db_error).ptr ();
383- py::register_exception <PyParserException>(m, " ParserException" , programming_error);
384- py::register_exception <PySyntaxException>(m, " SyntaxException" , programming_error);
385- py::register_exception <PyBinderException>(m, " BinderException" , programming_error);
386- py::register_exception <PyInvalidInputException>(m, " InvalidInputException" , programming_error);
387- py::register_exception <PyInvalidTypeException>(m, " InvalidTypeException" , programming_error);
382+ auto programming_error = nb::exception <ProgrammingError>(m, " ProgrammingError" , db_error).ptr ();
383+ nb::exception <PyParserException>(m, " ParserException" , programming_error);
384+ nb::exception <PySyntaxException>(m, " SyntaxException" , programming_error);
385+ nb::exception <PyBinderException>(m, " BinderException" , programming_error);
386+ nb::exception <PyInvalidInputException>(m, " InvalidInputException" , programming_error);
387+ nb::exception <PyInvalidTypeException>(m, " InvalidTypeException" , programming_error);
388388 // no type for expression exceptions?
389- py::register_exception <PyCatalogException>(m, " CatalogException" , programming_error);
389+ nb::exception <PyCatalogException>(m, " CatalogException" , programming_error);
390390
391391 // NotSupportedError
392- auto not_supported_error = py::register_exception <NotSupportedError>(m, " NotSupportedError" , db_error).ptr ();
393- py::register_exception <PyNotImplementedException>(m, " NotImplementedException" , not_supported_error);
392+ auto not_supported_error = nb::exception <NotSupportedError>(m, " NotSupportedError" , db_error).ptr ();
393+ nb::exception <PyNotImplementedException>(m, " NotImplementedException" , not_supported_error);
394394
395- py ::register_exception_translator ([](const std::exception_ptr &p, void *) {
395+ nb ::register_exception_translator ([](const std::exception_ptr &p, void *) {
396396 try {
397397 if (p) {
398398 std::rethrow_exception (p);
@@ -401,7 +401,7 @@ void RegisterExceptions(const py::module_ &m) {
401401 duckdb::ErrorData error (ex);
402402 UnsetPythonException ();
403403 PyThrowException (error, HTTP_EXCEPTION );
404- } catch (const py ::builtin_exception &ex) {
404+ } catch (const nb ::builtin_exception &ex) {
405405 // These represent Python exceptions, we don't want to catch these
406406 throw ;
407407 } catch (const std::exception &ex) {
0 commit comments