Import Upstream version 2.7.18

This commit is contained in:
geos_one
2025-08-15 16:28:06 +02:00
commit ba1f69ab39
4521 changed files with 1778434 additions and 0 deletions

1760
Mac/Modules/res/_Resmodule.c Normal file

File diff suppressed because it is too large Load Diff

102
Mac/Modules/res/resedit.py Normal file
View File

@@ -0,0 +1,102 @@
##resource_body = """
##char *buf;
##int len;
##Handle h;
##
##if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
## return NULL;
##h = NewHandle(len);
##if ( h == NULL ) {
## PyErr_NoMemory();
## return NULL;
##}
##HLock(h);
##memcpy(*h, buf, len);
##HUnlock(h);
##_res = ResObj_New(h);
##return _res;
##"""
##
##f = ManualGenerator("Resource", resource_body)
##f.docstring = lambda: """Convert a string to a resource object.
##
##The created resource object is actually just a handle,
##apply AddResource() to write it to a resource file.
##See also the Handle() docstring.
##"""
##functions.append(f)
handle_body = """
char *buf;
int len;
Handle h;
ResourceObject *rv;
if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
return NULL;
h = NewHandle(len);
if ( h == NULL ) {
PyErr_NoMemory();
return NULL;
}
HLock(h);
memcpy(*h, buf, len);
HUnlock(h);
rv = (ResourceObject *)ResObj_New(h);
rv->ob_freeit = PyMac_AutoDisposeHandle;
_res = (PyObject *)rv;
return _res;
"""
f = ManualGenerator("Handle", handle_body)
f.docstring = lambda: """Convert a string to a Handle object.
Resource() and Handle() are very similar, but objects created with Handle() are
by default automatically DisposeHandle()d upon object cleanup. Use AutoDispose()
to change this.
"""
functions.append(f)
# Convert resources to other things.
as_xxx_body = """
_res = %sObj_New((%sHandle)_self->ob_itself);
return _res;
"""
def genresconverter(longname, shortname):
f = ManualGenerator("as_%s"%longname, as_xxx_body%(shortname, longname))
docstring = "Return this resource/handle as a %s"%longname
f.docstring = lambda docstring=docstring: docstring
return f
resmethods.append(genresconverter("Control", "Ctl"))
resmethods.append(genresconverter("Menu", "Menu"))
# The definition of this one is MacLoadResource, so we do it by hand...
f = ResMethod(void, 'LoadResource',
(Handle, 'theResource', InMode),
)
resmethods.append(f)
#
# A method to set the auto-dispose flag
#
AutoDispose_body = """
int onoff, old = 0;
if (!PyArg_ParseTuple(_args, "i", &onoff))
return NULL;
if ( _self->ob_freeit )
old = 1;
if ( onoff )
_self->ob_freeit = PyMac_AutoDisposeHandle;
else
_self->ob_freeit = NULL;
_res = Py_BuildValue("i", old);
return _res;
"""
f = ManualGenerator("AutoDispose", AutoDispose_body)
f.docstring = lambda: "(int)->int. Automatically DisposeHandle the object on Python object cleanup"
resmethods.append(f)

View File

@@ -0,0 +1,83 @@
# Scan Resources.h header file, generate resgen.py and Resources.py files.
# Then run ressupport to generate Resmodule.c.
# (Should learn how to tell the compiler to compile it as well.)
import sys
import MacOS
from bgenlocations import TOOLBOXDIR, BGENDIR
sys.path.append(BGENDIR)
from scantools import Scanner
def main():
input = "Resources.h"
output = "resgen.py"
defsoutput = TOOLBOXDIR + "Resources.py"
scanner = ResourcesScanner(input, output, defsoutput)
scanner.scan()
scanner.close()
print "=== Testing definitions output code ==="
execfile(defsoutput, {}, {})
print "=== Done scanning and generating, now doing 'import ressupport' ==="
import ressupport
print "=== Done 'import ressupport'. It's up to you to compile Resmodule.c ==="
class ResourcesScanner(Scanner):
def destination(self, type, name, arglist):
classname = "ResFunction"
listname = "functions"
if arglist:
t, n, m = arglist[0]
if t == "Handle" and m == "InMode":
classname = "ResMethod"
listname = "resmethods"
return classname, listname
def makeblacklistnames(self):
return [
"ReadPartialResource",
"WritePartialResource",
"TempInsertROMMap",
## "RmveResource", # RemoveResource
## "SizeResource", # GetResourceSizeOnDisk
## "MaxSizeRsrc", # GetMaxResourceSize
# OS8 only
'RGetResource',
'OpenResFile',
'CreateResFile',
'RsrcZoneInit',
'InitResources',
'RsrcMapEntry',
]
def makeblacklisttypes(self):
return [
]
def makerepairinstructions(self):
return [
([("Str255", "*", "InMode")],
[("*", "*", "OutMode")]),
([("void_ptr", "*", "InMode"), ("long", "*", "InMode")],
[("InBuffer", "*", "*")]),
([("void", "*", "OutMode"), ("long", "*", "InMode")],
[("InOutBuffer", "*", "*")]),
([("void", "*", "OutMode"), ("long", "*", "InMode"),
("long", "*", "OutMode")],
[("OutBuffer", "*", "InOutMode")]),
([("SInt8", "*", "*")],
[("SignedByte", "*", "*")]),
([("UniCharCount", "*", "InMode"), ("UniChar_ptr", "*", "InMode")],
[("UnicodeReverseInBuffer", "*", "*")]),
]
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,221 @@
# This script will generate the Resources interface for Python.
# It uses the "bgen" package to generate C code.
# It execs the file resgen.py which contain the function definitions
# (resgen.py was generated by resscan.py, scanning the <Resources.h> header file).
from macsupport import *
class ResMixIn:
def checkit(self):
if self.returntype.__class__ != OSErrType:
OutLbrace()
Output("OSErr _err = ResError();")
Output("if (_err != noErr) return PyMac_Error(_err);")
OutRbrace()
FunctionGenerator.checkit(self) # XXX
class ResFunction(ResMixIn, OSErrWeakLinkFunctionGenerator): pass
class ResMethod(ResMixIn, OSErrWeakLinkMethodGenerator): pass
RsrcChainLocation = Type("RsrcChainLocation", "h")
FSCatalogInfoBitmap = FakeType("0") # Type("FSCatalogInfoBitmap", "l")
FSCatalogInfo_ptr = FakeType("(FSCatalogInfo *)0")
# includestuff etc. are imported from macsupport
includestuff = includestuff + """
#include <Carbon/Carbon.h>
#ifdef USE_TOOLBOX_OBJECT_GLUE
extern PyObject *_ResObj_New(Handle);
extern int _ResObj_Convert(PyObject *, Handle *);
extern PyObject *_OptResObj_New(Handle);
extern int _OptResObj_Convert(PyObject *, Handle *);
#define ResObj_New _ResObj_New
#define ResObj_Convert _ResObj_Convert
#define OptResObj_New _OptResObj_New
#define OptResObj_Convert _OptResObj_Convert
#endif
/* Function to dispose a resource, with a "normal" calling sequence */
static void
PyMac_AutoDisposeHandle(Handle h)
{
DisposeHandle(h);
}
"""
finalstuff = finalstuff + """
/* Alternative version of ResObj_New, which returns None for null argument */
PyObject *OptResObj_New(Handle itself)
{
if (itself == NULL) {
Py_INCREF(Py_None);
return Py_None;
}
return ResObj_New(itself);
}
int OptResObj_Convert(PyObject *v, Handle *p_itself)
{
PyObject *tmp;
if ( v == Py_None ) {
*p_itself = NULL;
return 1;
}
if (ResObj_Check(v))
{
*p_itself = ((ResourceObject *)v)->ob_itself;
return 1;
}
/* If it isn't a resource yet see whether it is convertible */
if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) ) {
*p_itself = ((ResourceObject *)tmp)->ob_itself;
Py_DECREF(tmp);
return 1;
}
PyErr_Clear();
PyErr_SetString(PyExc_TypeError, "Resource required");
return 0;
}
"""
initstuff = initstuff + """
PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, ResObj_New);
PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, ResObj_Convert);
PyMac_INIT_TOOLBOX_OBJECT_NEW(Handle, OptResObj_New);
PyMac_INIT_TOOLBOX_OBJECT_CONVERT(Handle, OptResObj_Convert);
"""
module = MacModule('_Res', 'Res', includestuff, finalstuff, initstuff)
class ResDefinition(PEP253Mixin, GlobalObjectDefinition):
getsetlist = [
('data',
"""
PyObject *res;
char state;
state = HGetState(self->ob_itself);
HLock(self->ob_itself);
res = PyString_FromStringAndSize(
*self->ob_itself,
GetHandleSize(self->ob_itself));
HUnlock(self->ob_itself);
HSetState(self->ob_itself, state);
return res;
""",
"""
char *data;
long size;
if ( v == NULL )
return -1;
if ( !PyString_Check(v) )
return -1;
size = PyString_Size(v);
data = PyString_AsString(v);
/* XXXX Do I need the GetState/SetState calls? */
SetHandleSize(self->ob_itself, size);
if ( MemError())
return -1;
HLock(self->ob_itself);
memcpy((char *)*self->ob_itself, data, size);
HUnlock(self->ob_itself);
/* XXXX Should I do the Changed call immediately? */
return 0;
""",
'The resource data'
), (
'size',
'return PyInt_FromLong(GetHandleSize(self->ob_itself));',
None,
'The length of the resource data'
)]
def outputCheckNewArg(self):
Output("if (itself == NULL) return PyMac_Error(resNotFound);")
def outputCheckConvertArg(self):
# if it isn't a resource we may be able to coerce it
Output("if (!%s_Check(v))", self.prefix)
OutLbrace()
Output("PyObject *tmp;")
Output('if ( (tmp=PyObject_CallMethod(v, "as_Resource", "")) )')
OutLbrace()
Output("*p_itself = ((ResourceObject *)tmp)->ob_itself;")
Output("Py_DECREF(tmp);")
Output("return 1;")
OutRbrace()
Output("PyErr_Clear();")
OutRbrace()
def outputStructMembers(self):
GlobalObjectDefinition.outputStructMembers(self)
Output("void (*ob_freeit)(%s ptr);", self.itselftype)
def outputInitStructMembers(self):
GlobalObjectDefinition.outputInitStructMembers(self)
Output("it->ob_freeit = NULL;")
def outputCleanupStructMembers(self):
Output("if (self->ob_freeit && self->ob_itself)")
OutLbrace()
Output("self->ob_freeit(self->ob_itself);")
OutRbrace()
Output("self->ob_itself = NULL;")
def output_tp_newBody(self):
Output("PyObject *self;")
Output
Output("if ((self = type->tp_alloc(type, 0)) == NULL) return NULL;")
Output("((%s *)self)->ob_itself = NULL;", self.objecttype)
Output("((%s *)self)->ob_freeit = NULL;", self.objecttype)
Output("return self;")
def output_tp_initBody(self):
Output("char *srcdata = NULL;")
Output("int srclen = 0;")
Output("%s itself;", self.itselftype);
Output("char *kw[] = {\"itself\", 0};")
Output()
Output("if (PyArg_ParseTupleAndKeywords(_args, _kwds, \"O&\", kw, %s_Convert, &itself))",
self.prefix);
OutLbrace()
Output("((%s *)_self)->ob_itself = itself;", self.objecttype)
Output("return 0;")
OutRbrace()
Output("PyErr_Clear();")
Output("if (!PyArg_ParseTupleAndKeywords(_args, _kwds, \"|s#\", kw, &srcdata, &srclen)) return -1;")
Output("if ((itself = NewHandle(srclen)) == NULL)")
OutLbrace()
Output("PyErr_NoMemory();")
Output("return 0;")
OutRbrace()
Output("((%s *)_self)->ob_itself = itself;", self.objecttype)
# XXXX Output("((%s *)self)->ob_freeit = PyMac_AutoDisposeHandle;")
Output("if (srclen && srcdata)")
OutLbrace()
Output("HLock(itself);")
Output("memcpy(*itself, srcdata, srclen);")
Output("HUnlock(itself);")
OutRbrace()
Output("return 0;")
resobject = ResDefinition('Resource', 'ResObj', 'Handle')
module.addobject(resobject)
functions = []
resmethods = []
execfile('resgen.py')
execfile('resedit.py')
for f in functions: module.add(f)
for f in resmethods: resobject.add(f)
SetOutputFileName('_Resmodule.c')
module.generate()