Skip to content

Commit 36e7d5d

Browse files
committed
_utils.py: port to pure-python syntax
Fix lint and formatting errors in the process
1 parent fe685c5 commit 36e7d5d

File tree

4 files changed

+14
-16
lines changed

4 files changed

+14
-16
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ requires = [
44
"setuptools>=36.6.0",
55
# In order to build wheels, and as required by PEP 517
66
"wheel",
7-
"Cython"
7+
"Cython>=3a"
88
]
99
build-backend = "setuptools.build_meta"
1010

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ warn_no_return = True
2828

2929
# Ignore missing stubs for third-party packages.
3030
# In future, these should be re-enabled if/when stubs for them become available.
31-
[mypy-copyreg,grpc,pluginbase,psutil,pyroaring,ruamel,multiprocessing.forkserver]
31+
[mypy-copyreg,grpc,pluginbase,psutil,pyroaring,ruamel,multiprocessing.forkserver,cython.*]
3232
ignore_missing_imports=True
3333

3434
# Ignore issues with generated files and vendored code

src/buildstream/_utils.pyx renamed to src/buildstream/_utils.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
This module contains utilities that have been optimized in Cython
2323
"""
2424

25-
from cpython.pystate cimport PyThreadState_SetAsyncExc
26-
from cpython.ref cimport PyObject
25+
import cython # pylint: disable=import-error
26+
from cython.cimports.cpython.pystate import PyThreadState_SetAsyncExc # pylint: disable=import-error
27+
from cython.cimports.cpython.ref import PyObject # pylint: disable=import-error
2728
from ._signals import TerminateException
2829

2930

30-
def url_directory_name(str url):
31+
def url_directory_name(url: str):
3132
"""Normalizes a url into a directory name
3233
3334
Args:
@@ -36,8 +37,7 @@ def url_directory_name(str url):
3637
Returns:
3738
A string which can be used as a directory name
3839
"""
39-
return ''.join([_transl(x) for x in url])
40-
40+
return "".join([_transl(x) for x in url])
4141

4242

4343
# terminate_thread()
@@ -47,8 +47,8 @@ def url_directory_name(str url):
4747
# Args:
4848
# thread_id (int): the thread id in which to throw the exception
4949
#
50-
def terminate_thread(long thread_id):
51-
res = PyThreadState_SetAsyncExc(thread_id, <PyObject*> TerminateException)
50+
def terminate_thread(thread_id: cython.long):
51+
res = PyThreadState_SetAsyncExc(thread_id, cython.cast(cython.pointer(PyObject), TerminateException))
5252
assert res == 1
5353

5454

@@ -60,9 +60,9 @@ def terminate_thread(long thread_id):
6060
# Returns:
6161
# (bool): True if all characters are valid, False otherwise.
6262
#
63-
def valid_chars_name(str name):
64-
cdef int char_value
65-
cdef int forbidden_char
63+
def valid_chars_name(name: str):
64+
char_value: cython.int
65+
forbidden_char: cython.int
6666

6767
for char_value in name:
6868
# 0-31 are control chars, 127 is DEL, and >127 means non-ASCII
@@ -96,7 +96,8 @@ def valid_chars_name(str name):
9696
#
9797
# This transforms the value to "_" if is it not a ascii letter, a digit or "%" or "_"
9898
#
99-
cdef Py_UNICODE _transl(Py_UNICODE x):
99+
@cython.cfunc
100+
def _transl(x: cython.Py_UNICODE) -> cython.Py_UNICODE:
100101
if ("a" <= x <= "z") or ("A" <= x <= "Z") or ("0" <= x <= "9") or x == "%":
101102
return x
102103
return "_"

src/buildstream/_utils.pyi

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)