How can I create an in-memory file object that has a file descriptor in Python?

I plan to use subprocess.Popen (in Python 3.11.2) to implement git mktree < foo.txt. Now I face the question.

In order to reproduce my situation, here is the script that creates the environment.

#!/bin/bash

export GIT_AUTHOR_NAME=foo
export [email protected]
export GIT_AUTHOR_DATE="Tue Jun 4 21:40:15 2024 +0800"
export GIT_COMMITTER_NAME=foo
export [email protected]
export GIT_COMMITTER_DATE="Tue Jun 4 21:40:15 2024 +0800"

rm -rf foo
git init foo
mkdir -p foo/hello
echo hello > foo/hello/hello.txt
echo hello > foo/hello.txt
echo world > foo/world.txt
git -C foo add .
git -C foo commit -m 'hello world'
git -C foo log --no-decorate
git -C foo ls-tree HEAD hello.txt
git -C foo ls-tree HEAD world.txt

It’s expected to print the commit and 2 blob entries.

commit d2b25fd15c1435f515dd6379eca8d691dde6abeb
Author: foo <[email protected]>
Date:   Tue Jun 4 21:40:15 2024 +0800

    hello world
100644 blob ce013625030ba8dba906f756967f9e9ca394464a    hello.txt
100644 blob cc628ccd10742baea8241c5924df992b5c019f71    world.txt

I want to create a commit from the tree that has only hello.txt and world.txt.

import subprocess

# get the blob entry of hello.txt
o, e = subprocess.Popen(
    ['git', 'ls-tree', 'HEAD', 'hello.txt'],
    stdout=subprocess.PIPE,
    env={'GIT_DIR': 'foo/.git'},
).communicate()
line1 = o.decode()

# get the blob entry of world.txt
o, e = subprocess.Popen(
    ['git', 'ls-tree', 'HEAD', 'world.txt'],
    stdout=subprocess.PIPE,
    env={'GIT_DIR': 'foo/.git'},
).communicate()
line2 = o.decode()

# write the 2 lines to foo.txt
with open('foo.txt', 'w') as f:
    f.write(line1)
    f.write(line2)

# create a new tree object from foo.txt
with open('foo.txt') as f:
    o, e = subprocess.Popen(
        ['git', 'mktree'],
        stdin=f,
        stdout=subprocess.PIPE,
        env={'GIT_DIR': 'foo/.git'},
    ).communicate()
    tree = o.decode()
    print(f'created tree object {tree}')

I wonder if I can use an in-memory file object so that I don’t have to create and remove foo.txt. As io.StringIO is recommended in many answers, I try the code.

import subprocess
import io

line1 = '100644 blob ce013625030ba8dba906f756967f9e9ca394464athello.txtn'
line2 = '100644 blob cc628ccd10742baea8241c5924df992b5c019f71tworld.txtn'

with io.StringIO(line1 + line2) as f:
    o, e = subprocess.Popen(
        ['git', 'mktree'],
        stdin=f,
        stdout=subprocess.PIPE,
        env={'GIT_DIR': 'foo/.git'},
    ).communicate()
    tree = o.decode()
    print(f'created tree object {tree}')

It raises an exception.

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:Python311Libsubprocess.py", line 892, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:Python311Libsubprocess.py", line 1339, in _get_handles
    p2cread = msvcrt.get_osfhandle(stdin.fileno())
                                   ^^^^^^^^^^^^^^
io.UnsupportedOperation: fileno

According to the io doc, it seems io.StringIO does not have a file descriptor.

fileno()

Return the underlying file descriptor (an integer) of the
stream if it exists. An OSError is raised if the IO object does not
use a file descriptor.

Is there any in-memory file object that has a file descriptor? Or is there any method to bypass the exception with io.StringIO in subprocess.Popen?

Thanks for any help.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật