A bare #define at the start of a line in a brace delimited %fragment body is
consumed by the SWIG preprocessor and never reaches the generated wrapper. The
macro therefore only exists at SWIG level and code that SWIG does not macro
expand, such as a typemap delimited with %{ ... %}, is left referring to an
undefined name. Use %#define so the definition is emitted for the C compiler.
Fixes SWIG_FromCharPtrAndSize for Python, SWIG_FromBinaryCharPtrAndSize for
Octave, Perl, R and Ruby, SWIG_ToUint8Array and SWIG_FromUint8Array for
JavaScript, and the SWIG_AsVal_* and SWIG_From_* macros in the Scilab char,
short, signed char, unsigned char, unsigned long and long long files.
Lib/scilab/scipointer.swg is deliberately left alone. Its fragments are never
requested by any typemap, so they are only emitted as SWIG preprocessor
definitions; switching them to %#define drops SWIG_ConvertPtr and
SWIG_NewPointerObj from the generated code entirely. Fixing those needs the
definitions moving to a runtime insert, as the other target languages do.
Add a common test case charptr_fragment. It uses a %{ ... %} delimited typemap
so the macro name survives into the wrapper, which only compiles when the
library defines the macro for the C compiler. The target languages without
these macros wrap the same functions using their default typemaps. Verified
that the generated Python module fails to load before this change with an
undefined symbol for SWIG_FromCharPtrAndSize.
Follows on from #3522.
Assisted-by: Claude Code (Opus 5)
The cdata.i (const void *BYTES, size_t LENGTH) typemap took LENGTH from
SWIG_AsCharPtrAndSize, whose psize includes a trailing NUL (data length
+ 1). When the destination buffer is sized exactly to the data, memmove
wrote one byte past the end. Confirmed with valgrind: "Invalid write of
size 1, 0 bytes after a block of size 512".
- Make SWIG_AsCharPtrAndSize binary aware in the languages that use the
generic Lib/cdata.i (Python, Perl, Ruby, Octave, R): when the caller
passes SWIG_BINARYSTR it now reports the exact byte count, not data
+ 1. Non cdata callers are unaffected as they never set the flag.
- Lua li_cdata_bytes tests looped to 0x99 instead of 0xff, verifying
only 154 of the 256 byte values; corrected to 0xff.
- Tcl cdata.i passed an int * to Tcl_GetSizeIntFromObj, which expects
a Tcl_Size * on Tcl 8.7 and 9; use Tcl_Size and reject values that
are not a byte.
- CHANGES.current: document the cdata type change and the overflow fix.
Assisted-by: Claude Opus 4.7
Use the full English word in the identifiers added by the previous
commit for cdata raw byte handling:
SWIG_BINSTR -> SWIG_BINARYSTR
SWIG_BINSTRMASK -> SWIG_BINARYSTRMASK
SWIG_AddBinMask -> SWIG_AddBinaryStrMask
SWIG_DelBinMask -> SWIG_DelBinaryStrMask
SWIG_IsBinStr -> SWIG_IsBinaryStr
SWIG_FromBinCharPtrAndSize -> SWIG_FromBinaryCharPtrAndSize
useBin (local variable) -> use_binary
Touches the constants in Lib/swigrun.swg, the SWIG_FromBinary*
macro defines in pystrings.swg, perlstrings.swg, rubystrings.swg,
octprimtypes.swg and rfragments.swg, and all call sites in
Lib/cdata.i and Lib/python/pystrings.swg.
Assisted-by: Claude Opus 4.7
Fix guile cdata.
Add SWIG_BINSTR flag
- Add SWIG_FromBinCharPtrAndSize with the new flag
To add languages that use Lib/cdata.i.
- python use binary string by using the new SWIG_BINSTR
in SWIG_AsCharPtrAndSize and SWIG_FromBinCharPtrAndSize.
Languages that were changed to uses list
- Tcl use list of integers.
- scilab use list of uint8.
And support passing list of numbers to C.
Add support to JavaScript
- use Uint8Array instead of strings.
- Add li_cdata_carrays. tests.
- Add li_cdata_bytes tests.
- Use Debian node-addon-api package location.
- napi folder location to Examples/test-suite/javascript/Makefile.in.
- Update documentation.
Update cdata.i documentation
This new cdata test focus on:
- Ensure we can receive proper data from C and pass proper data back to C.
- Use all possoble byte values , i.e. the full range of 0 to 255
and ensure values 128 to 255 do not pass Unicode transformation (UTF-8/16).
- Ensure zero is a valid value and not a string null termination
nor a modified UTF-8 which transform U+0000 to 0xC0 0x80.
- Check mutability of the cdata object.
Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
- [Lua] Fix off-by-one when nil terminates the table early: use $1 = i
(not i + 1) to match the count of strings actually copied, and pop
the nil from the Lua stack before breaking.
- [Python] In SWIG_AsCharPtrAndSize (pystrings.swg) decref bytes / obj
on the SWIG_MemoryError early returns added by the previous commits,
so an allocation failure no longer leaks the temporary PyObject.
- [Octave, Python] In argcargv.i free already-allocated string entries
and the array itself before failing on a per-element OOM (Python uses
the existing break path so the freearg typemap performs the cleanup).
- [Perl, PHP, Tcl] Drop unreachable `goto fail` after SWIG_croak /
SWIG_PHP_Error / SWIG_exception_fail, all of which already invoke
SWIG_fail. Compilers warning on unreachable code complained.
- Standardise the OOM error wording across languages and fix the
3- and 5-space indentation introduced by the previous commits.
- Update Examples/perl5/xmlstring/xmlstring.i to the new
%typemaps_string signature (it picks up the missing WarningLeakMsg
argument too) and add a SWIG_NewCopyXMLChArray fragment.
- CHANGES.current: document the API breakages introduced by removing
%new_copy_array, removing %typemaps_string_alloc and extending
%typemaps_string, including the actual error text users will see.
Assisted-by: Claude Opus 4.7 <noreply@anthropic.com>
* Python: Fix warnings about implicit type conversions
When compiling with -Wconversion -Wsign-conversion on
gcc, these places resulted in warnings.
Add explicit type casts to be clear to the compiler.
* new_copy_array casts size to size_t
This alleviates warnings when -Wconversion -Wsign-conversion
are enabled.
* Fix various conversions to correct integer signedness
len can be signed as is the case with python's size_t.
This casts it to size_t to avoid compilation warnings.
* Add -Wconversion and -Wsign-conversion compilation flags for tests
Only make a copy of the returned string if the passed in Python object
does not own the returned char* string. Callers of SWIG_AsCharPtrAndSize
should now ensure that the returned string is only used while the passed
in PyObject * string is valid. Performance improvement is for Python 3
input string handling.
The new SWIG_PyUnicode_AsUTF8AndSize function allows for this simplification.
This option, if used, has not had any effect on Python 3 code since commit a863d3 9 years ago.
I think we can assume that it is not needed for Python 3.
Running the examples and test-suite (Python 2) doesn't change the code
paths with and without -safecstrings because only SWIG_OLDOBJ and SWIG_NEWOBJ
are used in the typemaps and the following code is thus unaltered by -safecstrings
(which sets SWIG_PYTHON_SAFE_CSTRINGS):
%#if defined(SWIG_PYTHON_SAFE_CSTRINGS)
if (*alloc != SWIG_OLDOBJ)
%#else
if (*alloc == SWIG_NEWOBJ)
%#endif
{
*cptr = %new_copy_array(cstr, len + 1, char);
*alloc = SWIG_NEWOBJ;
printf("safe strings: %s\n", *cptr ? *cptr : "NULLSTRING");
} else {
*cptr = cstr;
*alloc = SWIG_OLDOBJ;
}
Note: nosafecstrings was also the default and -O didn't actually change this.
... if available on the version of Python that's in use. This allows
obtaining the original byte string (and potentially trying a fallback
encoding) if the bytes can't be decoded as UTF-8.
Previously, a UnicodeDecodeError would be raised with no way to treat
the data as bytes or try another codec.
- Fixed naming conventions; SwigPyBuiltin is used a lot
- Removed use of std::vector
- builtin.swg isn't included if -builtin isn't specified
- Changed many feature names to use a "python:" prefix
- Eliminated static vars in std_pair.i
- Eliminated C++-style comments (//)
- Enabled autodoc and docstring with -builtin
- Fixed non-ansi generated C code
- Detect and complain if two incompatible swig modules are loaded
- Removed argcargvtest_runme3.py, and fixed argcargvtest_runme.py
so that 2to3 handles it better
- Removed anonymous namespaces
- Eliminated builtin_init typemaps; consolidated functionality into
SWIG_Python_NewPointerObj
- Eliminate printf warnings from %U conversion character by switching
to %S, which works just as well
- Fixed li_std_set_runme.py for python3, which returns set members in
a different order from python2
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/szager-python-builtin@12562 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This is the (incomplemete) log produced by svnmerge.py:
Merged revisions 10405-10409,10420-10422,10426,10438,10445,10451,10454-10465,10467,10473-10475,10485,10488-10489,10493-10495,10497,10509-10510,10513-10514,10517,10520,10525,10528-10529,10533-10535,10554-10557,10570,10573,10593,10614,10666-10669,10673,10678,10687,10690,10704-10706,10731,10744,10750-10752,10755,10759,10770,10775-10776,10813,10819 via svnmerge from
https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-bhy
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10834 626c5289-ae23-0410-ae9c-e8d60b6d4f22