From ad05d90c255bda3f56bbc2a5147c941103acb94a Mon Sep 17 00:00:00 2001 From: Mark Harfouche Date: Sat, 28 Oct 2023 13:26:21 -0400 Subject: [PATCH] Small updates to enable Cython 3 (#1145) --- av/container/core.pyx | 12 ++++++------ av/container/pyio.pxd | 6 +++--- av/container/pyio.pyx | 12 ++++++------ av/logging.pyx | 4 ++-- av/video/format.pxd | 2 +- docs/development/includes.py | 13 ++++++++++--- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/av/container/core.pyx b/av/container/core.pyx index d21893c43..493576f16 100755 --- a/av/container/core.pyx +++ b/av/container/core.pyx @@ -20,7 +20,7 @@ from av.dictionary import Dictionary from av.logging import Capture as LogCapture -ctypedef int64_t (*seek_func_t)(void *opaque, int64_t offset, int whence) nogil +ctypedef int64_t (*seek_func_t)(void *opaque, int64_t offset, int whence) noexcept nogil cdef object _cinit_sentinel = object() @@ -29,7 +29,7 @@ cdef object _cinit_sentinel = object() # We want to use the monotonic clock if it is available. cdef object clock = getattr(time, 'monotonic', time.time) -cdef int interrupt_cb (void *p) nogil: +cdef int interrupt_cb (void *p) noexcept nogil: cdef timeout_info info = dereference( p) if info.timeout < 0: # timeout < 0 means no timeout @@ -56,7 +56,7 @@ cdef int pyav_io_open(lib.AVFormatContext *s, lib.AVIOContext **pb, const char *url, int flags, - lib.AVDictionary **options) nogil: + lib.AVDictionary **options) noexcept nogil: with gil: return pyav_io_open_gil(s, pb, url, flags, options) @@ -65,7 +65,7 @@ cdef int pyav_io_open_gil(lib.AVFormatContext *s, lib.AVIOContext **pb, const char *url, int flags, - lib.AVDictionary **options): + lib.AVDictionary **options) noexcept: cdef Container container cdef object file cdef PyIOFile pyio_file @@ -104,13 +104,13 @@ cdef int pyav_io_open_gil(lib.AVFormatContext *s, cdef void pyav_io_close(lib.AVFormatContext *s, - lib.AVIOContext *pb) nogil: + lib.AVIOContext *pb) noexcept nogil: with gil: pyav_io_close_gil(s, pb) cdef void pyav_io_close_gil(lib.AVFormatContext *s, - lib.AVIOContext *pb): + lib.AVIOContext *pb) noexcept: cdef Container container try: container = dereference(s).opaque diff --git a/av/container/pyio.pxd b/av/container/pyio.pxd index b2a593b14..0faeea4f1 100644 --- a/av/container/pyio.pxd +++ b/av/container/pyio.pxd @@ -2,11 +2,11 @@ from libc.stdint cimport int64_t, uint8_t cimport libav as lib -cdef int pyio_read(void *opaque, uint8_t *buf, int buf_size) nogil +cdef int pyio_read(void *opaque, uint8_t *buf, int buf_size) noexcept nogil -cdef int pyio_write(void *opaque, uint8_t *buf, int buf_size) nogil +cdef int pyio_write(void *opaque, uint8_t *buf, int buf_size) noexcept nogil -cdef int64_t pyio_seek(void *opaque, int64_t offset, int whence) nogil +cdef int64_t pyio_seek(void *opaque, int64_t offset, int whence) noexcept nogil cdef void pyio_close_gil(lib.AVIOContext *pb) diff --git a/av/container/pyio.pyx b/av/container/pyio.pyx index 17d977f3e..07224cd91 100644 --- a/av/container/pyio.pyx +++ b/av/container/pyio.pyx @@ -4,7 +4,7 @@ cimport libav as lib from av.error cimport stash_exception -ctypedef int64_t (*seek_func_t)(void *opaque, int64_t offset, int whence) nogil +ctypedef int64_t (*seek_func_t)(void *opaque, int64_t offset, int whence) noexcept nogil cdef class PyIOFile(object): @@ -76,11 +76,11 @@ cdef class PyIOFile(object): lib.av_freep(&self.buffer) -cdef int pyio_read(void *opaque, uint8_t *buf, int buf_size) nogil: +cdef int pyio_read(void *opaque, uint8_t *buf, int buf_size) noexcept nogil: with gil: return pyio_read_gil(opaque, buf, buf_size) -cdef int pyio_read_gil(void *opaque, uint8_t *buf, int buf_size): +cdef int pyio_read_gil(void *opaque, uint8_t *buf, int buf_size) noexcept: cdef PyIOFile self cdef bytes res try: @@ -95,11 +95,11 @@ cdef int pyio_read_gil(void *opaque, uint8_t *buf, int buf_size): return stash_exception() -cdef int pyio_write(void *opaque, uint8_t *buf, int buf_size) nogil: +cdef int pyio_write(void *opaque, uint8_t *buf, int buf_size) noexcept nogil: with gil: return pyio_write_gil(opaque, buf, buf_size) -cdef int pyio_write_gil(void *opaque, uint8_t *buf, int buf_size): +cdef int pyio_write_gil(void *opaque, uint8_t *buf, int buf_size) noexcept: cdef PyIOFile self cdef bytes bytes_to_write cdef int bytes_written @@ -114,7 +114,7 @@ cdef int pyio_write_gil(void *opaque, uint8_t *buf, int buf_size): return stash_exception() -cdef int64_t pyio_seek(void *opaque, int64_t offset, int whence) nogil: +cdef int64_t pyio_seek(void *opaque, int64_t offset, int whence) noexcept nogil: # Seek takes the standard flags, but also a ad-hoc one which means that # the library wants to know how large the file is. We are generally # allowed to ignore this. diff --git a/av/logging.pyx b/av/logging.pyx index 1bdb7fab7..2253560ad 100644 --- a/av/logging.pyx +++ b/av/logging.pyx @@ -208,7 +208,7 @@ cdef struct log_context: lib.AVClass *class_ const char *name -cdef const char *log_context_name(void *ptr) nogil: +cdef const char *log_context_name(void *ptr) noexcept nogil: cdef log_context *obj = ptr return obj.name @@ -229,7 +229,7 @@ cpdef log(int level, str name, str message): free(obj) -cdef void log_callback(void *ptr, int level, const char *format, lib.va_list args) nogil: +cdef void log_callback(void *ptr, int level, const char *format, lib.va_list args) noexcept nogil: cdef bint inited = lib.Py_IsInitialized() if not inited and not print_after_shutdown: diff --git a/av/video/format.pxd b/av/video/format.pxd index 372821666..923f05c44 100644 --- a/av/video/format.pxd +++ b/av/video/format.pxd @@ -24,4 +24,4 @@ cdef class VideoFormatComponent(object): cdef VideoFormat get_video_format(lib.AVPixelFormat c_format, unsigned int width, unsigned int height) -cdef lib.AVPixelFormat get_pix_fmt(const char *name) except lib.AV_PIX_FMT_NONE \ No newline at end of file +cdef lib.AVPixelFormat get_pix_fmt(const char *name) except lib.AV_PIX_FMT_NONE diff --git a/docs/development/includes.py b/docs/development/includes.py index b60629fda..8f350a81e 100644 --- a/docs/development/includes.py +++ b/docs/development/includes.py @@ -5,7 +5,7 @@ import xml.etree.ElementTree as etree -from Cython.Compiler.Main import compile_single, CompilationOptions +from Cython.Compiler.Main import CompilationOptions, Context from Cython.Compiler.TreeFragment import parse_from_strings from Cython.Compiler.Visitor import TreeVisitor from Cython.Compiler import Nodes @@ -107,9 +107,16 @@ def extract(path, **kwargs): c_string_encoding='ascii', ) - context = options.create_context() + context = Context( + options.include_path, + options.compiler_directives, + options.cplus, + options.language_level, + options=options, + ) - tree = parse_from_strings(name, open(path).read(), context, + tree = parse_from_strings( + name, open(path).read(), context, level='module_pxd' if path.endswith('.pxd') else None, **kwargs)