Skip to content

Commit

Permalink
Replace deprecated Py_UNICODE APIs to support Python 3.12
Browse files Browse the repository at this point in the history
Fixes #18

This is the minimum to let it build on Python 3.12, but I am not sure if it's the most efficient way.
  • Loading branch information
anthrotype committed Sep 6, 2023
1 parent 57044dc commit 5cc69c2
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 162 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ jobs:
# Skip
#
# * Python 3.6 on all platforms,
# * Python 3.12 on all platforms, and
# * PyPy on Windows.
#
# TODO: Activate Python 3.12 when issue 18 is resolved.
CIBW_SKIP: cp36-* cp312-* pp*-win_amd64
CIBW_SKIP: cp36-* pp*-win_amd64
steps:
- uses: actions/checkout@v2
with:
Expand Down
51 changes: 25 additions & 26 deletions src/openstep_plist/_test.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,78 @@ from .parser cimport (
parse_plist_string as _parse_plist_string,
)
from .util cimport (
PY_NARROW_UNICODE,
tounicode,
is_valid_unquoted_string_char as _is_valid_unquoted_string_char,
)
from .writer cimport string_needs_quotes as _string_needs_quotes
from cpython.mem cimport PyMem_Free
from cpython.unicode cimport (
PyUnicode_FromUnicode, PyUnicode_AS_UNICODE, PyUnicode_GET_SIZE,
PyUnicode_AsUCS4Copy, PyUnicode_GET_LENGTH,
)


cdef class ParseContext:

cdef unicode s
cdef ParseInfo pi
cdef Py_UCS4 *buf
cdef object dict_type

@classmethod
def fromstring(
ParseContext cls,
string,
Py_ssize_t offset=0,
dict_type=dict,
bint use_numbers=False
def __cinit__(
self,
string,
Py_ssize_t offset=0,
dict_type=dict,
bint use_numbers=False
):
cdef ParseContext self = ParseContext.__new__(cls)
self.s = tounicode(string)
cdef Py_ssize_t length = PyUnicode_GET_SIZE(self.s)
cdef Py_UNICODE* buf = PyUnicode_AS_UNICODE(self.s)
cdef Py_ssize_t length = PyUnicode_GET_LENGTH(self.s)
self.buf = PyUnicode_AsUCS4Copy(self.s)
if not self.buf:
raise MemoryError()
self.dict_type = dict_type
self.pi = ParseInfo(
begin=buf,
curr=buf + offset,
end=buf + length,
begin=self.buf,
curr=self.buf + offset,
end=self.buf + length,
dict_type=<void*>dict_type,
use_numbers=use_numbers,
)
return self

def __dealloc__(self):
PyMem_Free(self.buf)

def is_narrow_unicode():
return PY_NARROW_UNICODE


def is_valid_unquoted_string_char(Py_UNICODE c):
def is_valid_unquoted_string_char(Py_UCS4 c):
return _is_valid_unquoted_string_char(c)


def line_number_strings(s, offset=0):
cdef ParseContext ctx = ParseContext.fromstring(s, offset)
cdef ParseContext ctx = ParseContext(s, offset)
return _line_number_strings(&ctx.pi)


def advance_to_non_space(s, offset=0):
cdef ParseContext ctx = ParseContext.fromstring(s, offset)
cdef ParseContext ctx = ParseContext(s, offset)
eof = not _advance_to_non_space(&ctx.pi)
return None if eof else s[ctx.pi.curr - ctx.pi.begin]


def get_slashed_char(s, offset=0):
cdef ParseContext ctx = ParseContext.fromstring(s, offset)
cdef ParseContext ctx = ParseContext(s, offset)
return _get_slashed_char(&ctx.pi)


def parse_unquoted_plist_string(s):
cdef ParseContext ctx = ParseContext.fromstring(s)
cdef ParseContext ctx = ParseContext(s)
return _parse_unquoted_plist_string(&ctx.pi)


def parse_plist_string(s, required=True):
cdef ParseContext ctx = ParseContext.fromstring(s)
cdef ParseContext ctx = ParseContext(s)
return _parse_plist_string(&ctx.pi, required=required)


def string_needs_quotes(s):
cdef ParseContext ctx = ParseContext.fromstring(s)
cdef ParseContext ctx = ParseContext(s)
return _string_needs_quotes(ctx.pi.begin, len(s))
12 changes: 6 additions & 6 deletions src/openstep_plist/parser.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ from libcpp.vector cimport vector


ctypedef struct ParseInfo:
const Py_UNICODE *begin
const Py_UNICODE *curr
const Py_UNICODE *end
const Py_UCS4 *begin
const Py_UCS4 *curr
const Py_UCS4 *end
void *dict_type
bint use_numbers

Expand All @@ -22,10 +22,10 @@ cdef uint32_t line_number_strings(ParseInfo *pi)
cdef bint advance_to_non_space(ParseInfo *pi)


cdef Py_UNICODE get_slashed_char(ParseInfo *pi)
cdef Py_UCS4 get_slashed_char(ParseInfo *pi)


cdef unicode parse_quoted_plist_string(ParseInfo *pi, Py_UNICODE quote)
cdef unicode parse_quoted_plist_string(ParseInfo *pi, Py_UCS4 quote)


cdef enum UnquotedType:
Expand All @@ -34,7 +34,7 @@ cdef enum UnquotedType:
UNQUOTED_FLOAT = 2


cdef UnquotedType get_unquoted_string_type(const Py_UNICODE *buf, Py_ssize_t length)
cdef UnquotedType get_unquoted_string_type(const Py_UCS4 *buf, Py_ssize_t length)


cdef object parse_unquoted_plist_string(ParseInfo *pi, bint ensure_string=*)
Expand Down
Loading

0 comments on commit 5cc69c2

Please sign in to comment.