classification
Title: Support pathlike objects on dbm/shelve
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, DahlitzFlorian, DavidMertz, dreamsorcerer, ethan.furman, gene_wood, hakancelik, python-dev, serhiy.storchaka
Priority: normal Keywords: easy, patch

Created on 2020-05-08 14:55 by BTaskaya, last changed 2021-09-10 12:38 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20274 closed hakancelik, 2020-05-20 22:45
PR 21849 merged python-dev, 2020-08-12 18:00
Messages (9)
msg368446 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2020-05-08 14:55
>>> dbm.open("/tmp/x.db", "n").close()
>>> from pathlib import Path
>>> tmp = Path("/tmp")
>>> dbm.open(tmp / "x.db", "n").close()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/dbm/__init__.py", line 95, in open
    return mod.open(file, flag, mode)
TypeError: open() argument 1 must be str, not PosixPath
msg368447 - (view) Author: Hakan Çelik (hakancelik) * Date: 2020-05-08 15:12
I work on this issue to fix it.
msg369471 - (view) Author: Florian Dahlitz (DahlitzFlorian) * Date: 2020-05-20 19:29
Are you still working on this @hakancelik?
msg369478 - (view) Author: Hakan Çelik (hakancelik) * Date: 2020-05-20 21:41
Yes I will send pr soon.
msg401348 - (view) Author: Gene Wood (gene_wood) Date: 2021-09-07 22:46
@DahlitzFlorian it looks like a PR was submitted August of last year : https://github.com/python/cpython/pull/21849
msg401430 - (view) Author: David Mertz (DavidMertz) * Date: 2021-09-09 00:42
I've made the few additional changes to those in this PR.  When I work out the issues, I'll make a new PR.  I took out an attempt with `path_t`.  However, here is why I think argument clinic (or something else?!) is actually intercepting the attempted call:

With my temporary debugging, I have this function in `Modules/_gdbmmodule.c`:

```c
[clinic start generated code]*/

static PyObject *
dbmopen_impl(PyObject *module, PyObject *filename, const char *flags,
             int mode)
/*[clinic end generated code: output=9527750f5df90764 input=812b7d74399ceb0e]*/
{
    PyObject_Print(filename, stdout, 0);
    printf(" from _gdbmmodule.c (XXX)\n");
    /* ... rest of function ...*/
```

And I have a very simplified test script:

```python
import _gdbm
import sys
from pathlib import Path

print(sys.version)
path = '/tmp/tmp.db'

db = _gdbm.open(path, 'c')
print("Opened with string path")
db.close()

db = _gdbm.open(Path(path), 'c')
print("Opened with path-like")
db.close()
```

The output of running this is:

```
3.11.0a0 (heads/[bpo-45133](https://bugs.python.org/issue45133)-dirty:0376feb030, Sep  8 2021, 00:39:39) [GCC 10.3.0]
'/tmp/tmp.db' from _gdbmmodule.c (XXX)
Opened with string path
Traceback (most recent call last):
  File "/home/dmertz/tmp/pathlike-dbm.py", line 12, in <module>
    db = _gdbm.open(Path(path), 'c')
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: open() argument 1 must be str, not PosixPath
```

So before I get to the first line of the _gdbm.open() function, the TypeError is already occurring when passed a PosixPath.
msg401431 - (view) Author: David Mertz (DavidMertz) * Date: 2021-09-09 00:48
If anyone wants to look at my not-yet-complete changes (see other comment), it's https://github.com/DavidMertz/cpython/tree/bpo-45133.  It has a different bpo because I filed a duplicate before realizing.  I can change the branch name before a PR, but making it work is the more important issue.
msg401575 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-10 12:26
New changeset 707137b8637feef37b2e06a851fdca9d1b945861 by Henry-Joseph Audéoud in branch 'main':
bpo-40563: Support pathlike objects on dbm/shelve (GH-21849)
https://github.com/python/cpython/commit/707137b8637feef37b2e06a851fdca9d1b945861
msg401576 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-09-10 12:38
Thank you for your contribution, Hakan, Henry-Joseph.

Sorry, David, there was already a PR, and it was not dead, it just waited for a coredev review.
History
Date User Action Args
2021-09-10 12:38:18serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg401576

stage: patch review -> resolved
2021-09-10 12:26:22serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg401575
2021-09-09 00:48:58DavidMertzsetmessages: + msg401431
2021-09-09 00:42:28DavidMertzsetversions: + Python 3.11, - Python 3.9
nosy: + DavidMertz

messages: + msg401430

type: enhancement
2021-09-08 00:54:47ethan.furmansetnosy: + ethan.furman
2021-09-07 22:46:53gene_woodsetnosy: + gene_wood
messages: + msg401348
2020-08-12 18:00:45python-devsetnosy: + python-dev
pull_requests: + pull_request20977
2020-06-02 20:11:43dreamsorcerersetnosy: + dreamsorcerer
2020-05-20 22:45:38hakanceliksetkeywords: + patch
stage: patch review
pull_requests: + pull_request19554
2020-05-20 21:41:52hakanceliksetmessages: + msg369478
2020-05-20 19:29:58DahlitzFloriansetnosy: + DahlitzFlorian
messages: + msg369471
2020-05-19 12:59:19serhiy.storchakalinkissue40681 superseder
2020-05-08 15:12:23hakanceliksetnosy: + hakancelik
messages: + msg368447
2020-05-08 14:55:55BTaskayacreate