diff -pruN 0.3.7-2/.github/workflows/disperse.yml 0.3.8-1/.github/workflows/disperse.yml
--- 0.3.7-2/.github/workflows/disperse.yml	2025-11-04 15:53:48.000000000 +0000
+++ 0.3.8-1/.github/workflows/disperse.yml	2025-12-07 04:18:00.000000000 +0000
@@ -10,5 +10,5 @@ jobs:
     runs-on: ubuntu-latest
 
     steps:
-      - uses: actions/checkout@v5
+      - uses: actions/checkout@v6
       - uses: jelmer/action-disperse-validate@v2
diff -pruN 0.3.7-2/.github/workflows/pythonpackage.yml 0.3.8-1/.github/workflows/pythonpackage.yml
--- 0.3.7-2/.github/workflows/pythonpackage.yml	2025-11-04 15:53:48.000000000 +0000
+++ 0.3.8-1/.github/workflows/pythonpackage.yml	2025-12-07 04:18:00.000000000 +0000
@@ -14,7 +14,7 @@ jobs:
       fail-fast: false
 
     steps:
-      - uses: actions/checkout@v5
+      - uses: actions/checkout@v6
       - name: Set up Python ${{ matrix.python-version }}
         uses: actions/setup-python@v6
         with:
diff -pruN 0.3.7-2/.github/workflows/wheels.yaml 0.3.8-1/.github/workflows/wheels.yaml
--- 0.3.7-2/.github/workflows/wheels.yaml	2025-11-04 15:53:48.000000000 +0000
+++ 0.3.8-1/.github/workflows/wheels.yaml	2025-12-07 04:18:00.000000000 +0000
@@ -1,10 +1,11 @@
+---
 name: Build Python distributions
 
-on:
+"on":
   push:
   pull_request:
   schedule:
-    - cron: "0 6 * * *" # Daily 6AM UTC build
+    - cron: "0 6 * * *"  # Daily 6AM UTC build
 
 jobs:
   build-wheels:
@@ -15,7 +16,7 @@ jobs:
       fail-fast: true
 
     steps:
-      - uses: actions/checkout@v5
+      - uses: actions/checkout@v6
       - uses: actions/setup-python@v6
       - name: Install dependencies
         run: |
@@ -35,7 +36,7 @@ jobs:
   build-sdist:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v5
+      - uses: actions/checkout@v6
       - uses: actions/setup-python@v6
       - name: Install dependencies
         run: |
@@ -62,7 +63,7 @@ jobs:
           # See https://github.com/pypa/twine/issues/1216
           pip install "twine>=6.1.0" "packaging>=24.2"
       - name: Download sdist
-        uses: actions/download-artifact@v5
+        uses: actions/download-artifact@v6
         with:
           name: artifact-sdist
           path: dist
@@ -84,7 +85,7 @@ jobs:
       url: https://pypi.org/p/fastbencode
     steps:
       - name: Download distributions
-        uses: actions/download-artifact@v5
+        uses: actions/download-artifact@v6
         with:
           merge-multiple: true
           pattern: artifact-*
diff -pruN 0.3.7-2/Cargo.lock 0.3.8-1/Cargo.lock
--- 0.3.7-2/Cargo.lock	2025-11-04 15:53:48.000000000 +0000
+++ 0.3.8-1/Cargo.lock	2025-12-07 04:18:00.000000000 +0000
@@ -10,7 +10,7 @@ checksum = "c08606f8c3cbf4ce6ec8e28fb001
 
 [[package]]
 name = "fastbencode"
-version = "0.3.7"
+version = "0.3.8"
 dependencies = [
  "pyo3",
 ]
@@ -32,9 +32,9 @@ dependencies = [
 
 [[package]]
 name = "libc"
-version = "0.2.177"
+version = "0.2.178"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
+checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
 
 [[package]]
 name = "memoffset"
@@ -68,9 +68,9 @@ dependencies = [
 
 [[package]]
 name = "pyo3"
-version = "0.26.0"
+version = "0.27.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7ba0117f4212101ee6544044dae45abe1083d30ce7b29c4b5cbdfa2354e07383"
+checksum = "ab53c047fcd1a1d2a8820fe84f05d6be69e9526be40cb03b73f86b6b03e6d87d"
 dependencies = [
  "indoc",
  "libc",
@@ -85,18 +85,18 @@ dependencies = [
 
 [[package]]
 name = "pyo3-build-config"
-version = "0.26.0"
+version = "0.27.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4fc6ddaf24947d12a9aa31ac65431fb1b851b8f4365426e182901eabfb87df5f"
+checksum = "b455933107de8642b4487ed26d912c2d899dec6114884214a0b3bb3be9261ea6"
 dependencies = [
  "target-lexicon",
 ]
 
 [[package]]
 name = "pyo3-ffi"
-version = "0.26.0"
+version = "0.27.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "025474d3928738efb38ac36d4744a74a400c901c7596199e20e45d98eb194105"
+checksum = "1c85c9cbfaddf651b1221594209aed57e9e5cff63c4d11d1feead529b872a089"
 dependencies = [
  "libc",
  "pyo3-build-config",
@@ -104,9 +104,9 @@ dependencies = [
 
 [[package]]
 name = "pyo3-macros"
-version = "0.26.0"
+version = "0.27.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2e64eb489f22fe1c95911b77c44cc41e7c19f3082fc81cce90f657cdc42ffded"
+checksum = "0a5b10c9bf9888125d917fb4d2ca2d25c8df94c7ab5a52e13313a07e050a3b02"
 dependencies = [
  "proc-macro2",
  "pyo3-macros-backend",
@@ -116,9 +116,9 @@ dependencies = [
 
 [[package]]
 name = "pyo3-macros-backend"
-version = "0.26.0"
+version = "0.27.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "100246c0ecf400b475341b8455a9213344569af29a3c841d29270e53102e0fcf"
+checksum = "03b51720d314836e53327f5871d4c0cfb4fb37cc2c4a11cc71907a86342c40f9"
 dependencies = [
  "heck",
  "proc-macro2",
@@ -129,9 +129,9 @@ dependencies = [
 
 [[package]]
 name = "quote"
-version = "1.0.41"
+version = "1.0.42"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1"
+checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
 dependencies = [
  "proc-macro2",
 ]
@@ -144,9 +144,9 @@ checksum = "b39cdef0fa800fc44525c84ccb54
 
 [[package]]
 name = "syn"
-version = "2.0.108"
+version = "2.0.111"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917"
+checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
 dependencies = [
  "proc-macro2",
  "quote",
diff -pruN 0.3.7-2/Cargo.toml 0.3.8-1/Cargo.toml
--- 0.3.7-2/Cargo.toml	2025-11-04 15:53:48.000000000 +0000
+++ 0.3.8-1/Cargo.toml	2025-12-07 04:18:00.000000000 +0000
@@ -1,6 +1,6 @@
 [package]
 name = "fastbencode"
-version = "0.3.7"
+version = "0.3.8"
 edition = "2021"
 authors = ["Jelmer Vernooĳ <jelmer@jelmer.uk>"]
 license = "Apache-2.0"
@@ -14,4 +14,4 @@ name = "fastbencode__bencode_rs"
 crate-type = ["cdylib"]
 
 [dependencies]
-pyo3 = { version = ">=0.25,<0.27", features = ["extension-module"] }
+pyo3 = { version = "0.27", features = ["extension-module"] }
diff -pruN 0.3.7-2/debian/changelog 0.3.8-1/debian/changelog
--- 0.3.7-2/debian/changelog	2025-11-11 02:13:39.000000000 +0000
+++ 0.3.8-1/debian/changelog	2025-12-07 04:20:21.000000000 +0000
@@ -1,3 +1,10 @@
+python-fastbencode (0.3.8-1) unstable; urgency=medium
+
+  * New upstream release.
+   + Fix compatibility with newer PyO3 0.27. Closes: #1122000
+
+ -- Jelmer Vernooĳ <jelmer@debian.org>  Sun, 07 Dec 2025 04:20:21 +0000
+
 python-fastbencode (0.3.7-2) unstable; urgency=medium
 
   * Add missing build-dependency on python3-setuptools-rust. Closes: #1120252
diff -pruN 0.3.7-2/debian/control 0.3.8-1/debian/control
--- 0.3.7-2/debian/control	2025-11-11 02:13:32.000000000 +0000
+++ 0.3.8-1/debian/control	2025-12-07 04:19:38.000000000 +0000
@@ -4,10 +4,8 @@ Build-Depends: debhelper-compat (= 13),
                dh-sequence-python3,
                cargo:native,
                cython3,
-               librust-pyo3+default-dev (<< 0.27-~~),
-               librust-pyo3+default-dev (>= 0.25-~~),
-               librust-pyo3+extension-module-dev (<< 0.27-~~),
-               librust-pyo3+extension-module-dev (>= 0.25-~~),
+               librust-pyo3-0.27+default-dev,
+               librust-pyo3-0.27+extension-module-dev,
                libstd-rust-dev,
                python3-all-dev,
                python3-setuptools,
diff -pruN 0.3.7-2/pyproject.toml 0.3.8-1/pyproject.toml
--- 0.3.7-2/pyproject.toml	2025-11-04 15:53:48.000000000 +0000
+++ 0.3.8-1/pyproject.toml	2025-12-07 04:18:00.000000000 +0000
@@ -33,7 +33,7 @@ GitHub = "https://github.com/breezy-team
 [project.optional-dependencies]
 rust = ["setuptools-rust>=1.0.0"]
 dev = [
-    "ruff==0.14.3"
+    "ruff==0.14.6"
 ]
 
 [tool.setuptools]
diff -pruN 0.3.7-2/src/lib.rs 0.3.8-1/src/lib.rs
--- 0.3.7-2/src/lib.rs	2025-11-04 15:53:48.000000000 +0000
+++ 0.3.8-1/src/lib.rs	2025-12-07 04:18:00.000000000 +0000
@@ -177,7 +177,12 @@ impl Decoder {
 
         // Return as bytes or decode depending on bytestring_encoding
         if let Some(encoding) = &self.bytestring_encoding {
-            Ok(PyString::from_object(&bytes_obj, encoding, "strict")?.into_any())
+            let encoding_cstr = std::ffi::CString::new(encoding.as_str())
+                .map_err(|_| PyValueError::new_err("invalid encoding string"))?;
+            Ok(
+                PyString::from_encoded_object(&bytes_obj, Some(&encoding_cstr), Some(c"strict"))?
+                    .into_any(),
+            )
         } else {
             Ok(bytes_obj)
         }
@@ -367,7 +372,7 @@ impl Encoder {
         let mut keys: Vec<Bound<PyBytes>> = dict
             .keys()
             .iter()
-            .map(|key| key.extract::<Bound<PyBytes>>())
+            .map(|key| key.extract::<Bound<PyBytes>>().map_err(|e| e.into()))
             .collect::<PyResult<Vec<_>>>()?;
         keys.sort_by(|a, b| {
             let a_str = a.extract::<&[u8]>().unwrap();
