Skip to content

Commit

Permalink
Small updates to enable Cython 3 (#1145)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmaarrfk authored Oct 28, 2023
1 parent 00cf38f commit ad05d90
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
12 changes: 6 additions & 6 deletions av/container/core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(<timeout_info*> p)
if info.timeout < 0: # timeout < 0 means no timeout
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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 = <Container>dereference(s).opaque
Expand Down
6 changes: 3 additions & 3 deletions av/container/pyio.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions av/container/pyio.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions av/logging.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <log_context*>ptr
return obj.name

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion av/video/format.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
cdef lib.AVPixelFormat get_pix_fmt(const char *name) except lib.AV_PIX_FMT_NONE
13 changes: 10 additions & 3 deletions docs/development/includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit ad05d90

Please sign in to comment.