Skip to content

Commit 274b0b2

Browse files
Fix path join on Windows (#314)
* Switch to GitHub Actions * Support Python 3.12
1 parent 40e3f2a commit 274b0b2

File tree

9 files changed

+81
-45
lines changed

9 files changed

+81
-45
lines changed

.github/workflows/ci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: build
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
strategy:
6+
fail-fast: false
7+
matrix:
8+
os: [macos-latest, windows-latest, ubuntu-latest]
9+
language:
10+
[
11+
{python-version: "3.8", toxenv: "py38"},
12+
{python-version: "3.9", toxenv: "py39"},
13+
{python-version: "3.10", toxenv: "py310"},
14+
{python-version: "3.11", toxenv: "py311"},
15+
{python-version: "3.12", toxenv: "py312"},
16+
{python-version: "pypy3.8", toxenv: "pypy38"},
17+
]
18+
include:
19+
- os: ubuntu-latest
20+
language: {python-version: "3.8", toxenv: "codestyle"}
21+
- os: ubuntu-latest
22+
language: {python-version: "3.8", toxenv: "lint"}
23+
- os: ubuntu-latest
24+
language: {python-version: "3.8", toxenv: "typecheck"}
25+
runs-on: ${{ matrix.os }}
26+
steps:
27+
- name: Check out repository
28+
uses: actions/checkout@v4
29+
- name: Setup Python
30+
uses: actions/setup-python@v4
31+
with:
32+
python-version: ${{ matrix.language.python-version }}
33+
- name: Install Python requirements
34+
run: |
35+
pip install --upgrade pip
36+
pip install --upgrade --editable '.[testing]'
37+
- name: Test
38+
run: tox
39+
env:
40+
TOXENV: ${{ matrix.language.toxenv }}

.travis.yml

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# tldextract [![PyPI version](https://badge.fury.io/py/tldextract.svg)](https://badge.fury.io/py/tldextract) [![Build Status](https://travis-ci.com/john-kurkowski/tldextract.svg?branch=master)](https://app.travis-ci.com/github/john-kurkowski/tldextract)
1+
# tldextract [![PyPI version](https://badge.fury.io/py/tldextract.svg)](https://badge.fury.io/py/tldextract) [![Build Status](https://github.com/john-kurkowski/tldextract/actions/workflows/ci.yml/badge.svg)](https://github.com/john-kurkowski/tldextract/actions/workflows/ci.yml)
22

33
`tldextract` accurately separates a URL's subdomain, domain, and public suffix,
44
using [the Public Suffix List (PSL)](https://publicsuffix.org).

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ classifiers = [
2727
"Programming Language :: Python :: 3.9",
2828
"Programming Language :: Python :: 3.10",
2929
"Programming Language :: Python :: 3.11",
30+
"Programming Language :: Python :: 3.12",
3031
]
3132
requires-python = ">=3.8"
3233
dynamic = ["version"]

tests/custom_suffix_test.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import os
44
import tempfile
5+
from pathlib import Path
56

67
import tldextract
78
from tldextract.tldextract import ExtractResult
89

9-
FAKE_SUFFIX_LIST_URL = "file://" + os.path.join(
10-
os.path.dirname(os.path.abspath(__file__)), "fixtures/fake_suffix_list_fixture.dat"
11-
)
10+
FAKE_SUFFIX_LIST_URL = Path(
11+
os.path.dirname(os.path.abspath(__file__)),
12+
"fixtures",
13+
"fake_suffix_list_fixture.dat",
14+
).as_uri()
15+
1216
EXTRA_SUFFIXES = ["foo1", "bar1", "baz1"]
1317

1418
extract_using_fake_suffix_list = tldextract.TLDExtract(

tests/test_cache.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Test the caching functionality."""
22
from __future__ import annotations
33

4-
import os.path
54
import sys
65
import types
76
from collections.abc import Hashable
@@ -56,23 +55,23 @@ def test_get_cache_dir(monkeypatch: pytest.MonkeyPatch) -> None:
5655
monkeypatch.delenv("HOME", raising=False)
5756
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
5857
monkeypatch.delenv("TLDEXTRACT_CACHE", raising=False)
59-
assert get_cache_dir().endswith("tldextract/.suffix_cache/")
58+
assert get_cache_dir().endswith(str(Path("tldextract", ".suffix_cache")))
6059

6160
# with home set, but not anything else specified, use XDG_CACHE_HOME default
6261
monkeypatch.setenv("HOME", "/home/john")
6362
monkeypatch.delenv("XDG_CACHE_HOME", raising=False)
6463
monkeypatch.delenv("TLDEXTRACT_CACHE", raising=False)
65-
assert get_cache_dir() == os.path.join(
66-
"/home/john", ".cache/python-tldextract", pkg_identifier
64+
assert get_cache_dir() == str(
65+
Path("/home/john", ".cache/python-tldextract", pkg_identifier)
6766
)
6867

6968
# if XDG_CACHE_HOME is set, use it
7069
monkeypatch.setenv("HOME", "/home/john")
7170
monkeypatch.setenv("XDG_CACHE_HOME", "/my/alt/cache")
7271
monkeypatch.delenv("TLDEXTRACT_CACHE", raising=False)
7372

74-
assert get_cache_dir() == os.path.join(
75-
"/my/alt/cache/python-tldextract", pkg_identifier
73+
assert get_cache_dir() == str(
74+
Path("/my/alt/cache/python-tldextract", pkg_identifier)
7675
)
7776

7877
# if TLDEXTRACT_CACHE is set, use it

tests/test_parallel.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Test ability to run in parallel with shared cache."""
22

3+
from __future__ import annotations
4+
35
import os
4-
import os.path
56
from multiprocessing import Pool
67
from pathlib import Path
78

@@ -43,9 +44,23 @@ def test_cache_cleared_by_other_process(
4344
extract("google.com")
4445
orig_unlink = os.unlink
4546

46-
def evil_unlink(filename: str) -> None:
47+
def is_relative_to(path: Path, other_path: str | Path) -> bool:
48+
"""Return True if path is relative to other_path or False.
49+
50+
Taken from the Python 3.9 standard library.
51+
Reference: https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.is_relative_to
52+
"""
53+
try:
54+
path.relative_to(other_path)
55+
return True
56+
except ValueError:
57+
return False
58+
59+
def evil_unlink(filename: str | Path) -> None:
4760
"""Simulate someone deletes the file right before we try to."""
48-
if filename.startswith(cache_dir):
61+
if (isinstance(filename, str) and filename.startswith(cache_dir)) or (
62+
isinstance(filename, Path) and is_relative_to(filename, cache_dir)
63+
):
4964
orig_unlink(filename)
5065
orig_unlink(filename)
5166

tldextract/cache.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import json
77
import logging
88
import os
9-
import os.path
109
import sys
1110
from collections.abc import Callable, Hashable, Iterable
11+
from pathlib import Path
1212
from typing import (
1313
TypeVar,
1414
cast,
@@ -79,15 +79,15 @@ def get_cache_dir() -> str:
7979
if xdg_cache_home is None:
8080
user_home = os.getenv("HOME", None)
8181
if user_home:
82-
xdg_cache_home = os.path.join(user_home, ".cache")
82+
xdg_cache_home = str(Path(user_home, ".cache"))
8383

8484
if xdg_cache_home is not None:
85-
return os.path.join(
86-
xdg_cache_home, "python-tldextract", get_pkg_unique_identifier()
85+
return str(
86+
Path(xdg_cache_home, "python-tldextract", get_pkg_unique_identifier())
8787
)
8888

8989
# fallback to trying to use package directory itself
90-
return os.path.join(os.path.dirname(__file__), ".suffix_cache/")
90+
return str(Path(os.path.dirname(__file__), ".suffix_cache"))
9191

9292

9393
class DiskCache:
@@ -153,7 +153,7 @@ def clear(self) -> None:
153153
self.file_ext + ".lock"
154154
):
155155
try:
156-
os.unlink(os.path.join(root, filename))
156+
os.unlink(str(Path(root, filename)))
157157
except FileNotFoundError:
158158
pass
159159
except OSError as exc:
@@ -165,10 +165,10 @@ def clear(self) -> None:
165165
def _key_to_cachefile_path(
166166
self, namespace: str, key: str | dict[str, Hashable]
167167
) -> str:
168-
namespace_path = os.path.join(self.cache_dir, namespace)
168+
namespace_path = str(Path(self.cache_dir, namespace))
169169
hashed_key = _make_cache_key(key)
170170

171-
cache_path = os.path.join(namespace_path, hashed_key + self.file_ext)
171+
cache_path = str(Path(namespace_path, hashed_key + self.file_ext))
172172

173173
return cache_path
174174

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py{38,39,310,311,py3},codestyle,lint,typecheck
2+
envlist = py{38,39,310,311,312,py38},codestyle,lint,typecheck
33

44
[testenv]
55
commands = pytest {posargs}

0 commit comments

Comments
 (0)