forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.i
More file actions
81 lines (70 loc) · 3.12 KB
/
strings.i
File metadata and controls
81 lines (70 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
// Wrapper functions to provide a scripting-language-friendly interface
// to our string libraries.
//
// NOTE: as of 2005-01-13, this SWIG file is not used to generate a pywrap
// library for manipulation of various string-related types or access
// to the special string functions (Python has plenty). This SWIG file
// should be %import'd so that other SWIG wrappers have proper access
// to the types in //strings (such as the StringPiece object). We may
// generate a pywrap at some point in the future.
//
// NOTE: (Dan Ardelean) as of 2005-11-15 added typemaps to convert Java String
// arguments to C++ StringPiece& objects. This is required because a
// StringPiece class does not make sense - the code SWIG generates for a
// StringPiece class is useless, because it releases the buffer set in
// StringPiece after creating the object. C++ StringPiece objects rely on
// the buffer holding the data being allocated externally.
// NOTE: for now, we'll just start with what is needed, and add stuff
// as it comes up.
%{
#include "tensorflow/core/lib/core/stringpiece.h"
// Handles str in Python 2, bytes in Python 3.
// Returns true on success, false on failure.
bool _BytesToStringPiece(PyObject* obj, tensorflow::StringPiece* result) {
if (obj == Py_None) {
result->clear();
} else {
char* ptr;
Py_ssize_t len;
if (PyBytes_AsStringAndSize(obj, &ptr, &len) == -1) {
// Python has raised an error (likely TypeError or UnicodeEncodeError).
return false;
}
result->set(ptr, len);
}
return true;
}
%}
%typemap(typecheck) tensorflow::StringPiece = char *;
%typemap(typecheck) const tensorflow::StringPiece & = char *;
// "tensorflow::StringPiece" arguments must be specified as a 'str' or 'bytes' object.
%typemap(in) tensorflow::StringPiece {
if (!_BytesToStringPiece($input, &$1)) SWIG_fail;
}
// "const tensorflow::StringPiece&" arguments can be provided the same as
// "tensorflow::StringPiece", whose typemap is defined above.
%typemap(in) const tensorflow::StringPiece & (tensorflow::StringPiece temp) {
if (!_BytesToStringPiece($input, &temp)) SWIG_fail;
$1 = &temp;
}
// C++ functions returning tensorflow::StringPiece will simply return bytes in Python,
// or None if the StringPiece contained a NULL pointer.
%typemap(out) tensorflow::StringPiece {
if ($1.data()) {
$result = PyBytes_FromStringAndSize($1.data(), $1.size());
} else {
Py_INCREF(Py_None);
$result = Py_None;
}
}