Skip to content

Commit

Permalink
Resolve compatibility issue with Cython 3 (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoPrevato authored Sep 19, 2023
1 parent bed3a99 commit d0a9f17
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 29 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

- name: Install dependencies
run: |
pip install cython==0.29.34
pip install cython==3.0.2
pip install -r requirements.txt
pip install flake8
Expand Down Expand Up @@ -176,7 +176,8 @@ jobs:

- name: Install build dependencies
run: |
pip install --upgrade cython build
pip install cython==3.0.2
pip install --upgrade build
- name: Compile Cython extensions
run: |
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0a11] - 2023-09-19 :warning:

- Resolves bug in `2.0a10` caused by incompatibility issue with `Cython 3`.
- Pins `Cython` to `3.0.2` in the build job.

## [2.0a10] - 2023-08-21 :broccoli:

- Add support for `.jinja` extension by @thearchitector.
Expand Down
2 changes: 1 addition & 1 deletion blacksheep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
used types to reduce the verbosity of the imports statements.
"""
__author__ = "Roberto Prevato <[email protected]>"
__version__ = "2.0a10"
__version__ = "2.0a11"

from .contents import Content as Content
from .contents import FormContent as FormContent
Expand Down
2 changes: 1 addition & 1 deletion blacksheep/messages.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ from .url cimport URL


cdef class Message:
cdef list __headers
cdef list _raw_headers
cdef public Content content
cdef object __weakref__

Expand Down
1 change: 0 additions & 1 deletion blacksheep/messages.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ class Response(Message):
headers: Optional[List[HeaderType]] = None,
content: Optional[Content] = None,
) -> None:
self.__headers = headers or []
self.status = status
self.content = content
def __repr__(self) -> str: ...
Expand Down
34 changes: 17 additions & 17 deletions blacksheep/messages.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ cpdef str parse_charset(bytes value):
cdef class Message:

def __init__(self, list headers):
self.__headers = headers or []
self._raw_headers = headers or []

@property
def headers(self):
cdef str key = '_headers'
if key in self.__dict__:
return self.__dict__[key]
self.__dict__[key] = Headers(self.__headers)
self.__dict__[key] = Headers(self._raw_headers)
return self.__dict__[key]

cpdef Message with_content(self, Content content):
Expand All @@ -46,15 +46,15 @@ cdef class Message:
cpdef bytes get_first_header(self, bytes key):
cdef tuple header
key = key.lower()
for header in self.__headers:
for header in self._raw_headers:
if header[0].lower() == key:
return header[1]

cpdef list get_headers(self, bytes key):
cdef list results = []
cdef tuple header
key = key.lower()
for header in self.__headers:
for header in self._raw_headers:
if header[0].lower() == key:
results.append(header[1])
return results
Expand All @@ -63,7 +63,7 @@ cdef class Message:
cdef list results = []
cdef tuple header
key = key.lower()
for header in self.__headers:
for header in self._raw_headers:
if header[0].lower() == key:
results.append(header)
return results
Expand All @@ -80,22 +80,22 @@ cdef class Message:
cdef tuple header
cdef list to_remove = []
key = key.lower()
for header in self.__headers:
for header in self._raw_headers:
if header[0].lower() == key:
to_remove.append(header)

for header in to_remove:
self.__headers.remove(header)
self._raw_headers.remove(header)

cdef void remove_headers(self, list headers):
cdef tuple header
for header in headers:
self.__headers.remove(header)
self._raw_headers.remove(header)

cdef bint _has_header(self, bytes key):
cdef bytes existing_key, existing_value
key = key.lower()
for existing_key, existing_value in self.__headers:
for existing_key, existing_value in self._raw_headers:
if existing_key.lower() == key:
return True
return False
Expand All @@ -104,18 +104,18 @@ cdef class Message:
return self._has_header(key)

cdef void _add_header(self, bytes key, bytes value):
self.__headers.append((key, value))
self._raw_headers.append((key, value))

cdef void _add_header_if_missing(self, bytes key, bytes value):
if not self._has_header(key):
self.__headers.append((key, value))
self._raw_headers.append((key, value))

cpdef void add_header(self, bytes key, bytes value):
self.__headers.append((key, value))
self._raw_headers.append((key, value))

cpdef void set_header(self, bytes key, bytes value):
self.remove_header(key)
self.__headers.append((key, value))
self._raw_headers.append((key, value))

cpdef bytes content_type(self):
if self.content and self.content.type:
Expand Down Expand Up @@ -267,7 +267,7 @@ cdef class Request(Message):
list headers
):
cdef URL _url = URL(url) if url else None
self.__headers = headers or []
self._raw_headers = headers or []
self.method = method
self._url = _url
self._session = None
Expand Down Expand Up @@ -473,7 +473,7 @@ cdef class Request(Message):
if existing_cookie:
self.set_header(b"cookie", existing_cookie + b";" + new_value)
else:
self.__headers.append((b"cookie", new_value))
self._raw_headers.append((b"cookie", new_value))

@property
def etag(self):
Expand All @@ -499,7 +499,7 @@ cdef class Response(Message):
list headers = None,
Content content = None
):
self.__headers = headers or []
self._raw_headers = headers or []
self.status = status
self.content = content

Expand Down Expand Up @@ -541,7 +541,7 @@ cdef class Response(Message):
return None

def set_cookie(self, Cookie cookie):
self.__headers.append((b'set-cookie', write_cookie_for_response(cookie)))
self._raw_headers.append((b'set-cookie', write_cookie_for_response(cookie)))

def set_cookies(self, list cookies):
cdef Cookie cookie
Expand Down
12 changes: 6 additions & 6 deletions blacksheep/scribe.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ cpdef bytes write_request_without_body(Request request):

ensure_host_header(request)

extend_data_with_headers(request.__headers, data)
extend_data_with_headers(request._raw_headers, data)
data.extend(b'\r\n')
return bytes(data)

Expand All @@ -172,7 +172,7 @@ cpdef bytes write_small_request(Request request):
ensure_host_header(request)
set_headers_for_content(request)

extend_data_with_headers(request.__headers, data)
extend_data_with_headers(request._raw_headers, data)
data.extend(b'\r\n')
if request.content:
data.extend(request.content.body)
Expand All @@ -183,7 +183,7 @@ cdef bytes write_small_response(Response response):
cdef bytearray data = bytearray()
data.extend(STATUS_LINES[response.status])
set_headers_for_content(response)
extend_data_with_headers(response.__headers, data)
extend_data_with_headers(response._raw_headers, data)
data.extend(b'\r\n')
if response.content:
data.extend(response.content.body)
Expand Down Expand Up @@ -236,7 +236,7 @@ async def write_request(Request request):
set_headers_for_content(request)

yield HTTP_METHODS[request.method] + b' ' + write_request_uri(request) + b' HTTP/1.1\r\n' + \
write_headers(request.__headers) + b'\r\n'
write_headers(request._raw_headers) + b'\r\n'

content = request.content

Expand Down Expand Up @@ -297,7 +297,7 @@ async def write_response(Response response):
set_headers_for_content(response)

yield STATUS_LINES[response.status] + \
write_headers(response.__headers) + b'\r\n'
write_headers(response._raw_headers) + b'\r\n'

async for chunk in write_response_content(response):
yield chunk
Expand All @@ -312,7 +312,7 @@ async def send_asgi_response(Response response, object send):
await send({
'type': 'http.response.start',
'status': response.status,
'headers': response.__headers
'headers': response._raw_headers
})

if content:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ charset-normalizer==3.1.0
click==8.1.3
coverage==6.5.0
cryptography==41.0.0
Cython==0.29.34; platform_system != "Windows"
Cython==3.0.2; platform_system != "Windows"
essentials==1.1.5
essentials-openapi==1.0.6
Flask==2.2.3
Expand Down

0 comments on commit d0a9f17

Please sign in to comment.