Skip to content

Writing to an SPSS already open file crashes the process in Windows #328

@pablodegrande

Description

@pablodegrande

Describe the issue

Whe trying to create a df to SPSS format, if the file is opened in windows, it kills the process.

To Reproduce

Run the script below in Windows and the process hangs

File example


import pandas as pd
import pyreadstat
import msvcrt
import time

import sys
import platform
import pandas as pd
import pyreadstat

print("Python:", sys.version.replace("\n", " "))
print("Platform:", platform.platform())
print("pandas:", pd.__version__)
print("pyreadstat:", pyreadstat.__version__)

ruta_sav = "salida.sav"

df_init = pd.DataFrame({"id": [0], "nombre": ["init"]})
pyreadstat.write_sav(df_init, ruta_sav)

f = open(ruta_sav, "r+b")
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)  # lock no bloqueante

print("archivo lockeado, intentando escribir...")

try:
    df_dpto = pd.DataFrame({
        "id": [1, 2, 3],
        "nombre": ["A", "B", "C"]
    })

    pyreadstat.write_sav(df_dpto, ruta_sav)

    print("escritura completada (no debería si el lock fue efectivo)")
except Exception as e:
    print("error:", e)

time.sleep(10)  # mantener lock activo
f.close()

Expected behavior

print the lock error.

Setup Information:

pip
Python: 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)]
Platform: Windows-10-10.0.26200-SP0
pandas: 1.5.3
pyreadstat: 1.3.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions