{"record":{"id":"c978164dd7a57e82","repo":"kovidgoyal/kitty","slug":"cannot-specify-both-name-and-size","errorCode":null,"errorMessage":"Cannot specify both name and size","messagePattern":"Cannot specify both name and size","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"kitty/shm.py","lineNumber":65,"sourceCode":"    size_fmt = '!I'\n    num_bytes_for_size = struct.calcsize(size_fmt)\n\n    def __init__(\n        self,\n        name: str = '',\n        size: int = 0,\n        readonly: bool = False,\n        mode: int = stat.S_IREAD | stat.S_IWRITE,\n        prefix: str = 'kitty-',\n        unlink_on_exit: bool = False,\n        ignore_close_failure: bool = False,\n    ):\n        self.unlink_on_exit = unlink_on_exit\n        self.ignore_close_failure = ignore_close_failure\n        if size < 0:\n            raise TypeError(\"'size' must be a non-negative integer\")\n        if size and name:\n            raise TypeError('Cannot specify both name and size')\n        if not name:\n            flags = os.O_CREAT | os.O_EXCL\n            if not size:\n                raise TypeError(\"'size' must be > 0\")\n        else:\n            flags = 0\n        flags |= os.O_RDONLY if readonly else os.O_RDWR\n\n        tries = 30\n        while not name and tries > 0:\n            tries -= 1\n            q = make_filename(prefix)\n            try:\n                self._fd = shm_open(q, flags, mode)\n                name = q\n            except FileExistsError:\n                continue\n        if tries <= 0:","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kitty/shm.py#L47-L83","documentation":"Raised by SharedMemory's __init__ when both a `name` and a `size` are passed. Opening an existing named SHM object takes a name; creating a new one takes a size — doing both is contradictory because the size of an existing object is already fixed. The library enforces this with a TypeError before attempting shm_open.","triggerScenarios":"Calling SharedMemory(name='foo', size=4096) — any constructor invocation where both arguments are truthy.","commonSituations":"Copy-pasting code that opens an existing segment and then adding a size 'to be safe'; refactoring a create call into an open call without removing the size kwarg.","solutions":["Pass only `name` to open an existing segment, or only `size` to create a new anonymous one","If you want to resize, delete and recreate the segment instead"],"exampleFix":"// before\nshm = SharedMemory(name='kitty-shm-1', size=4096)\n// after\nshm = SharedMemory(name='kitty-shm-1')  # existing\n# or\nshm = SharedMemory(size=4096)  # create new","handlingStrategy":"validation","validationCode":"def open_shm(name=None, size=0):\n    if name and size:\n        raise ValueError('pass either name or size, not both')\n    return SharedMemory(name=name, size=size)","typeGuard":"def is_create_kwargs(kwargs: dict) -> bool:\n    return bool(kwargs.get('name')) != bool(kwargs.get('size'))","tryCatchPattern":"try:\n    shm = SharedMemory(name=n, size=s)\nexcept TypeError as e:\n    if 'both name and size' in str(e):\n        shm = SharedMemory(name=n)  # fallback: open existing\n    else:\n        raise","preventionTips":["Decide upfront whether you are creating or opening a segment and pass exactly one of name/size","Wrap segment creation in a small factory that enforces the either/or rule"],"tags":["shared-memory","constructor","invalid-arguments"],"backgroundTag":"invalid-argument-combination","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}